1'use strict'; 2 3const common = require('../common'); 4if (!common.hasCrypto) 5 common.skip('missing crypto'); 6 7const assert = require('assert'); 8const events = require('events'); 9const { createServer, connect } = require('http2'); 10 11events.captureRejections = true; 12 13{ 14 // Test error thrown in the server 'stream' event, 15 // after a respond() 16 17 const server = createServer(); 18 server.on('stream', common.mustCall(async (stream) => { 19 server.close(); 20 21 stream.respond({ ':status': 200 }); 22 23 const _err = new Error('kaboom'); 24 stream.on('error', common.mustCall((err) => { 25 assert.strictEqual(err, _err); 26 })); 27 throw _err; 28 })); 29 30 server.listen(0, common.mustCall(() => { 31 const { port } = server.address(); 32 const session = connect(`http://localhost:${port}`); 33 34 const req = session.request(); 35 36 req.on('error', common.mustCall((err) => { 37 assert.strictEqual(err.code, 'ERR_HTTP2_STREAM_ERROR'); 38 })); 39 40 req.on('close', common.mustCall(() => { 41 session.close(); 42 })); 43 })); 44} 45 46{ 47 // Test error thrown in the server 'stream' event, 48 // before a respond(). 49 50 const server = createServer(); 51 server.on('stream', common.mustCall(async (stream) => { 52 server.close(); 53 54 stream.on('error', common.mustNotCall()); 55 56 throw new Error('kaboom'); 57 })); 58 59 server.listen(0, common.mustCall(() => { 60 const { port } = server.address(); 61 const session = connect(`http://localhost:${port}`); 62 63 const req = session.request(); 64 65 req.on('response', common.mustCall((headers) => { 66 assert.strictEqual(headers[':status'], 500); 67 })); 68 69 req.on('close', common.mustCall(() => { 70 session.close(); 71 })); 72 })); 73} 74 75{ 76 // Test error thrown in 'request' event 77 78 const server = createServer(common.mustCall(async (req, res) => { 79 server.close(); 80 res.setHeader('content-type', 'application/json'); 81 const _err = new Error('kaboom'); 82 throw _err; 83 })); 84 85 server.listen(0, common.mustCall(() => { 86 const { port } = server.address(); 87 const session = connect(`http://localhost:${port}`); 88 89 const req = session.request(); 90 91 req.on('response', common.mustCall((headers) => { 92 assert.strictEqual(headers[':status'], 500); 93 assert.strictEqual(Object.hasOwn(headers, 'content-type'), false); 94 })); 95 96 req.on('close', common.mustCall(() => { 97 session.close(); 98 })); 99 100 req.resume(); 101 })); 102} 103 104{ 105 // Test error thrown in the client 'stream' event 106 107 const server = createServer(); 108 server.on('stream', common.mustCall(async (stream) => { 109 const { port } = server.address(); 110 111 server.close(); 112 113 stream.pushStream({ 114 ':scheme': 'http', 115 ':path': '/foobar', 116 ':authority': `localhost:${port}`, 117 }, common.mustCall((err, push) => { 118 push.respond({ 119 'content-type': 'text/html', 120 ':status': 200 121 }); 122 push.end('pushed by the server'); 123 124 stream.end('test'); 125 })); 126 127 stream.respond({ 128 ':status': 200 129 }); 130 })); 131 132 server.listen(0, common.mustCall(() => { 133 const { port } = server.address(); 134 const session = connect(`http://localhost:${port}`); 135 136 const req = session.request(); 137 req.resume(); 138 139 session.on('stream', common.mustCall(async (stream) => { 140 session.close(); 141 142 const _err = new Error('kaboom'); 143 stream.on('error', common.mustCall((err) => { 144 assert.strictEqual(err, _err); 145 })); 146 throw _err; 147 })); 148 149 req.end(); 150 })); 151} 152