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);