Skip to content

Commit

Permalink
added test for readable streams
Browse files Browse the repository at this point in the history
  • Loading branch information
wheresrhys committed Sep 23, 2018
1 parent 1ca5078 commit f0c45ac
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion test/server.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fetchMock = require('../src/server.js');
const expect = require('chai').expect;
const http = require('http');
const sinon = require('sinon');
const { promisify } = require('util');

describe('nodejs tests', () => {
Expand All @@ -19,7 +20,8 @@ describe('nodejs tests', () => {

require('./runner')(fetchMock, global, require('node-fetch'));

describe('support for Buffers', () => {
describe('support for nodejs body types', () => {
afterEach(() => fetchMock.reset());
it('can respond with a buffer', () => {
fetchMock.mock(/a/, new Buffer('buffer'), { sendAsJson: false });
return fetchMock
Expand All @@ -29,5 +31,28 @@ describe('nodejs tests', () => {
expect(txt).to.equal('buffer');
});
});

it('can respond with a readable stream', done => {
const { Readable, Writable } = require('stream');
const readable = new Readable();
const write = sinon.stub().callsFake((chunk, enc, cb) => {
cb();
});
const writable = new Writable({
write
});
readable.push('response string');
readable.push(null);

fetchMock.mock(/a/, readable, { sendAsJson: false });
fetchMock.fetchHandler('http://a.com').then(res => {
res.body.pipe(writable);
});

writable.on('finish', () => {
expect(write.args[0][0].toString('utf8')).to.equal('response string');
done();
});
});
});
});

0 comments on commit f0c45ac

Please sign in to comment.