Skip to content

Commit

Permalink
Allow an empty response on a successful request (#180)
Browse files Browse the repository at this point in the history
fixes #179
  • Loading branch information
reconbot committed Aug 3, 2016
1 parent 8fea145 commit b21fed8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
12 changes: 9 additions & 3 deletions lib/twitter.js
Expand Up @@ -138,9 +138,15 @@ Twitter.prototype.__request = function(method, path, params, callback) {
return callback(error, data, response);
}

// JSON parse error
// JSON parse error or empty strings
try {
data = JSON.parse(data);
// An empty string is a valid response
if (data === '') {
data = {};
}
else {
data = JSON.parse(data);
}
}
catch(parseError) {
return callback(
Expand All @@ -150,6 +156,7 @@ Twitter.prototype.__request = function(method, path, params, callback) {
);
}


// response object errors
// This should return an error object not an array of errors
if (data.errors !== undefined) {
Expand All @@ -164,7 +171,6 @@ Twitter.prototype.__request = function(method, path, params, callback) {
response
);
}

// no errors
callback(null, data, response);
});
Expand Down
28 changes: 24 additions & 4 deletions test/twitter.js
Expand Up @@ -184,7 +184,7 @@ describe('Twitter', function() {
this.twitter = new Twitter();
});

it('accepts a 2xx response', function(done) {
it('accepts any 2xx response', function(done) {
var jsonResponse = {favorites: [] };
this.nock.get(/.*/).reply(201, jsonResponse);
this.twitter.__request('get', '/tweet', function(error, data, response){
Expand All @@ -195,9 +195,9 @@ describe('Twitter', function() {
});
});

it('errors on non 4xx or 5xx responses', function(done){
it('errors when there is an error object', function(done){
var jsonResponse = {errors: ['nope']};
this.nock.get(/.*/).reply(403, jsonResponse);
this.nock.get(/.*/).reply(203, jsonResponse);
this.twitter.__request('get', '/tweet', function(error, data, response){
assert.deepEqual(error, ['nope']);
assert.deepEqual(data, jsonResponse);
Expand All @@ -207,7 +207,7 @@ describe('Twitter', function() {
});

it('errors on bad json', function(done) {
this.nock.get(/.*/).reply(500, 'fail whale');
this.nock.get(/.*/).reply(200, 'fail whale');
this.twitter.__request('get', '/tweet', function(error, data, response){
assert(error instanceof Error);
assert.equal(data, 'fail whale');
Expand All @@ -216,6 +216,26 @@ describe('Twitter', function() {
});
});

it('allows an empty response', function(done){
this.nock.get(/.*/).reply(201, '');
this.twitter.__request('get', '/tweet', function(error, data, response){
assert.equal(error, null);
assert.deepEqual(data, {});
assert.notEqual(response, null);
done();
});
});

it('errors when there is a bad http status code', function(done) {
this.nock.get(/.*/).reply(500, '{}');
this.twitter.__request('get', '/tweet', function(error, data, response){
assert(error instanceof Error);
assert.deepEqual(data, {});
assert.notEqual(response, null);
done();
});
});

it('errors on a request or network error', function(done) {
this.nock.get(/.*/).replyWithError('something bad happened');
this.twitter.__request('get', '/tweet', function(error, data, response){
Expand Down

0 comments on commit b21fed8

Please sign in to comment.