Files
node/test/parallel/test-quic-h3-error-codes.mjs
Efe Karasakal b6a692d590 quic: handle application-specific no-error codes
Signed-off-by: Efe Karasakal <hi@efe.dev>
PR-URL: https://github.com/nodejs/node/pull/67094
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Tim Perry <pimterry@gmail.com>
2026-11-25 17:59:17 +01:00

205 lines
5.7 KiB
JavaScript

// Flags: --experimental-quic --experimental-stream-iter --no-warnings
// Test: HTTP/3 error code handling.
// H3 application error codes are propagated correctly
// Graceful close uses H3_NO_ERROR - streams complete cleanly
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 { bytes } = await import('stream/iter');
const key = createPrivateKey(fixtures.readKey('agent1-key.pem'));
const cert = fixtures.readKey('agent1-cert.pem');
const decoder = new TextDecoder();
// Server closes with explicit application error code.
// Client's session closed rejects with the error.
{
const serverDone = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(async (ss) => {
ss.onstream = mustCall(async (stream) => {
await stream.closed;
// Close with an explicit H3 application error code.
ss.close({ code: 0x101, type: 'application' });
serverDone.resolve();
});
}), {
sni: { '*': { keys: [key], certs: [cert] } },
onheaders: mustCall(function(headers) {
this.sendHeaders({ ':status': '200' });
this.writer.writeSync('ok');
this.writer.endSync();
}),
});
const clientSession = await connect(serverEndpoint.address, {
servername: 'localhost',
verifyPeer: 'manual',
});
await clientSession.opened;
const stream = await clientSession.createBidirectionalStream({
headers: {
':method': 'GET',
':path': '/',
':scheme': 'https',
':authority': 'localhost',
},
onheaders: mustCall(function(headers) {
assert.strictEqual(headers[':status'], 200);
}),
});
const body = await bytes(stream);
assert.strictEqual(decoder.decode(body), 'ok');
await Promise.all([stream.closed, serverDone.promise]);
// Client sees the application error code.
await assert.rejects(clientSession.closed, {
code: 'ERR_QUIC_APPLICATION_ERROR',
});
await serverEndpoint.close();
}
// Graceful close with no explicit error code.
// Both streams complete normally. The close uses H3_NO_ERROR
// which is treated as a clean shutdown (not an error).
{
const serverDone = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(async (ss) => {
ss.onstream = mustCall(async (stream) => {
await stream.closed;
ss.close();
serverDone.resolve();
});
}), {
sni: { '*': { keys: [key], certs: [cert] } },
onheaders: mustCall(function(headers) {
this.sendHeaders({ ':status': '200' });
this.writer.writeSync('ok');
this.writer.endSync();
}),
});
const clientSession = await connect(serverEndpoint.address, {
servername: 'localhost',
verifyPeer: 'manual',
});
await clientSession.opened;
const stream = await clientSession.createBidirectionalStream({
headers: {
':method': 'GET',
':path': '/',
':scheme': 'https',
':authority': 'localhost',
},
onheaders: mustCall(function(headers) {
assert.strictEqual(headers[':status'], 200);
}),
});
const body = await bytes(stream);
assert.strictEqual(decoder.decode(body), 'ok');
await stream.closed;
// Graceful close - session close promise resolves
// because H3_NO_ERROR is a clean close.
await serverDone.promise;
await clientSession.close();
await serverEndpoint.close();
}
// RESET_STREAM with H3_NO_ERROR is a clean application-level reset. It does
// not reject stream.closed, and truncatedReads: 'ignore' accepts the missing
// FIN without turning the nonzero code back into an error.
{
const H3_NO_ERROR = 0x100n;
const reset = Promise.withResolvers();
const serverEndpoint = await listen(mustCall(), {
sni: { '*': { keys: [key], certs: [cert] } },
onheaders: mustCall(function() {
this.resetStream(H3_NO_ERROR);
}),
});
const clientSession = await connect(serverEndpoint.address, {
servername: 'localhost',
verifyPeer: 'manual',
truncatedReads: 'ignore',
});
await clientSession.opened;
const stream = await clientSession.createBidirectionalStream({
headers: {
':method': 'GET',
':path': '/',
':scheme': 'https',
':authority': 'localhost',
},
});
stream.onreset = mustCall((error) => {
assert.strictEqual(error.code, 'ERR_QUIC_APPLICATION_ERROR');
assert.strictEqual(error.errorCode, H3_NO_ERROR);
reset.resolve();
});
stream.writer.endSync();
const body = await bytes(stream);
assert.strictEqual(body.byteLength, 0);
await reset.promise;
await stream.closed;
await clientSession.close();
await serverEndpoint.close();
}
// A locally sent STOP_SENDING with H3_NO_ERROR is a clean application-level
// truncation. With truncatedReads: 'ignore', the nonzero code does not fail
// the read.
{
const H3_NO_ERROR = 0x100n;
const serverEndpoint = await listen(mustCall(), {
sni: { '*': { keys: [key], certs: [cert] } },
onheaders: mustCall(),
});
const clientSession = await connect(serverEndpoint.address, {
servername: 'localhost',
verifyPeer: 'manual',
truncatedReads: 'ignore',
});
await clientSession.opened;
const stream = await clientSession.createBidirectionalStream({
headers: {
':method': 'GET',
':path': '/',
':scheme': 'https',
':authority': 'localhost',
},
});
stream.writer.endSync();
stream.stopSending(H3_NO_ERROR);
const body = await bytes(stream);
assert.strictEqual(body.byteLength, 0);
await stream.closed;
await clientSession.close();
await serverEndpoint.close();
}