Skip to content

Commit 9b8d317

Browse files
committed
test: fix flaky test-http2-invalidheaderfield
Separate test cases to avoid side effects and race conditions. Fixes: #34172 PR-URL: #34173 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Andrey Pechkurov <apechkurov@gmail.com>
1 parent d08334a commit 9b8d317

File tree

1 file changed

+67
-46
lines changed

1 file changed

+67
-46
lines changed

test/parallel/test-http2-invalidheaderfield.js

+67-46
Original file line numberDiff line numberDiff line change
@@ -10,56 +10,77 @@ if (!common.hasCrypto) { common.skip('missing crypto'); }
1010
const http2 = require('http2');
1111
const { throws, strictEqual } = require('assert');
1212

13-
const server = http2.createServer(common.mustCall((req, res) => {
14-
throws(() => {
15-
res.setHeader(':path', '/');
16-
}, {
17-
code: 'ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'
18-
});
19-
throws(() => {
20-
res.setHeader('t est', 123);
21-
}, {
22-
code: 'ERR_INVALID_HTTP_TOKEN'
23-
});
24-
res.setHeader('TEST', 123);
25-
res.setHeader('test_', 123);
26-
res.setHeader(' test', 123);
27-
res.end();
28-
}));
13+
{
14+
const server = http2.createServer(common.mustCall((req, res) => {
15+
throws(() => {
16+
res.setHeader(':path', '/');
17+
}, {
18+
code: 'ERR_HTTP2_PSEUDOHEADER_NOT_ALLOWED'
19+
});
20+
throws(() => {
21+
res.setHeader('t est', 123);
22+
}, {
23+
code: 'ERR_INVALID_HTTP_TOKEN'
24+
});
25+
res.setHeader('TEST', 123);
26+
res.setHeader('test_', 123);
27+
res.setHeader(' test', 123);
28+
res.end();
29+
}));
30+
31+
server.listen(0, common.mustCall(() => {
32+
const session = http2.connect(`http://localhost:${server.address().port}`);
33+
session.request({ 'test_': 123, 'TEST': 123 })
34+
.on('end', common.mustCall(() => {
35+
session.close();
36+
server.close();
37+
}));
38+
}));
39+
}
2940

30-
server.listen(0, common.mustCall(() => {
31-
const session1 = http2.connect(`http://localhost:${server.address().port}`);
32-
session1.request({ 'test_': 123, 'TEST': 123 })
33-
.on('end', common.mustCall(() => {
34-
session1.close();
41+
{
42+
const server = http2.createServer();
43+
server.listen(0, common.mustCall(() => {
44+
const session = http2.connect(`http://localhost:${server.address().port}`);
45+
session.on('error', common.mustCall((e) => {
46+
strictEqual(e.code, 'ERR_INVALID_HTTP_TOKEN');
3547
server.close();
3648
}));
37-
38-
const session2 = http2.connect(`http://localhost:${server.address().port}`);
39-
session2.on('error', common.mustCall((e) => {
40-
strictEqual(e.code, 'ERR_INVALID_HTTP_TOKEN');
49+
throws(() => {
50+
session.request({ 't est': 123 });
51+
}, {
52+
code: 'ERR_INVALID_HTTP_TOKEN'
53+
});
4154
}));
42-
throws(() => {
43-
session2.request({ 't est': 123 });
44-
}, {
45-
code: 'ERR_INVALID_HTTP_TOKEN'
46-
});
55+
}
4756

48-
const session3 = http2.connect(`http://localhost:${server.address().port}`);
49-
session3.on('error', common.mustCall((e) => {
50-
strictEqual(e.code, 'ERR_INVALID_HTTP_TOKEN');
57+
58+
{
59+
const server = http2.createServer();
60+
server.listen(0, common.mustCall(() => {
61+
const session = http2.connect(`http://localhost:${server.address().port}`);
62+
session.on('error', common.mustCall((e) => {
63+
strictEqual(e.code, 'ERR_INVALID_HTTP_TOKEN');
64+
server.close();
65+
}));
66+
throws(() => {
67+
session.request({ ' test': 123 });
68+
}, {
69+
code: 'ERR_INVALID_HTTP_TOKEN'
70+
});
5171
}));
52-
throws(() => {
53-
session3.request({ ' test': 123 });
54-
}, {
55-
code: 'ERR_INVALID_HTTP_TOKEN'
56-
});
72+
}
5773

58-
const session4 = http2.connect(`http://localhost:${server.address().port}`);
59-
throws(() => {
60-
session4.request({ ':test': 123 });
61-
}, {
62-
code: 'ERR_HTTP2_INVALID_PSEUDOHEADER'
63-
});
64-
session4.close();
65-
}));
74+
{
75+
const server = http2.createServer();
76+
server.listen(0, common.mustCall(() => {
77+
const session4 = http2.connect(`http://localhost:${server.address().port}`);
78+
throws(() => {
79+
session4.request({ ':test': 123 });
80+
}, {
81+
code: 'ERR_HTTP2_INVALID_PSEUDOHEADER'
82+
});
83+
session4.close();
84+
server.close();
85+
}));
86+
}

0 commit comments

Comments
 (0)