|
| 1 | +// Flags: --expose-http2 |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +if (!common.hasCrypto) |
| 6 | + common.skip('missing crypto'); |
| 7 | +const http2 = require('http2'); |
| 8 | +const { |
| 9 | + constants, |
| 10 | + Http2Session, |
| 11 | + nghttp2ErrorString |
| 12 | +} = process.binding('http2'); |
| 13 | + |
| 14 | +// tests error handling within additionalHeaders |
| 15 | +// - NGHTTP2_ERR_NOMEM (should emit session error) |
| 16 | +// - every other NGHTTP2 error from binding (should emit stream error) |
| 17 | + |
| 18 | +const specificTestKeys = [ |
| 19 | + 'NGHTTP2_ERR_NOMEM' |
| 20 | +]; |
| 21 | + |
| 22 | +const specificTests = [ |
| 23 | + { |
| 24 | + ngError: constants.NGHTTP2_ERR_NOMEM, |
| 25 | + error: { |
| 26 | + code: 'ERR_OUTOFMEMORY', |
| 27 | + type: Error, |
| 28 | + message: 'Out of memory' |
| 29 | + }, |
| 30 | + type: 'session' |
| 31 | + } |
| 32 | +]; |
| 33 | + |
| 34 | +const genericTests = Object.getOwnPropertyNames(constants) |
| 35 | + .filter((key) => ( |
| 36 | + key.indexOf('NGHTTP2_ERR') === 0 && specificTestKeys.indexOf(key) < 0 |
| 37 | + )) |
| 38 | + .map((key) => ({ |
| 39 | + ngError: constants[key], |
| 40 | + error: { |
| 41 | + code: 'ERR_HTTP2_ERROR', |
| 42 | + type: Error, |
| 43 | + message: nghttp2ErrorString(constants[key]) |
| 44 | + }, |
| 45 | + type: 'stream' |
| 46 | + })); |
| 47 | + |
| 48 | + |
| 49 | +const tests = specificTests.concat(genericTests); |
| 50 | + |
| 51 | +let currentError; |
| 52 | + |
| 53 | +// mock sendHeaders because we only care about testing error handling |
| 54 | +Http2Session.prototype.sendHeaders = () => currentError.ngError; |
| 55 | + |
| 56 | +const server = http2.createServer(); |
| 57 | +server.on('stream', common.mustCall((stream, headers) => { |
| 58 | + const errorMustCall = common.expectsError(currentError.error); |
| 59 | + const errorMustNotCall = common.mustNotCall( |
| 60 | + `${currentError.error.code} should emit on ${currentError.type}` |
| 61 | + ); |
| 62 | + |
| 63 | + if (currentError.type === 'stream') { |
| 64 | + stream.session.on('error', errorMustNotCall); |
| 65 | + stream.on('error', errorMustCall); |
| 66 | + stream.on('error', common.mustCall(() => { |
| 67 | + stream.respond(); |
| 68 | + stream.end(); |
| 69 | + })); |
| 70 | + } else { |
| 71 | + stream.session.once('error', errorMustCall); |
| 72 | + stream.on('error', errorMustNotCall); |
| 73 | + } |
| 74 | + |
| 75 | + stream.additionalHeaders({ ':status': 100 }); |
| 76 | +}, tests.length)); |
| 77 | + |
| 78 | +server.listen(0, common.mustCall(() => runTest(tests.shift()))); |
| 79 | + |
| 80 | +function runTest(test) { |
| 81 | + const port = server.address().port; |
| 82 | + const url = `http://localhost:${port}`; |
| 83 | + const headers = { |
| 84 | + ':path': '/', |
| 85 | + ':method': 'POST', |
| 86 | + ':scheme': 'http', |
| 87 | + ':authority': `localhost:${port}` |
| 88 | + }; |
| 89 | + |
| 90 | + const client = http2.connect(url); |
| 91 | + const req = client.request(headers); |
| 92 | + |
| 93 | + currentError = test; |
| 94 | + req.resume(); |
| 95 | + req.end(); |
| 96 | + |
| 97 | + req.on('end', common.mustCall(() => { |
| 98 | + client.destroy(); |
| 99 | + |
| 100 | + if (!tests.length) { |
| 101 | + server.close(); |
| 102 | + } else { |
| 103 | + runTest(tests.shift()); |
| 104 | + } |
| 105 | + })); |
| 106 | +} |
0 commit comments