-
Notifications
You must be signed in to change notification settings - Fork 29.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add captureRejection support to OutgoingMessage
PR-URL: #27867 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Michaël Zasso <targos@protonmail.com>
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
test/parallel/test-http-outgoing-message-capture-rejection.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const events = require('events'); | ||
const { createServer, request } = require('http'); | ||
|
||
events.captureRejections = true; | ||
|
||
{ | ||
const server = createServer(common.mustCall((req, res) => { | ||
const _err = new Error('kaboom'); | ||
res.on('drain', common.mustCall(async () => { | ||
throw _err; | ||
})); | ||
|
||
res.socket.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err, _err); | ||
})); | ||
|
||
// Write until there is space in the buffer | ||
while (res.write('hello')) {} | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const req = request({ | ||
method: 'GET', | ||
host: server.address().host, | ||
port: server.address().port | ||
}); | ||
|
||
req.end(); | ||
|
||
req.on('response', common.mustCall((res) => { | ||
res.on('aborted', common.mustCall()); | ||
res.resume(); | ||
server.close(); | ||
})); | ||
})); | ||
} | ||
|
||
{ | ||
let _res; | ||
let shouldEnd = false; | ||
// Not using mustCall here, because it is OS-dependant. | ||
const server = createServer((req, res) => { | ||
// So that we cleanly stop | ||
_res = res; | ||
|
||
if (shouldEnd) { | ||
res.end(); | ||
} | ||
}); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const _err = new Error('kaboom'); | ||
|
||
const req = request({ | ||
method: 'POST', | ||
host: server.address().host, | ||
port: server.address().port | ||
}); | ||
|
||
req.on('response', common.mustNotCall((res) => { | ||
// So that we cleanly stop | ||
res.resume(); | ||
server.close(); | ||
})); | ||
|
||
req.on('error', common.mustCall((err) => { | ||
server.close(); | ||
// On some variants of Windows, this can happen before | ||
// the server has received the request. | ||
if (_res) { | ||
_res.end(); | ||
} else { | ||
shouldEnd = true; | ||
} | ||
assert.strictEqual(err, _err); | ||
})); | ||
|
||
req.on('drain', common.mustCall(async () => { | ||
throw _err; | ||
})); | ||
|
||
// Write until there is space in the buffer | ||
while (req.write('hello')) {} | ||
})); | ||
} |