http: OutgoingMessage.end() should return this

PR-URL: https://github.com/nodejs/node/pull/18780
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Matteo Collina 2018-02-14 12:32:01 +00:00
parent f6721c20df
commit 8118da7430
4 changed files with 21 additions and 8 deletions

View File

@ -544,11 +544,16 @@ See [`request.socket`][]
### request.end([data[, encoding]][, callback]) ### request.end([data[, encoding]][, callback])
<!-- YAML <!-- YAML
added: v0.1.90 added: v0.1.90
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18780
description: This method now returns a reference to `ClientRequest`.
--> -->
* `data` {string|Buffer} * `data` {string|Buffer}
* `encoding` {string} * `encoding` {string}
* `callback` {Function} * `callback` {Function}
* Returns: {this}
Finishes sending the request. If any parts of the body are Finishes sending the request. If any parts of the body are
unsent, it will flush them to the stream. If the request is unsent, it will flush them to the stream. If the request is
@ -1041,11 +1046,16 @@ See [`response.socket`][].
### response.end([data][, encoding][, callback]) ### response.end([data][, encoding][, callback])
<!-- YAML <!-- YAML
added: v0.1.90 added: v0.1.90
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/18780
description: This method now returns a reference to `ServerResponse`.
--> -->
* `data` {string|Buffer} * `data` {string|Buffer}
* `encoding` {string} * `encoding` {string}
* `callback` {Function} * `callback` {Function}
* Returns: {this}
This method signals to the server that all of the response headers and body This method signals to the server that all of the response headers and body
have been sent; that server should consider this message complete. have been sent; that server should consider this message complete.

View File

@ -736,7 +736,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
} }
if (this.finished) { if (this.finished) {
return false; return this;
} }
var uncork; var uncork;
@ -766,12 +766,11 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
var finish = onFinish.bind(undefined, this); var finish = onFinish.bind(undefined, this);
var ret;
if (this._hasBody && this.chunkedEncoding) { 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 { } else {
// Force a flush, HACK. // Force a flush, HACK.
ret = this._send('', 'latin1', finish); this._send('', 'latin1', finish);
} }
if (uncork) if (uncork)
@ -788,7 +787,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
this._finish(); this._finish();
} }
return ret; return this;
}; };

View File

@ -31,7 +31,7 @@ const server = http.Server(function(req, res) {
server.listen(0, function() { server.listen(0, function() {
const req = http.get({ port: this.address().port }, function(res) { const req = http.get({ port: this.address().port }, function(res) {
res.on('end', function() { res.on('end', function() {
assert.ok(!req.end()); assert.strictEqual(req.end(), req);
server.close(); server.close();
}); });
res.resume(); res.resume();

View File

@ -44,7 +44,7 @@ const server = http.Server(function(req, res) {
}); });
server.listen(0, function() { server.listen(0, function() {
http.request({ const req = http.request({
port: this.address().port, port: this.address().port,
path: '/', path: '/',
method: 'POST' method: 'POST'
@ -54,5 +54,9 @@ server.listen(0, function() {
}).on('error', function(e) { }).on('error', function(e) {
console.log(e.message); console.log(e.message);
process.exit(1); process.exit(1);
}).end(expected); });
const result = req.end(expected);
assert.strictEqual(req, result);
}); });