mirror of
https://github.com/nodejs/node.git
synced 2025-05-02 16:22:29 +00:00

Extensive re-work of http1 compatibility layer based on tests in express, on-finished and finalhandler. Fix handling of HEAD method to match http1. Adjust write, end, etc. to call writeHead as in http1 and as expected by user-land modules. Add socket proxy that instead uses the Http2Stream for the vast majority of socket interactions. Add and change tests to closer represent http1 behaviour. Refs: https://github.com/nodejs/node/pull/15633 Refs: https://github.com/expressjs/express/tree/master/test Refs: https://github.com/jshttp/on-finished/blob/master/test/test.js Refs: https://github.com/pillarjs/finalhandler/blob/master/test/test.js PR-URL: https://github.com/nodejs/node/pull/15702 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
'use strict';
|
|
|
|
const common = require('../common');
|
|
if (!common.hasCrypto)
|
|
common.skip('missing crypto');
|
|
const assert = require('assert');
|
|
const http2 = require('http2');
|
|
|
|
const {
|
|
HTTP2_HEADER_METHOD,
|
|
HTTP2_HEADER_PATH,
|
|
HTTP2_METHOD_POST,
|
|
NGHTTP2_CANCEL,
|
|
NGHTTP2_NO_ERROR,
|
|
NGHTTP2_PROTOCOL_ERROR,
|
|
NGHTTP2_REFUSED_STREAM,
|
|
NGHTTP2_INTERNAL_ERROR
|
|
} = http2.constants;
|
|
|
|
const errCheck = common.expectsError({ code: 'ERR_HTTP2_STREAM_ERROR' }, 6);
|
|
|
|
function checkRstCode(rstMethod, expectRstCode) {
|
|
const server = http2.createServer();
|
|
server.on('stream', (stream, headers, flags) => {
|
|
stream.respond({
|
|
'content-type': 'text/html',
|
|
':status': 200
|
|
});
|
|
stream.write('test');
|
|
if (rstMethod === 'rstStream')
|
|
stream[rstMethod](expectRstCode);
|
|
else
|
|
stream[rstMethod]();
|
|
|
|
if (expectRstCode !== NGHTTP2_NO_ERROR &&
|
|
expectRstCode !== NGHTTP2_CANCEL) {
|
|
stream.on('error', common.mustCall(errCheck));
|
|
} else {
|
|
stream.on('error', common.mustNotCall());
|
|
}
|
|
});
|
|
|
|
server.listen(0, common.mustCall(() => {
|
|
const port = server.address().port;
|
|
const client = http2.connect(`http://localhost:${port}`);
|
|
|
|
const headers = {
|
|
[HTTP2_HEADER_PATH]: '/',
|
|
[HTTP2_HEADER_METHOD]: HTTP2_METHOD_POST
|
|
};
|
|
const req = client.request(headers);
|
|
|
|
req.setEncoding('utf8');
|
|
req.on('streamClosed', common.mustCall((actualRstCode) => {
|
|
assert.strictEqual(
|
|
expectRstCode, actualRstCode, `${rstMethod} is not match rstCode`);
|
|
server.close();
|
|
client.destroy();
|
|
}));
|
|
req.on('data', common.mustCall());
|
|
req.on('aborted', common.mustCall());
|
|
req.on('end', common.mustCall());
|
|
|
|
if (expectRstCode !== NGHTTP2_NO_ERROR &&
|
|
expectRstCode !== NGHTTP2_CANCEL) {
|
|
req.on('error', common.mustCall(errCheck));
|
|
} else {
|
|
req.on('error', common.mustNotCall());
|
|
}
|
|
|
|
}));
|
|
}
|
|
|
|
checkRstCode('rstStream', NGHTTP2_NO_ERROR);
|
|
checkRstCode('rstWithNoError', NGHTTP2_NO_ERROR);
|
|
checkRstCode('rstWithProtocolError', NGHTTP2_PROTOCOL_ERROR);
|
|
checkRstCode('rstWithCancel', NGHTTP2_CANCEL);
|
|
checkRstCode('rstWithRefuse', NGHTTP2_REFUSED_STREAM);
|
|
checkRstCode('rstWithInternalError', NGHTTP2_INTERNAL_ERROR);
|