node/test/parallel/test-http2-allow-http1-upgrade-ws.js
Tim Perry 2a0fcdbc88
http2: fix allowHttp1+Upgrade, broken by shouldUpgradeCallback
This is required to use HTTP/1 websockets on an HTTP/2 server, which is
fairly common as websockets over HTTP/2 is much less widely supported.

This was broken by the recent shouldUpgradeCallback HTTP/1 addition,
which wasn't correctly added to the corresponding allowHttp1 part of
the HTTP/2 implementation.

PR-URL: https://github.com/nodejs/node/pull/59924
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
2025-09-20 18:18:23 +00:00

40 lines
1.1 KiB
JavaScript

// Flags: --expose-internals
'use strict';
const common = require('../common');
const fixtures = require('../common/fixtures');
if (!common.hasCrypto) common.skip('missing crypto');
const http2 = require('http2');
const undici = require('internal/deps/undici/undici');
const WebSocketServer = require('../common/websocket-server');
(async function main() {
const server = http2.createSecureServer({
key: fixtures.readKey('agent1-key.pem'),
cert: fixtures.readKey('agent1-cert.pem'),
allowHTTP1: true,
});
server.on('request', common.mustNotCall());
new WebSocketServer({ server }); // Handles websocket 'upgrade' events
await new Promise((resolve) => server.listen(0, resolve));
await new Promise((resolve, reject) => {
const ws = new WebSocket(`wss://localhost:${server.address().port}`, {
dispatcher: new undici.EnvHttpProxyAgent({
connect: { rejectUnauthorized: false }
})
});
ws.addEventListener('open', common.mustCall(() => {
ws.close();
resolve();
}));
ws.addEventListener('error', reject);
});
server.close();
})().then(common.mustCall());