From d87031def48c298f2c2dca7caa529efc6f898121 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Fri, 8 May 2020 21:25:55 -0400 Subject: [PATCH] http: return this from ClientRequest#destroy() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This commit updates ClientRequest#destroy() to return `this` for consistency with other writable streams. PR-URL: https://github.com/nodejs/node/pull/32789 Reviewed-By: Robert Nagy Reviewed-By: Anna Henningsen Reviewed-By: Michaƫl Zasso Reviewed-By: Ruben Bridgewater --- doc/api/http.md | 5 +++++ lib/_http_client.js | 4 +++- test/parallel/test-client-request-destroy.js | 13 +++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-client-request-destroy.js diff --git a/doc/api/http.md b/doc/api/http.md index 75c5292ab9127a..546ab616ef4468 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -646,6 +646,11 @@ is finished. ### `request.destroy([error])` * `error` {Error} Optional, an error to emit with `'error'` event. diff --git a/lib/_http_client.js b/lib/_http_client.js index 23e82c807fc6e0..5cafd5455bd717 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -349,7 +349,7 @@ ClientRequest.prototype.abort = function abort() { ClientRequest.prototype.destroy = function destroy(err) { if (this.destroyed) { - return; + return this; } this.destroyed = true; @@ -365,6 +365,8 @@ ClientRequest.prototype.destroy = function destroy(err) { } else if (err) { this[kError] = err; } + + return this; }; function _destroy(req, socket, err) { diff --git a/test/parallel/test-client-request-destroy.js b/test/parallel/test-client-request-destroy.js new file mode 100644 index 00000000000000..2f3efcf81204b3 --- /dev/null +++ b/test/parallel/test-client-request-destroy.js @@ -0,0 +1,13 @@ +'use strict'; + +// Test that http.ClientRequest,prototype.destroy() returns `this`. +require('../common'); + +const assert = require('assert'); +const http = require('http'); +const clientRequest = new http.ClientRequest({ createConnection: () => {} }); + +assert.strictEqual(clientRequest.destroyed, false); +assert.strictEqual(clientRequest.destroy(), clientRequest); +assert.strictEqual(clientRequest.destroyed, true); +assert.strictEqual(clientRequest.destroy(), clientRequest);