-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: implement capture rejections for 'request' event
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
129 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
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,108 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const events = require('events'); | ||
const { createServer, request } = require('http'); | ||
const assert = require('assert'); | ||
|
||
events.captureRejections = true; | ||
|
||
{ | ||
const server = createServer(common.mustCall(async (req, res) => { | ||
// We will test that this header is cleaned up before forwarding. | ||
res.setHeader('content-type', 'application/json'); | ||
throw new Error('kaboom'); | ||
})); | ||
|
||
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) => { | ||
assert.strictEqual(res.statusCode, 500); | ||
assert.strictEqual(res.headers.hasOwnProperty('content-type'), false); | ||
let data = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', common.mustCall((chunk) => { | ||
data += chunk; | ||
})); | ||
res.on('end', common.mustCall(() => { | ||
assert.strictEqual(data, 'Internal Server Error'); | ||
server.close(); | ||
})); | ||
})); | ||
})); | ||
} | ||
|
||
{ | ||
let resolve; | ||
const latch = new Promise((_resolve) => { | ||
resolve = _resolve; | ||
}); | ||
const server = createServer(common.mustCall(async (req, res) => { | ||
server.close(); | ||
|
||
// We will test that this header is cleaned up before forwarding. | ||
res.setHeader('content-type', 'application/json'); | ||
res.write('{'); | ||
req.resume(); | ||
|
||
// Wait so the data is on the wire | ||
await latch; | ||
|
||
throw new Error('kaboom'); | ||
})); | ||
|
||
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) => { | ||
assert.strictEqual(res.statusCode, 200); | ||
assert.strictEqual(res.headers['content-type'], 'application/json'); | ||
resolve(); | ||
|
||
let data = ''; | ||
res.setEncoding('utf8'); | ||
res.on('data', common.mustCall((chunk) => { | ||
data += chunk; | ||
})); | ||
|
||
req.on('close', common.mustCall(() => { | ||
assert.strictEqual(data, '{'); | ||
})); | ||
})); | ||
})); | ||
} | ||
|
||
{ | ||
const server = createServer(common.mustCall(async (req, res) => { | ||
// We will test that this header is cleaned up before forwarding. | ||
res.writeHead(200); | ||
throw new Error('kaboom'); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const req = request({ | ||
method: 'GET', | ||
host: server.address().host, | ||
port: server.address().port | ||
}); | ||
|
||
req.end(); | ||
req.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.code, 'ECONNRESET'); | ||
server.close(); | ||
})); | ||
})); | ||
} |