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); });