Skip to content
Browse files

fix up attachments tests

  • Loading branch information...
1 parent 6cb139c commit 371cffd3a8da387db98bafa39186604e4c8bfe9b @daleharvey daleharvey committed Apr 1, 2012
Showing with 48 additions and 23 deletions.
  1. +21 −10 src/pouch.js
  2. +27 −12 tests/test.attachments.js
  3. +0 −1 tests/test.utils.js
View
31 src/pouch.js
@@ -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);
};
@@ -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);
}
View
39 tests/test.attachments.js
@@ -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();
+ });
+ });
});
});
});
View
1 tests/test.utils.js
@@ -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);
});
});

0 comments on commit 371cffd

Please sign in to comment.
Something went wrong with that request. Please try again.