2017-09-30 14:06:21 +00:00
|
|
|
'use strict';
|
2017-12-11 05:56:41 +00:00
|
|
|
// Flags: --expose-internals
|
2017-09-30 14:06:21 +00:00
|
|
|
|
|
|
|
const common = require('../common');
|
|
|
|
if (!common.hasCrypto)
|
|
|
|
common.skip('missing crypto');
|
|
|
|
const http2 = require('http2');
|
2018-08-14 23:01:54 +00:00
|
|
|
const { internalBinding } = require('internal/test/binding');
|
2017-09-30 14:06:21 +00:00
|
|
|
const {
|
|
|
|
constants,
|
2017-11-15 18:55:31 +00:00
|
|
|
Http2Stream,
|
2017-09-30 14:06:21 +00:00
|
|
|
nghttp2ErrorString
|
2018-08-14 23:01:54 +00:00
|
|
|
} = internalBinding('http2');
|
2017-12-11 05:56:41 +00:00
|
|
|
const { NghttpError } = require('internal/http2/util');
|
2017-09-30 14:06:21 +00:00
|
|
|
|
2019-01-21 00:22:27 +00:00
|
|
|
// Tests error handling within additionalHeaders
|
2017-09-30 14:06:21 +00:00
|
|
|
// - every other NGHTTP2 error from binding (should emit stream error)
|
|
|
|
|
2017-11-15 18:55:31 +00:00
|
|
|
const specificTestKeys = [];
|
|
|
|
const specificTests = [];
|
2017-09-30 14:06:21 +00:00
|
|
|
|
|
|
|
const genericTests = Object.getOwnPropertyNames(constants)
|
|
|
|
.filter((key) => (
|
|
|
|
key.indexOf('NGHTTP2_ERR') === 0 && specificTestKeys.indexOf(key) < 0
|
|
|
|
))
|
|
|
|
.map((key) => ({
|
|
|
|
ngError: constants[key],
|
|
|
|
error: {
|
|
|
|
code: 'ERR_HTTP2_ERROR',
|
2019-12-25 17:02:16 +00:00
|
|
|
constructor: NghttpError,
|
2019-03-16 11:09:14 +00:00
|
|
|
name: 'Error',
|
2017-09-30 14:06:21 +00:00
|
|
|
message: nghttp2ErrorString(constants[key])
|
|
|
|
},
|
|
|
|
type: 'stream'
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
|
|
const tests = specificTests.concat(genericTests);
|
|
|
|
|
|
|
|
let currentError;
|
|
|
|
|
2018-12-03 16:15:45 +00:00
|
|
|
// Mock sendHeaders because we only care about testing error handling
|
2017-11-15 18:55:31 +00:00
|
|
|
Http2Stream.prototype.info = () => currentError.ngError;
|
2017-09-30 14:06:21 +00:00
|
|
|
|
|
|
|
const server = http2.createServer();
|
|
|
|
server.on('stream', common.mustCall((stream, headers) => {
|
|
|
|
const errorMustCall = common.expectsError(currentError.error);
|
|
|
|
const errorMustNotCall = common.mustNotCall(
|
|
|
|
`${currentError.error.code} should emit on ${currentError.type}`
|
|
|
|
);
|
|
|
|
|
|
|
|
if (currentError.type === 'stream') {
|
|
|
|
stream.session.on('error', errorMustNotCall);
|
|
|
|
stream.on('error', errorMustCall);
|
|
|
|
} else {
|
|
|
|
stream.session.once('error', errorMustCall);
|
|
|
|
stream.on('error', errorMustNotCall);
|
|
|
|
}
|
|
|
|
|
|
|
|
stream.additionalHeaders({ ':status': 100 });
|
|
|
|
}, tests.length));
|
|
|
|
|
|
|
|
server.listen(0, common.mustCall(() => runTest(tests.shift())));
|
|
|
|
|
|
|
|
function runTest(test) {
|
2017-12-12 19:34:17 +00:00
|
|
|
const client = http2.connect(`http://localhost:${server.address().port}`);
|
|
|
|
const req = client.request({ ':method': 'POST' });
|
2017-09-30 14:06:21 +00:00
|
|
|
|
|
|
|
currentError = test;
|
|
|
|
req.resume();
|
|
|
|
req.end();
|
|
|
|
|
2017-12-12 19:34:17 +00:00
|
|
|
req.on('error', common.expectsError({
|
|
|
|
code: 'ERR_HTTP2_STREAM_ERROR',
|
2019-12-25 17:02:16 +00:00
|
|
|
name: 'Error',
|
2018-02-23 21:28:29 +00:00
|
|
|
message: 'Stream closed with error code NGHTTP2_INTERNAL_ERROR'
|
2017-12-12 19:34:17 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
req.on('close', common.mustCall(() => {
|
|
|
|
client.close();
|
2017-09-30 14:06:21 +00:00
|
|
|
|
|
|
|
if (!tests.length) {
|
|
|
|
server.close();
|
|
|
|
} else {
|
|
|
|
runTest(tests.shift());
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|