Skip to content

Commit 756d5d3

Browse files
authored
http2: split tests of http2.js into multiple files (#4561)
1 parent 6365a90 commit 756d5d3

14 files changed

+2010
-1893
lines changed

test/http2-abort.js

Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
'use strict'
2+
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
5+
const { createSecureServer } = require('node:http2')
6+
const { once } = require('node:events')
7+
8+
const pem = require('@metcoder95/https-pem')
9+
10+
const { Client } = require('..')
11+
12+
test('#2364 - Concurrent aborts', async t => {
13+
t = tspl(t, { plan: 10 })
14+
15+
const server = createSecureServer(await pem.generate({ opts: { keySize: 2048 } }))
16+
17+
server.on('stream', (stream, headers, _flags, rawHeaders) => {
18+
setTimeout(() => {
19+
stream.respond({
20+
'content-type': 'text/plain; charset=utf-8',
21+
'x-custom-h2': 'hello',
22+
':status': 200
23+
})
24+
stream.end('hello h2!')
25+
}, 100)
26+
})
27+
28+
after(() => server.close())
29+
await once(server.listen(0), 'listening')
30+
31+
const client = new Client(`https://localhost:${server.address().port}`, {
32+
connect: {
33+
rejectUnauthorized: false
34+
},
35+
allowH2: true
36+
})
37+
38+
after(() => client.close())
39+
const signal = AbortSignal.timeout(100)
40+
41+
client.request(
42+
{
43+
path: '/1',
44+
method: 'GET',
45+
headers: {
46+
'x-my-header': 'foo'
47+
}
48+
},
49+
(err, response) => {
50+
t.ifError(err)
51+
t.strictEqual(
52+
response.headers['content-type'],
53+
'text/plain; charset=utf-8'
54+
)
55+
t.strictEqual(response.headers['x-custom-h2'], 'hello')
56+
t.strictEqual(response.statusCode, 200)
57+
}
58+
)
59+
60+
client.request(
61+
{
62+
path: '/2',
63+
method: 'GET',
64+
headers: {
65+
'x-my-header': 'foo'
66+
},
67+
signal
68+
},
69+
(err, response) => {
70+
t.strictEqual(err.name, 'TimeoutError')
71+
}
72+
)
73+
74+
client.request(
75+
{
76+
path: '/3',
77+
method: 'GET',
78+
headers: {
79+
'x-my-header': 'foo'
80+
}
81+
},
82+
(err, response) => {
83+
t.ifError(err)
84+
t.strictEqual(
85+
response.headers['content-type'],
86+
'text/plain; charset=utf-8'
87+
)
88+
t.strictEqual(response.headers['x-custom-h2'], 'hello')
89+
t.strictEqual(response.statusCode, 200)
90+
}
91+
)
92+
93+
client.request(
94+
{
95+
path: '/4',
96+
method: 'GET',
97+
headers: {
98+
'x-my-header': 'foo'
99+
},
100+
signal
101+
},
102+
(err, response) => {
103+
t.strictEqual(err.name, 'TimeoutError')
104+
}
105+
)
106+
107+
await t.completed
108+
})
109+
110+
test('#2364 - Concurrent aborts (2nd variant)', async t => {
111+
t = tspl(t, { plan: 10 })
112+
113+
const server = createSecureServer(await pem.generate({ opts: { keySize: 2048 } }))
114+
let counter = 0
115+
116+
server.on('stream', (stream, headers, _flags, rawHeaders) => {
117+
counter++
118+
119+
if (counter % 2 === 0) {
120+
setTimeout(() => {
121+
if (stream.destroyed) {
122+
return
123+
}
124+
125+
stream.respond({
126+
'content-type': 'text/plain; charset=utf-8',
127+
'x-custom-h2': 'hello',
128+
':status': 200
129+
})
130+
131+
stream.end('hello h2!')
132+
}, 400)
133+
134+
return
135+
}
136+
137+
stream.respond({
138+
'content-type': 'text/plain; charset=utf-8',
139+
'x-custom-h2': 'hello',
140+
':status': 200
141+
})
142+
143+
stream.end('hello h2!')
144+
})
145+
146+
after(() => server.close())
147+
await once(server.listen(0), 'listening')
148+
149+
const client = new Client(`https://localhost:${server.address().port}`, {
150+
connect: {
151+
rejectUnauthorized: false
152+
},
153+
allowH2: true
154+
})
155+
after(() => client.close())
156+
157+
const signal = AbortSignal.timeout(300)
158+
159+
client.request(
160+
{
161+
path: '/1',
162+
method: 'GET',
163+
headers: {
164+
'x-my-header': 'foo'
165+
}
166+
},
167+
(err, response) => {
168+
t.ifError(err)
169+
t.strictEqual(
170+
response.headers['content-type'],
171+
'text/plain; charset=utf-8'
172+
)
173+
t.strictEqual(response.headers['x-custom-h2'], 'hello')
174+
t.strictEqual(response.statusCode, 200)
175+
}
176+
)
177+
178+
client.request(
179+
{
180+
path: '/2',
181+
method: 'GET',
182+
headers: {
183+
'x-my-header': 'foo'
184+
},
185+
signal
186+
},
187+
(err, response) => {
188+
t.strictEqual(err.name, 'TimeoutError')
189+
}
190+
)
191+
192+
client.request(
193+
{
194+
path: '/3',
195+
method: 'GET',
196+
headers: {
197+
'x-my-header': 'foo'
198+
}
199+
},
200+
(err, response) => {
201+
t.ifError(err)
202+
t.strictEqual(
203+
response.headers['content-type'],
204+
'text/plain; charset=utf-8'
205+
)
206+
t.strictEqual(response.headers['x-custom-h2'], 'hello')
207+
t.strictEqual(response.statusCode, 200)
208+
}
209+
)
210+
211+
client.request(
212+
{
213+
path: '/4',
214+
method: 'GET',
215+
headers: {
216+
'x-my-header': 'foo'
217+
},
218+
signal
219+
},
220+
(err, response) => {
221+
t.strictEqual(err.name, 'TimeoutError')
222+
}
223+
)
224+
225+
await t.completed
226+
})

test/http2-agent.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
const { tspl } = require('@matteo.collina/tspl')
4+
const { test, after } = require('node:test')
5+
const { createSecureServer } = require('node:http2')
6+
const { once } = require('node:events')
7+
8+
const pem = require('@metcoder95/https-pem')
9+
10+
const { Agent } = require('..')
11+
12+
test('Agent should support H2 connection', async t => {
13+
t = tspl(t, { plan: 6 })
14+
15+
const body = []
16+
const server = createSecureServer(await pem.generate({ opts: { keySize: 2048 } }))
17+
18+
server.on('stream', (stream, headers) => {
19+
t.strictEqual(headers['x-my-header'], 'foo')
20+
t.strictEqual(headers[':method'], 'GET')
21+
stream.respond({
22+
'content-type': 'text/plain; charset=utf-8',
23+
'x-custom-h2': 'hello',
24+
':status': 200
25+
})
26+
stream.end('hello h2!')
27+
})
28+
29+
after(() => server.close())
30+
await once(server.listen(0), 'listening')
31+
32+
const client = new Agent({
33+
connect: {
34+
rejectUnauthorized: false
35+
},
36+
allowH2: true
37+
})
38+
after(() => client.close())
39+
40+
const response = await client.request({
41+
origin: `https://localhost:${server.address().port}`,
42+
path: '/',
43+
method: 'GET',
44+
headers: {
45+
'x-my-header': 'foo'
46+
}
47+
})
48+
49+
response.body.on('data', chunk => {
50+
body.push(chunk)
51+
})
52+
53+
await once(response.body, 'end')
54+
55+
t.strictEqual(response.statusCode, 200)
56+
t.strictEqual(response.headers['content-type'], 'text/plain; charset=utf-8')
57+
t.strictEqual(response.headers['x-custom-h2'], 'hello')
58+
t.strictEqual(Buffer.concat(body).toString('utf8'), 'hello h2!')
59+
60+
await t.completed
61+
})

test/http2-alpn.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,9 @@ test('Should upgrade to HTTP/2 when HTTPS/1 is available for GET', async (t) =>
4444
}
4545
)
4646

47-
server.listen(0)
48-
await once(server, 'listening')
49-
5047
// close the server on teardown
5148
after(() => server.close())
49+
await once(server.listen(0), 'listening')
5250

5351
// set the port
5452
const port = server.address().port
@@ -122,6 +120,8 @@ test('Should upgrade to HTTP/2 when HTTPS/1 is available for GET', async (t) =>
122120
alpnProtocol: false,
123121
httpVersion: '1.1'
124122
}))
123+
124+
await t.completed
125125
})
126126

127127
test('Should upgrade to HTTP/2 when HTTPS/1 is available for POST', async (t) => {
@@ -191,11 +191,9 @@ test('Should upgrade to HTTP/2 when HTTPS/1 is available for POST', async (t) =>
191191
stream.end('hello h2!')
192192
})
193193

194-
server.listen(0)
195-
await once(server, 'listening')
196-
197194
// close the server on teardown
198195
after(() => server.close())
196+
await once(server.listen(0), 'listening')
199197

200198
// set the port
201199
const port = server.address().port
@@ -275,4 +273,6 @@ test('Should upgrade to HTTP/2 when HTTPS/1 is available for POST', async (t) =>
275273
t.equal(httpsResponse.headers['x-custom-alpn-protocol'], 'false')
276274
t.equal(Buffer.concat(httpsResponseBody).toString('utf-8'), 'hello http/1!')
277275
t.equal(Buffer.concat(httpsRequestChunks).toString('utf-8'), expectedBody)
276+
277+
await t.completed
278278
})

0 commit comments

Comments
 (0)