Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
http: OutgoingMessage.end() should return this
  • Loading branch information
mcollina committed Feb 16, 2018
commit 6dfd96e44bf1bd9097b8b4fbfe16dff1081cd4d2
10 changes: 10 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -513,11 +513,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 @@ -1010,11 +1015,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);
});