Skip to content
This repository has been archived by the owner on Mar 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #153 from glynnbird/promisesissues
Browse files Browse the repository at this point in the history
Promisesissues
  • Loading branch information
glynnbird authored Oct 9, 2016
2 parents ec9acec + 2a1e82d commit 0175894
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cloudant.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ function Cloudant(options, callback) {
if (typeof options == "object") {
if (options.requestDefaults) {
requestDefaults = options.requestDefaults;
delete options.requestDefaults;
}
theurl = reconfigure(options);
} else {
Expand All @@ -60,6 +59,7 @@ function Cloudant(options, callback) {
// plugin a request library
var plugin = null;
if (options.plugin) {
options.requestDefaults = requestDefaults;
if(typeof options.plugin === 'string') {
var plugintype = options.plugin || 'default';
debug('Using the "' + plugintype + '" plugin');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "Apache-2.0",
"homepage": "http://github.com/cloudant/nodejs-cloudant",
"repository": "git://github.com/cloudant/nodejs-cloudant",
"version": "1.5.0",
"version": "1.5.1",
"author": "Jason Smith <[email protected]>",
"contributors": [
"Glynn Bird <[email protected]>"
Expand Down
6 changes: 6 additions & 0 deletions plugins/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,16 @@ module.exports = function(options) {
return new Promise(function(resolve, reject) {
request(req, function(err, h, b) {
var statusCode = h && h.statusCode || 500;
if (b) {
try { b = JSON.parse(b); } catch (err) { }
}
if (statusCode >= 200 && statusCode < 400) {
callback(null, h, b);
return resolve(b);
}
if (b) {
b.statusCode = statusCode;
}
reject(err || b);
callback(err, h, b);
})
Expand Down
23 changes: 20 additions & 3 deletions tests/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,32 @@ describe('promise plugin', function() {

it('should return a promise', function(done) {
var mocks = nock(SERVER)
.get('/' + MYDB).reply(200, {});
.get('/' + MYDB).reply(200, { ok: true});
var cloudant = Cloudant({plugin:'promises', account:ME, password: PASSWORD});
var db = cloudant.db.use(MYDB);
var p = db.info().then(function(data) {
data.should.be.an.object;
data.should.be.an.Object;
done();
});
assert.equal(p instanceof Promise, true);
})
});

it('should return an error status code', function(done) {
var mocks = nock(SERVER)
.get('/' + MYDB).reply(404, { ok: false});
var cloudant = Cloudant({plugin:'promises', account:ME, password: PASSWORD});
var db = cloudant.db.use(MYDB);
var p = db.info().then(function(data) {
assert(false);
}).catch(function(e) {
e.should.be.an.Object;
e.should.have.property.statusCode;
e.statusCode.should.be.a.Number;
e.statusCode.should.equal(404);
done();
});
assert.equal(p instanceof Promise, true);
});

});

Expand Down

0 comments on commit 0175894

Please sign in to comment.