Commit 49dd5aa7 authored by Romain Courteaud's avatar Romain Courteaud

MemoryStorage: fix include_docs parameter handling.

Copy all documents/attachments set in the storage, to prevent unwanting external modifications
parent 506f646f
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
*/ */
/*jslint nomen: true*/ /*jslint nomen: true*/
/*global jIO*/ /*global jIO, RSVP*/
/** /**
* JIO Memory Storage. Type = 'memory'. * JIO Memory Storage. Type = 'memory'.
...@@ -20,7 +20,7 @@ ...@@ -20,7 +20,7 @@
* @class MemoryStorage * @class MemoryStorage
*/ */
(function (jIO) { (function (jIO, JSON, RSVP) {
"use strict"; "use strict";
/** /**
...@@ -39,13 +39,13 @@ ...@@ -39,13 +39,13 @@
attachments: {} attachments: {}
}; };
} }
this._database[id].doc = metadata; this._database[id].doc = JSON.stringify(metadata);
return id; return id;
}; };
MemoryStorage.prototype.get = function (id) { MemoryStorage.prototype.get = function (id) {
try { try {
return this._database[id].doc; return JSON.parse(this._database[id].doc);
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
...@@ -92,7 +92,7 @@ ...@@ -92,7 +92,7 @@
404 404
); );
} }
return result; return jIO.util.dataURItoBlob(result);
} catch (error) { } catch (error) {
if (error instanceof TypeError) { if (error instanceof TypeError) {
throw new jIO.util.jIOError( throw new jIO.util.jIOError(
...@@ -114,7 +114,13 @@ ...@@ -114,7 +114,13 @@
} }
throw error; throw error;
} }
attachment_dict[name] = blob; return new RSVP.Queue()
.push(function () {
return jIO.util.readBlobAsDataURL(blob);
})
.push(function (evt) {
attachment_dict[name] = evt.target.result;
});
}; };
MemoryStorage.prototype.removeAttachment = function (id, name) { MemoryStorage.prototype.removeAttachment = function (id, name) {
...@@ -145,7 +151,7 @@ ...@@ -145,7 +151,7 @@
rows.push({ rows.push({
id: i, id: i,
value: {}, value: {},
doc: this._database[i] doc: JSON.parse(this._database[i].doc)
}); });
} else { } else {
rows.push({ rows.push({
...@@ -161,4 +167,4 @@ ...@@ -161,4 +167,4 @@
jIO.addStorage('memory', MemoryStorage); jIO.addStorage('memory', MemoryStorage);
}(jIO)); }(jIO, JSON, RSVP));
...@@ -58,9 +58,7 @@ ...@@ -58,9 +58,7 @@
equal(uuid, "put1"); equal(uuid, "put1");
deepEqual(test.jio.__storage._database.put1, { deepEqual(test.jio.__storage._database.put1, {
attachments: {}, attachments: {},
doc: { doc: "{\"title\":\"myPut1\"}"
"title": "myPut1"
}
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -88,9 +86,7 @@ ...@@ -88,9 +86,7 @@
deepEqual(test.jio.__storage._database.put1, { deepEqual(test.jio.__storage._database.put1, {
"foo": "bar", "foo": "bar",
"attachments": {"foo": "bar"}, "attachments": {"foo": "bar"},
doc: { doc: "{\"title\":\"myPut2\"}"
"title": "myPut2"
}
}); });
}) })
.fail(function (error) { .fail(function (error) {
...@@ -133,7 +129,7 @@ ...@@ -133,7 +129,7 @@
test("get document", function () { test("get document", function () {
var id = "post1"; var id = "post1";
this.jio.__storage._database[id] = { this.jio.__storage._database[id] = {
"doc": {title: "myPost1"} "doc": "{\"title\":\"myPost1\"}"
}; };
stop(); stop();
expect(1); expect(1);
...@@ -156,7 +152,7 @@ ...@@ -156,7 +152,7 @@
var id = "putattmt1"; var id = "putattmt1";
this.jio.__storage._database[id] = { this.jio.__storage._database[id] = {
"doc": {}, "doc": "{}",
"attachments": { "attachments": {
putattmt2: undefined putattmt2: undefined
} }
...@@ -370,14 +366,14 @@ ...@@ -370,14 +366,14 @@
this.jio.__storage._database[id] = { this.jio.__storage._database[id] = {
"doc": JSON.stringify({}), "doc": JSON.stringify({}),
"attachments": { "attachments": {
"putattmt2": blob "putattmt2": "data:x-application/foo;base64,dGVzdA=="
} }
}; };
this.jio.getAttachment(id, attachment) this.jio.getAttachment(id, attachment)
.then(function (result) { .then(function (result) {
ok(result instanceof Blob, "Data is Blob"); ok(result instanceof Blob, "Data is Blob");
equal(result, blob); deepEqual(result, blob);
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -431,7 +427,8 @@ ...@@ -431,7 +427,8 @@
jio.putAttachment(id, "putattmt2", blob) jio.putAttachment(id, "putattmt2", blob)
.then(function () { .then(function () {
equal(jio.__storage._database[id].attachments.putattmt2, blob); equal(jio.__storage._database[id].attachments.putattmt2,
"data:x-application/foo;base64,dGVzdA==");
}) })
.fail(function (error) { .fail(function (error) {
ok(false, error); ok(false, error);
...@@ -555,8 +552,8 @@ ...@@ -555,8 +552,8 @@
}); });
test("list documents with include_docs", function () { test("list documents with include_docs", function () {
this.jio.__storage._database.foo2 = "bar2"; this.jio.__storage._database.foo2 = {doc: "{\"title\":\"bar2\"}"};
this.jio.__storage._database.foo1 = "bar1"; this.jio.__storage._database.foo1 = {doc: "{\"title\":\"bar1\"}"};
stop(); stop();
expect(1); expect(1);
...@@ -569,12 +566,12 @@ ...@@ -569,12 +566,12 @@
{ {
"id": "foo2", "id": "foo2",
"value": {}, "value": {},
"doc": "bar2" "doc": {title: "bar2"}
}, },
{ {
"id": "foo1", "id": "foo1",
"value": {}, "value": {},
"doc": "bar1" "doc": {title: "bar1"}
} }
], ],
"total_rows": 2 "total_rows": 2
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment