Skip to content

Commit 5908018

Browse files
authored
test: add unexpected disconnect guards to remaining http2 tests (#5686)
1 parent 721cd23 commit 5908018

4 files changed

Lines changed: 89 additions & 7 deletions

File tree

‎test/http2-agent.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ test('Agent should support H2 connection', async t => {
3737
})
3838
after(() => client.close())
3939

40+
client.on('disconnect', () => {
41+
if (!client.closed && !client.destroyed) {
42+
t.fail('unexpected disconnect')
43+
}
44+
})
45+
4046
const response = await client.request({
4147
origin: `https://localhost:${server.address().port}`,
4248
path: '/',

‎test/http2-alpn.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ test('Should upgrade to HTTP/2 when HTTPS/1 is available for GET', async (t) =>
6363
// close the client on teardown
6464
after(() => client.close())
6565

66+
client.on('disconnect', () => {
67+
if (!client.closed && !client.destroyed) {
68+
t.fail('unexpected disconnect')
69+
}
70+
})
71+
6672
// make an undici request using where it wants http/2
6773
const response = await client.request({
6874
path: '/',
@@ -211,6 +217,12 @@ test('Should upgrade to HTTP/2 when HTTPS/1 is available for POST', async (t) =>
211217
// close the client on teardown
212218
after(() => client.close())
213219

220+
client.on('disconnect', () => {
221+
if (!client.closed && !client.destroyed) {
222+
t.fail('unexpected disconnect')
223+
}
224+
})
225+
214226
// make an undici request using where it wants http/2
215227
const response = await client.request({
216228
path: '/',

‎test/http2-dispatcher.js‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ test('Dispatcher#Stream', async t => {
4545
})
4646
after(() => client.close())
4747

48+
client.on('disconnect', () => {
49+
if (!client.closed && !client.destroyed) {
50+
t.fail('unexpected disconnect')
51+
}
52+
})
53+
4854
await client.stream(
4955
{ path: '/', opaque: { bufs }, method: 'POST', body: expectedBody },
5056
({ statusCode, headers, opaque: { bufs } }) => {
@@ -99,6 +105,12 @@ test('Dispatcher#Pipeline', async t => {
99105
})
100106
after(() => client.close())
101107

108+
client.on('disconnect', () => {
109+
if (!client.closed && !client.destroyed) {
110+
t.fail('unexpected disconnect')
111+
}
112+
})
113+
102114
pipeline(
103115
new Readable({
104116
read () {
@@ -159,6 +171,12 @@ test('Dispatcher#Connect', async t => {
159171
})
160172
after(() => forward.close())
161173

174+
forward.on('disconnect', () => {
175+
if (!forward.closed && !forward.destroyed) {
176+
t.fail('unexpected disconnect')
177+
}
178+
})
179+
162180
try {
163181
const response = await forward.request({
164182
path: '/',
@@ -206,6 +224,12 @@ test('Dispatcher#Connect', async t => {
206224
after(() => proxy.close())
207225
after(() => server.close())
208226

227+
client.on('disconnect', () => {
228+
if (!client.closed && !client.destroyed) {
229+
t.fail('unexpected disconnect')
230+
}
231+
})
232+
209233
const { statusCode, headers, socket } = await client.connect({ path: '/', headers: { 'x-my-header': 'foo' } })
210234
t.strictEqual(statusCode, 200)
211235
t.strictEqual(headers['x-my-header'], 'foo')
@@ -589,6 +613,12 @@ test('Should handle h2 request without body', async t => {
589613
})
590614
after(() => client.close())
591615

616+
client.on('disconnect', () => {
617+
if (!client.closed && !client.destroyed) {
618+
t.fail('unexpected disconnect')
619+
}
620+
})
621+
592622
const response = await client.request({
593623
path: '/',
594624
method: 'POST',
@@ -635,6 +665,12 @@ test('Should clear h2 request stream references before completing a response', a
635665
})
636666
after(() => client.close())
637667

668+
client.on('disconnect', () => {
669+
if (!client.closed && !client.destroyed) {
670+
t.fail('unexpected disconnect')
671+
}
672+
})
673+
638674
let requestStreamIdSymbol = null
639675
let requestStreamSymbol = null
640676
let requestStreamCleanupSymbol = null
@@ -925,6 +961,12 @@ test('Should send http2 PING frames', async t => {
925961
server.close()
926962
})
927963

964+
client.on('disconnect', () => {
965+
if (!client.closed && !client.destroyed) {
966+
t.fail('unexpected disconnect')
967+
}
968+
})
969+
928970
client.dispatch({
929971
path: '/',
930972
method: 'PUT',
@@ -996,6 +1038,12 @@ test('Should not send http2 PING frames if interval === 0', async t => {
9961038
server.close()
9971039
})
9981040

1041+
client.on('disconnect', () => {
1042+
if (!client.closed && !client.destroyed) {
1043+
t.fail('unexpected disconnect')
1044+
}
1045+
})
1046+
9991047
client.dispatch({
10001048
path: '/',
10011049
method: 'PUT',
@@ -1068,6 +1116,12 @@ test('Should not send http2 PING frames after connection is closed', async t =>
10681116
server.close()
10691117
})
10701118

1119+
client.on('disconnect', () => {
1120+
if (!client.closed && !client.destroyed) {
1121+
t.fail('unexpected disconnect')
1122+
}
1123+
})
1124+
10711125
client.dispatch({
10721126
path: '/',
10731127
method: 'PUT',

‎test/http2-trailers.js‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

3-
const assert = require('node:assert')
4-
const { test } = require('node:test')
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
55
const { createSecureServer } = require('node:http2')
66
const { once } = require('node:events')
77

@@ -10,10 +10,12 @@ const pem = require('@metcoder95/https-pem')
1010
const { Client } = require('..')
1111

1212
test('Should handle http2 trailers', async t => {
13+
t = tspl(t, { plan: 4 })
14+
1315
const server = createSecureServer(pem)
1416
let client = null
1517

16-
t.after(async () => {
18+
after(async () => {
1719
await client?.close()
1820
await new Promise(resolve => server.close(resolve))
1921
})
@@ -44,14 +46,22 @@ test('Should handle http2 trailers', async t => {
4446
allowH2: true
4547
})
4648

49+
client.on('disconnect', () => {
50+
if (!client.closed && !client.destroyed) {
51+
t.fail('unexpected disconnect')
52+
}
53+
})
54+
4755
const { statusCode, headers, body, trailers } = await client.request({
4856
path: '/',
4957
method: 'PUT',
5058
body: 'hello'
5159
})
5260

53-
assert.strictEqual(statusCode, 200)
54-
assert.strictEqual(headers['content-type'], 'text/plain; charset=utf-8')
55-
assert.strictEqual(await body.text(), 'hello h2!')
56-
assert.deepStrictEqual(trailers, { 'x-trailer': 'hello' })
61+
t.strictEqual(statusCode, 200)
62+
t.strictEqual(headers['content-type'], 'text/plain; charset=utf-8')
63+
t.strictEqual(await body.text(), 'hello h2!')
64+
t.deepStrictEqual(trailers, { 'x-trailer': 'hello' })
65+
66+
await t.completed
5767
})

0 commit comments

Comments
 (0)