|
| 1 | +// Flags: --expose-http2 |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +if (!common.hasCrypto) |
| 6 | + common.skip('missing crypto'); |
| 7 | +const assert = require('assert'); |
| 8 | +const http2 = require('http2'); |
| 9 | + |
| 10 | +// Check that destroying the Http2ServerResponse stream produces |
| 11 | +// the expected result, including the ability to throw an error |
| 12 | +// which is emitted on server.streamError |
| 13 | + |
| 14 | +const errors = [ |
| 15 | + 'test-error', |
| 16 | + Error('test') |
| 17 | +]; |
| 18 | +let nextError; |
| 19 | + |
| 20 | +const server = http2.createServer(common.mustCall((req, res) => { |
| 21 | + req.on('error', common.mustNotCall()); |
| 22 | + res.on('error', common.mustNotCall()); |
| 23 | + |
| 24 | + res.on('finish', common.mustCall(() => { |
| 25 | + assert.doesNotThrow(() => res.destroy(nextError)); |
| 26 | + assert.strictEqual(res.closed, true); |
| 27 | + })); |
| 28 | + |
| 29 | + if (req.path !== '/') { |
| 30 | + nextError = errors.shift(); |
| 31 | + } |
| 32 | + res.destroy(nextError); |
| 33 | +}, 3)); |
| 34 | + |
| 35 | +server.on( |
| 36 | + 'streamError', |
| 37 | + common.mustCall((err) => assert.strictEqual(err, nextError), 2) |
| 38 | +); |
| 39 | + |
| 40 | +server.listen(0, common.mustCall(() => { |
| 41 | + const port = server.address().port; |
| 42 | + const client = http2.connect(`http://localhost:${port}`); |
| 43 | + const req = client.request({ |
| 44 | + ':path': '/', |
| 45 | + ':method': 'GET', |
| 46 | + ':scheme': 'http', |
| 47 | + ':authority': `localhost:${port}` |
| 48 | + }); |
| 49 | + |
| 50 | + req.on('response', common.mustNotCall()); |
| 51 | + req.on('error', common.mustNotCall()); |
| 52 | + req.on('end', common.mustCall()); |
| 53 | + |
| 54 | + req.resume(); |
| 55 | + req.end(); |
| 56 | + |
| 57 | + const req2 = client.request({ |
| 58 | + ':path': '/error', |
| 59 | + ':method': 'GET', |
| 60 | + ':scheme': 'http', |
| 61 | + ':authority': `localhost:${port}` |
| 62 | + }); |
| 63 | + |
| 64 | + req2.on('response', common.mustNotCall()); |
| 65 | + req2.on('error', common.mustNotCall()); |
| 66 | + req2.on('end', common.mustCall()); |
| 67 | + |
| 68 | + req2.resume(); |
| 69 | + req2.end(); |
| 70 | + |
| 71 | + const req3 = client.request({ |
| 72 | + ':path': '/error', |
| 73 | + ':method': 'GET', |
| 74 | + ':scheme': 'http', |
| 75 | + ':authority': `localhost:${port}` |
| 76 | + }); |
| 77 | + |
| 78 | + req3.on('response', common.mustNotCall()); |
| 79 | + req3.on('error', common.expectsError({ |
| 80 | + code: 'ERR_HTTP2_STREAM_ERROR', |
| 81 | + type: Error, |
| 82 | + message: 'Stream closed with error code 2' |
| 83 | + })); |
| 84 | + req3.on('end', common.mustCall(() => { |
| 85 | + server.close(); |
| 86 | + client.destroy(); |
| 87 | + })); |
| 88 | + |
| 89 | + req3.resume(); |
| 90 | + req3.end(); |
| 91 | +})); |
0 commit comments