-
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.
http2: implement capture rection for 'request' and 'stream' events
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
197 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,152 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
if (!common.hasCrypto) | ||
common.skip('missing crypto'); | ||
|
||
const assert = require('assert'); | ||
const events = require('events'); | ||
const { createServer, connect } = require('http2'); | ||
|
||
events.captureRejections = true; | ||
|
||
{ | ||
// Test error thrown in the server 'stream' event, | ||
// after a respond() | ||
|
||
const server = createServer(); | ||
server.on('stream', common.mustCall(async (stream) => { | ||
server.close(); | ||
|
||
stream.respond({ ':status': 200 }); | ||
|
||
const _err = new Error('kaboom'); | ||
stream.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err, _err); | ||
})); | ||
throw _err; | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const { port } = server.address(); | ||
const session = connect(`http://localhost:${port}`); | ||
|
||
const req = session.request(); | ||
|
||
req.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR'); | ||
})); | ||
|
||
req.on('close', common.mustCall(() => { | ||
session.close(); | ||
})); | ||
})); | ||
} | ||
|
||
{ | ||
// Test error thrown in the server 'stream' event, | ||
// before a respond(). | ||
|
||
const server = createServer(); | ||
server.on('stream', common.mustCall(async (stream) => { | ||
server.close(); | ||
|
||
stream.on('error', common.mustNotCall()); | ||
|
||
throw new Error('kaboom'); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const { port } = server.address(); | ||
const session = connect(`http://localhost:${port}`); | ||
|
||
const req = session.request(); | ||
|
||
req.on('response', common.mustCall((headers) => { | ||
assert.strictEqual(headers[':status'], 500); | ||
})); | ||
|
||
req.on('close', common.mustCall(() => { | ||
session.close(); | ||
})); | ||
})); | ||
} | ||
|
||
|
||
{ | ||
// Test error thrown in 'request' event | ||
|
||
const server = createServer(common.mustCall(async (req, res) => { | ||
server.close(); | ||
res.setHeader('content-type', 'application/json'); | ||
const _err = new Error('kaboom'); | ||
throw _err; | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const { port } = server.address(); | ||
const session = connect(`http://localhost:${port}`); | ||
|
||
const req = session.request(); | ||
|
||
req.on('response', common.mustCall((headers) => { | ||
assert.strictEqual(headers[':status'], 500); | ||
assert.strictEqual(Object.hasOwnProperty.call(headers, 'content-type'), | ||
false); | ||
})); | ||
|
||
req.on('close', common.mustCall(() => { | ||
session.close(); | ||
})); | ||
|
||
req.resume(); | ||
})); | ||
} | ||
|
||
{ | ||
// Test error thrown in the client 'stream' event | ||
|
||
const server = createServer(); | ||
server.on('stream', common.mustCall(async (stream) => { | ||
const { port } = server.address(); | ||
|
||
server.close(); | ||
|
||
stream.pushStream({ | ||
':scheme': 'http', | ||
':path': '/foobar', | ||
':authority': `localhost:${port}`, | ||
}, common.mustCall((err, push) => { | ||
push.respond({ | ||
'content-type': 'text/html', | ||
':status': 200 | ||
}); | ||
push.end('pushed by the server'); | ||
|
||
stream.end('test'); | ||
})); | ||
|
||
stream.respond({ | ||
':status': 200 | ||
}); | ||
})); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const { port } = server.address(); | ||
const session = connect(`http://localhost:${port}`); | ||
|
||
const req = session.request(); | ||
|
||
session.on('stream', common.mustCall(async (stream) => { | ||
session.close(); | ||
|
||
const _err = new Error('kaboom'); | ||
stream.on('error', common.mustCall((err) => { | ||
assert.strictEqual(err, _err); | ||
})); | ||
throw _err; | ||
})); | ||
|
||
req.end(); | ||
})); | ||
} |