diff --git a/lib/test.js b/lib/test.js index 3b3b255..59030a8 100644 --- a/lib/test.js +++ b/lib/test.js @@ -109,7 +109,7 @@ class Test extends Request { * Defer invoking superagent's `.end()` until * the server is listening. * - * @param {Function} fn + * @param {?Function} fn * @api public */ end(fn) { @@ -133,7 +133,7 @@ class Test extends Request { * * @param {?Error} resError * @param {Response} res - * @param {Function} fn + * @param {?Function} fn * @api private */ assert(resError, res, fn) { @@ -169,7 +169,9 @@ class Test extends Request { errorObj = resError; } - fn.call(this, errorObj || null, res); + if (fn) { + fn.call(this, errorObj || null, res); + } } /** diff --git a/test/supertest.js b/test/supertest.js index 9eebf31..68f8a37 100644 --- a/test/supertest.js +++ b/test/supertest.js @@ -9,6 +9,7 @@ try { } const fs = require('fs'); const path = require('path'); +const { Readable } = require('stream'); const should = require('should'); const express = require('express'); const bodyParser = require('body-parser'); @@ -155,6 +156,28 @@ describe('request(app)', function () { .expect('john', done); }); + it('should work with pipe', function (done) { + const app = express(); + + app.use(express.json()); + + app.post('/', function (req, res) { + res.send(req.body.name); + }); + + const body = Readable.from(JSON.stringify({ name: 'john' })); + const req = request(app) + .post('/') + .set('Content-Type', 'application/json') + .on('response', function (res) { + should.exist(res); + res.status.should.be.equal(200); + res.text.should.be.equal('john'); + done(); + }); + body.pipe(req); + }); + it('should work when unbuffered', function (done) { const app = express();