From 8118da7430c33fc80a2f60c4a8cb3f568edbb29b Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 14 Feb 2018 12:32:01 +0000 Subject: [PATCH] http: OutgoingMessage.end() should return this PR-URL: https://github.com/nodejs/node/pull/18780 Reviewed-By: James M Snell Reviewed-By: Ruben Bridgewater Reviewed-By: Benjamin Gruenbaum Reviewed-By: Anna Henningsen Reviewed-By: Luigi Pinca --- doc/api/http.md | 10 ++++++++++ lib/_http_outgoing.js | 9 ++++----- test/parallel/test-http-request-end-twice.js | 2 +- test/parallel/test-http-request-end.js | 8 ++++++-- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/doc/api/http.md b/doc/api/http.md index 74ba9e8792c4ef..daa058a8b0b426 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -544,11 +544,16 @@ See [`request.socket`][] ### request.end([data[, encoding]][, callback]) * `data` {string|Buffer} * `encoding` {string} * `callback` {Function} +* Returns: {this} Finishes sending the request. If any parts of the body are unsent, it will flush them to the stream. If the request is @@ -1041,11 +1046,16 @@ See [`response.socket`][]. ### response.end([data][, encoding][, callback]) * `data` {string|Buffer} * `encoding` {string} * `callback` {Function} +* Returns: {this} This method signals to the server that all of the response headers and body have been sent; that server should consider this message complete. diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index f513315b7cd421..985b74679c0027 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -736,7 +736,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { } if (this.finished) { - return false; + return this; } var uncork; @@ -766,12 +766,11 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { var finish = onFinish.bind(undefined, this); - var ret; if (this._hasBody && this.chunkedEncoding) { - ret = this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish); + this._send('0\r\n' + this._trailer + '\r\n', 'latin1', finish); } else { // Force a flush, HACK. - ret = this._send('', 'latin1', finish); + this._send('', 'latin1', finish); } if (uncork) @@ -788,7 +787,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) { this._finish(); } - return ret; + return this; }; diff --git a/test/parallel/test-http-request-end-twice.js b/test/parallel/test-http-request-end-twice.js index 525377d2e1ccbf..47f08fd6e422f0 100644 --- a/test/parallel/test-http-request-end-twice.js +++ b/test/parallel/test-http-request-end-twice.js @@ -31,7 +31,7 @@ const server = http.Server(function(req, res) { server.listen(0, function() { const req = http.get({ port: this.address().port }, function(res) { res.on('end', function() { - assert.ok(!req.end()); + assert.strictEqual(req.end(), req); server.close(); }); res.resume(); diff --git a/test/parallel/test-http-request-end.js b/test/parallel/test-http-request-end.js index 6dd5fa4e91b1b2..a0cdcf27dd03a9 100644 --- a/test/parallel/test-http-request-end.js +++ b/test/parallel/test-http-request-end.js @@ -44,7 +44,7 @@ const server = http.Server(function(req, res) { }); server.listen(0, function() { - http.request({ + const req = http.request({ port: this.address().port, path: '/', method: 'POST' @@ -54,5 +54,9 @@ server.listen(0, function() { }).on('error', function(e) { console.log(e.message); process.exit(1); - }).end(expected); + }); + + const result = req.end(expected); + + assert.strictEqual(req, result); });