From f37e858555b3918f258b49f9c953211c7f5f335f Mon Sep 17 00:00:00 2001 From: Develop-KIM Date: Thu, 9 Jul 2026 12:07:30 +0900 Subject: [PATCH] test: add unexpected disconnect guards to http2 tests Add the disconnect guard pattern from docs/best-practices/writing-tests.md to the happy-path HTTP/2 test files so silent reconnects fail the tests. Refs: #251 Co-Authored-By: Claude Fable 5 --- test/http2-connection.js | 30 ++++++++++++++++++++++++++++++ test/http2-continue.js | 12 ++++++++++++ test/http2-pipelining-default.js | 18 ++++++++++++++++++ test/http2-pseudo-headers.js | 12 ++++++++++++ 4 files changed, 72 insertions(+) diff --git a/test/http2-connection.js b/test/http2-connection.js index a928c52cdcf..67ba62bbdb8 100644 --- a/test/http2-connection.js +++ b/test/http2-connection.js @@ -44,6 +44,12 @@ test('Should support H2 connection', async t => { }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const response = await client.request({ path: '/', method: 'GET', @@ -98,6 +104,12 @@ test('Should support H2 connection(multiple requests)', async t => { }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + for (let i = 0; i < 3; i++) { const sendBody = `seq ${i}` const body = [] @@ -156,6 +168,12 @@ test('Should support H2 connection (headers as array)', async t => { }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const response = await client.request({ path: '/', method: 'GET', @@ -215,6 +233,12 @@ test('Should support multiple header values with semicolon separator', async t = }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const response = await client.request({ path: '/', method: 'GET', @@ -298,6 +322,12 @@ test('Should support H2 connection(POST Buffer)', async t => { }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const sendBody = 'hello!' const body = [] const response = await client.request({ diff --git a/test/http2-continue.js b/test/http2-continue.js index 15252d0051c..b2ef2105b0b 100644 --- a/test/http2-continue.js +++ b/test/http2-continue.js @@ -43,6 +43,12 @@ test('Should handle h2 continue', async t => { }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const response = await client.request({ path: '/', method: 'POST', @@ -92,6 +98,12 @@ test('Should deliver an early final response to an Expect: 100-continue request }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const response = await client.request({ path: '/', method: 'POST', diff --git a/test/http2-pipelining-default.js b/test/http2-pipelining-default.js index 54d5e585b8c..38eeb083342 100644 --- a/test/http2-pipelining-default.js +++ b/test/http2-pipelining-default.js @@ -36,6 +36,12 @@ test('h2 client multiplexes concurrent requests by default (#4143)', async t => }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const results = await Promise.all( Array.from({ length: N }, () => client.request({ path: '/', method: 'GET' }) @@ -96,6 +102,12 @@ test('Pool with connections=1 multiplexes h2 streams on the single session (#414 }) after(() => pool.close()) + pool.on('disconnect', () => { + if (!pool.closed && !pool.destroyed) { + t.fail('unexpected disconnect') + } + }) + const results = await Promise.all( Array.from({ length: N }, () => pool.request({ path: '/', method: 'GET' }) @@ -130,6 +142,12 @@ test('Client#pipelining keeps its h1 (RFC7230) semantic on an h2 client', async }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + // Even after negotiating h2, client.pipelining reflects only the user-set // h1 pipelining factor — the h2 dispatch limit is maxConcurrentStreams. const r = await client.request({ path: '/', method: 'GET' }) diff --git a/test/http2-pseudo-headers.js b/test/http2-pseudo-headers.js index ac23a9dd077..8e4c763d119 100644 --- a/test/http2-pseudo-headers.js +++ b/test/http2-pseudo-headers.js @@ -44,6 +44,12 @@ test('Should provide pseudo-headers in proper order', async t => { }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const response = await client.request({ path: '/', method: 'GET' @@ -79,6 +85,12 @@ test('The h2 pseudo-headers is not included in the headers', async t => { }) after(() => client.close()) + client.on('disconnect', () => { + if (!client.closed && !client.destroyed) { + t.fail('unexpected disconnect') + } + }) + const response = await client.request({ path: '/', method: 'GET'