Skip to content

Commit

Permalink
Add a describe() block for streamed responses.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexjeffburke committed Jun 24, 2015
1 parent d65a4ef commit 3050b2d
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion test/BeanBag.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
/*global describe, it, emit, __dirname*/
/*global describe, it, emit, __dirname, JSON*/
var BeanBag = require('../lib/BeanBag'),
httpErrors = require('httperrors'),
socketErrors = require('socketerrors'),
unexpected = require('unexpected'),
fs = require('fs'),
stream = require('stream'),
pathModule = require('path');

describe('BeanBag', function () {
Expand Down Expand Up @@ -171,6 +172,54 @@ describe('BeanBag', function () {
}, 'to call the callback with error', new httpErrors[500](error.message));
});

describe('with a streamed response', function () {
it('should handle simple response stream', function () {
var responseStream = new stream.Readable();
responseStream._read = function () {
responseStream.push(new Buffer(JSON.stringify({ a: 1, b: 2 })));
responseStream.push(null);
};

return expect(function (cb) {
new BeanBag({ url: 'http://localhost:5984/' }).request({ method: 'GET', path: 'foo' }, cb);
}, 'with http mocked out', {
request: {
url: 'GET http://localhost:5984/foo'
},
response: {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: responseStream
}
}, 'to call the callback with no error');
});

it('should throw an error on invalid JSON', function () {
var responseStream = new stream.Readable();
responseStream._read = function () {
responseStream.push(new Buffer('{]'));
responseStream.push(null);
};

return expect(function (cb) {
new BeanBag({ url: 'http://localhost:5984/' }).request({ method: 'GET', path: 'foo' }, cb);
}, 'with http mocked out', {
request: {
url: 'GET http://localhost:5984/foo'
},
response: {
statusCode: 200,
headers: {
'Content-Type': 'application/json'
},
body: responseStream
}
}, 'to call the callback with error', new httpErrors.BadGateway('Error parsing JSON response body'));
});
});

describe('with a query', function () {
it('should allow specifying the query string as a string', function () {
return expect(function (cb) {
Expand Down

0 comments on commit 3050b2d

Please sign in to comment.