Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix end function when piping on request #832

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -169,7 +169,9 @@ class Test extends Request {
errorObj = resError;
}

fn.call(this, errorObj || null, res);
if (fn) {
fn.call(this, errorObj || null, res);
}
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/supertest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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();

Expand Down