Files
node/test/parallel/test-quic-new-token.mjs
2026-07-27 19:04:04 +02:00

55 lines
1.6 KiB
JavaScript

// Flags: --experimental-quic --no-warnings
import { hasQuic, skip, mustCall } from '../common/index.mjs';
import assert from 'node:assert';
import * as fixtures from '../common/fixtures.mjs';
if (!hasQuic) {
skip('QUIC is not enabled');
}
const { listen, connect } = await import('node:quic');
const { createPrivateKey } = await import('node:crypto');
const key = createPrivateKey(fixtures.readKey('agent1-key.pem'));
const cert = fixtures.readKey('agent1-cert.pem');
// The token option must be an ArrayBufferView if provided
await assert.rejects(connect({ port: 1234 }, {
alpn: 'quic-test',
token: 'not-a-buffer',
}), {
code: 'ERR_INVALID_ARG_TYPE',
});
// After a successful handshake, the server automatically sends a
// NEW_TOKEN frame. The client should receive it via the onnewtoken
// callback set at connection time.
const clientToken = Promise.withResolvers();
const serverEndpoint = await listen(mustCall((serverSession) => {
serverSession.opened.then(mustCall());
}), {
sni: { '*': { keys: [key], certs: [cert] } },
alpn: ['quic-test'],
});
const clientSession = await connect(serverEndpoint.address, {
alpn: 'quic-test',
verifyPeer: 'manual',
servername: 'localhost',
// Set onnewtoken at connection time to avoid missing the event.
onnewtoken: mustCall(function(token, address) {
assert.ok(Buffer.isBuffer(token), 'token should be a Buffer');
assert.ok(token.length > 0, 'token should not be empty');
assert.ok(address !== undefined, 'address should be defined');
clientToken.resolve();
}),
});
await Promise.all([clientSession.opened, clientToken.promise]);
await clientSession.close();
await serverEndpoint.close();