Skip to content

Commit

Permalink
[fix] Properly escape ids and attachment names
Browse files Browse the repository at this point in the history
  • Loading branch information
indexzero committed Mar 2, 2012
1 parent b44f7dc commit 5d2128f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions lib/cradle/database/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Database.prototype.getAttachment = function (id, attachmentName, callback) {

return this.connection.rawRequest({
method: 'GET',
path: '/' + [this.name, id, attachmentName].map(querystring.escape).join('/')
path: '/' + [this.name, querystring.escape(id), attachmentName].join('/')
}, callback);
};

Expand Down Expand Up @@ -40,7 +40,7 @@ Database.prototype.removeAttachment = function (doc, attachmentName, callback) {

this.query({
method: 'DELETE',
path: [id, attachmentName].join('/'),
path: [querystring.escape(id), attachmentName].join('/'),
query: { rev: rev }
}, callback);
};
Expand Down Expand Up @@ -78,7 +78,7 @@ Database.prototype.saveAttachment = function (doc, attachment, callback) {
}

options.method = 'PUT';
options.path = '/' + [this.name, id, attachmentName].map(querystring.escape).join('/');
options.path = '/' + [this.name, querystring.escape(id), attachmentName].join('/');
options.headers = {
'Content-Type': attachment['content-type']
|| attachment['contentType']
Expand Down
14 changes: 7 additions & 7 deletions test/database-attachment-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ vows.describe('cradle/database/attachments').addBatch({
id: res.id,
rev: res.rev
}, {
name: 'foo.txt',
name: 'cached/foo.txt',
'Content-Type': 'text/plain',
body: 'Foo!'
}, function () {
Expand All @@ -49,8 +49,8 @@ vows.describe('cradle/database/attachments').addBatch({
},
"with the _attachments": function (cached) {
assert.ok(cached._attachments);
assert.ok(cached._attachments['foo.txt']);
assert.equal(cached._attachments['foo.txt'].stub, true);
assert.ok(cached._attachments['cached/foo.txt']);
assert.equal(cached._attachments['cached/foo.txt'].stub, true);
},
"and is valid enough to re-save": {
topic: function (cached, db) {
Expand All @@ -61,7 +61,7 @@ vows.describe('cradle/database/attachments').addBatch({
});
},
"has the attachment": function (res) {
var att = res._attachments['foo.txt'];
var att = res._attachments['cached/foo.txt'];
assert.equal(att.stub, true);
assert.equal(att.content_type, 'text/plain');
assert.equal(att.length, 4);
Expand Down Expand Up @@ -293,7 +293,7 @@ vows.describe('cradle/database/attachments').addBatch({
assert.ok(res.ok);
}
},
"when it doesnt exist": {
"when the document doesnt exist": {
topic: function (db) {
db.removeAttachment({
id: 'YUNOEXIST',
Expand All @@ -318,15 +318,15 @@ vows.describe('cradle/database/attachments').addBatch({
topic: function (db) {
var that = this;
db.get('attachment-cacher', function (err, doc) {
db.removeAttachment(doc._id, 'foo.txt', that.callback);
db.removeAttachment(doc._id, 'cached/foo.txt', that.callback);
});
},
"should remove the attachment": function (err, res) {
assert.isNull(err);
assert.ok(res.ok);
}
},
"when it doesnt exist": {
"when the document doesnt exist": {
topic: function (db) {
db.removeAttachment({
id: 'YUNOEXIST',
Expand Down

0 comments on commit 5d2128f

Please sign in to comment.