Skip to content

Commit

Permalink
http: OutgoingMessage.end() should return this
Browse files Browse the repository at this point in the history
PR-URL: nodejs#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>
  • Loading branch information
mcollina authored and MayaLekova committed May 8, 2018
1 parent 35ee7c7 commit 39e12f1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
10 changes: 10 additions & 0 deletions doc/api/http.md
Expand Up @@ -544,11 +544,16 @@ See [`request.socket`][]
### request.end([data[, encoding]][, callback])
<!-- YAML
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}
* `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
Expand Down Expand Up @@ -1041,11 +1046,16 @@ See [`response.socket`][].
### response.end([data][, encoding][, callback])
<!-- YAML
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}
* `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.
Expand Down
9 changes: 4 additions & 5 deletions lib/_http_outgoing.js
Expand Up @@ -736,7 +736,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
}

if (this.finished) {
return false;
return this;
}

var uncork;
Expand Down Expand Up @@ -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)
Expand All @@ -788,7 +787,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
this._finish();
}

return ret;
return this;
};


Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-http-request-end-twice.js
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions test/parallel/test-http-request-end.js
Expand Up @@ -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'
Expand All @@ -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);
});

0 comments on commit 39e12f1

Please sign in to comment.