Skip to content

Commit

Permalink
fix up attachments tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daleharvey committed Apr 2, 2012
1 parent 6cb139c commit 371cffd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
31 changes: 21 additions & 10 deletions src/pouch.js
Expand Up @@ -121,24 +121,28 @@ parseUri.options = {
};

var ajax = function (options, callback) {
options.success = function (obj) {
callback(null, obj);
};
options.error = function (err) {
if (err) callback(err);
else callback(true);
var defaults = {
success: function (obj) {
callback(null, obj);
},
error: function (err) {
if (err) callback(err);
else callback(true);
},
dataType: 'json',
contentType: 'application/json'
};
options = $.extend({}, defaults, options);

if (options.data && typeof options.data !== 'string') {
options.data = JSON.stringify(options.data);
}
options.dataType = 'json';
if (options.auth) {
options.beforeSend = function(xhr) {
var token = btoa(options.auth.username + ":" + options.auth.password);
xhr.setRequestHeader("Authorization", "Basic " + token);
}
}
options.contentType = 'application/json';
$.ajax(options);
};

Expand Down Expand Up @@ -358,10 +362,17 @@ parseUri.options = {
params = params.join('&');
params = params === '' ? '' : '?' + params;

ajax({
var options = {
auth: host.auth,
type: 'GET',
url: genUrl(host, id + params)}, function(err, doc) {
url: genUrl(host, id + params)
};

if (/\//.test(id) && !/^_local/.test(id)) {
options.dataType = false;
}

ajax(options, function(err, doc) {
if (err) {
return call(callback, Errors.MISSING_DOC);
}
Expand Down
39 changes: 27 additions & 12 deletions tests/test.attachments.js
Expand Up @@ -10,24 +10,39 @@ module('attachments', {
}
});

asyncTest("Test some attachments", function() {
var binAttDoc = {
_id: "bin_doc",
_attachments:{
"foo.txt": {
content_type:"text/plain",
data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
}
}
};

var binAttDoc = {
_id: "bin_doc",
_attachments:{
"foo.txt": {
content_type:"text/plain",
data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
}
// empty attachment
var binAttDoc2 = {
_id: "bin_doc2",
_attachments:{
"foo.txt": {
content_type:"text/plain",
data: ""
}
};
}
}

asyncTest("Test some attachments", function() {
initTestDB(this.name, function(err, db) {
db.put(binAttDoc, function(err, _) {
ok(!err, 'saved doc with attachment');
db.get('bin_doc/foo.txt', function() {
console.log(arguments);
start();
db.get('bin_doc/foo.txt', function(err, res) {
ok(res === 'This is a base64 encoded text', 'Correct data returned');
db.put(binAttDoc2, function(err, _) {
db.get('bin_doc2/foo.txt', function(err, res) {
ok(res === '', 'Correct data returned');
start();
});
});
});
});
});
Expand Down
1 change: 0 additions & 1 deletion tests/test.utils.js
Expand Up @@ -33,7 +33,6 @@ function initTestDB(name, callback) {
function initDBPair(local, remote, callback) {
initTestDB(local, function(err, localDb) {
initTestDB(remote, function(err, remoteDb) {
console.log(localDb, remoteDb);
callback(localDb, remoteDb);
});
});
Expand Down

0 comments on commit 371cffd

Please sign in to comment.