Skip to content

Commit

Permalink
http: OutgoingMessage.end() should return this
Browse files Browse the repository at this point in the history
PR-URL: #18780
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
  • Loading branch information
mcollina committed Feb 19, 2018
1 parent f6721c2 commit 8118da7
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 8118da7

Please sign in to comment.