diff --git a/doc/api/http.md b/doc/api/http.md index a6ab067ce5c9f6..4a7b2e82778779 100644 --- a/doc/api/http.md +++ b/doc/api/http.md @@ -1833,9 +1833,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 e58bd6ddae213b..d4431256646573 100644 --- a/lib/_http_incoming.js +++ b/lib/_http_incoming.js @@ -114,6 +114,7 @@ IncomingMessage.prototype._read = function _read(n) { IncomingMessage.prototype.destroy = function destroy(error) { 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);