diff --git a/doc/api/http.md b/doc/api/http.md index 97bfc36233b0bc..75c5292ab9127a 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1875,9 +1875,15 @@ const req = http.request({ ### `message.destroy([error])` * `error` {Error} +* Returns: {this} Calls `destroy()` on the socket that received the `IncomingMessage`. If `error` is provided, an `'error'` event is emitted on the socket and `error` is passed diff --git a/lib/_http_incoming.js b/lib/_http_incoming.js index 02b88dfe87ca85..59136833c6b340 100644 --- a/lib/_http_incoming.js +++ b/lib/_http_incoming.js @@ -123,6 +123,7 @@ IncomingMessage.prototype.destroy = function destroy(error) { this.destroyed = true; if (this.socket) this.socket.destroy(error); + return this; }; diff --git a/test/parallel/test-http-incoming-message-destroy.js b/test/parallel/test-http-incoming-message-destroy.js new file mode 100644 index 00000000000000..4241ec8e7d85ef --- /dev/null +++ b/test/parallel/test-http-incoming-message-destroy.js @@ -0,0 +1,10 @@ +'use strict'; + +// Test that http.IncomingMessage,prototype.destroy() returns `this`. +require('../common'); + +const assert = require('assert'); +const http = require('http'); +const incomingMessage = new http.IncomingMessage(); + +assert.strictEqual(incomingMessage.destroy(), incomingMessage);