Skip to content

Commit 09f9d00

Browse files
apapirovskiMylesBorins
authored andcommitted
test: increase Http2ServerResponse test coverage
Modify existing header tests for Http2ServerResponse to include sendDate (get and set) and headersSent. Expand existing test for end to include a check for closed. Add a new test for destroy. PR-URL: #15074 Refs: #14985 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 488a576 commit 09f9d00

4 files changed

+100
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Flags: --expose-http2
2+
'use strict';
3+
4+
const common = require('../common');
5+
if (!common.hasCrypto)
6+
common.skip('missing crypto');
7+
const assert = require('assert');
8+
const http2 = require('http2');
9+
10+
// Check that destroying the Http2ServerResponse stream produces
11+
// the expected result, including the ability to throw an error
12+
// which is emitted on server.streamError
13+
14+
const errors = [
15+
'test-error',
16+
Error('test')
17+
];
18+
let nextError;
19+
20+
const server = http2.createServer(common.mustCall((req, res) => {
21+
req.on('error', common.mustNotCall());
22+
res.on('error', common.mustNotCall());
23+
24+
res.on('finish', common.mustCall(() => {
25+
assert.doesNotThrow(() => res.destroy(nextError));
26+
assert.strictEqual(res.closed, true);
27+
}));
28+
29+
if (req.path !== '/') {
30+
nextError = errors.shift();
31+
}
32+
res.destroy(nextError);
33+
}, 3));
34+
35+
server.on(
36+
'streamError',
37+
common.mustCall((err) => assert.strictEqual(err, nextError), 2)
38+
);
39+
40+
server.listen(0, common.mustCall(() => {
41+
const port = server.address().port;
42+
const client = http2.connect(`http://localhost:${port}`);
43+
const req = client.request({
44+
':path': '/',
45+
':method': 'GET',
46+
':scheme': 'http',
47+
':authority': `localhost:${port}`
48+
});
49+
50+
req.on('response', common.mustNotCall());
51+
req.on('error', common.mustNotCall());
52+
req.on('end', common.mustCall());
53+
54+
req.resume();
55+
req.end();
56+
57+
const req2 = client.request({
58+
':path': '/error',
59+
':method': 'GET',
60+
':scheme': 'http',
61+
':authority': `localhost:${port}`
62+
});
63+
64+
req2.on('response', common.mustNotCall());
65+
req2.on('error', common.mustNotCall());
66+
req2.on('end', common.mustCall());
67+
68+
req2.resume();
69+
req2.end();
70+
71+
const req3 = client.request({
72+
':path': '/error',
73+
':method': 'GET',
74+
':scheme': 'http',
75+
':authority': `localhost:${port}`
76+
});
77+
78+
req3.on('response', common.mustNotCall());
79+
req3.on('error', common.expectsError({
80+
code: 'ERR_HTTP2_STREAM_ERROR',
81+
type: Error,
82+
message: 'Stream closed with error code 2'
83+
}));
84+
req3.on('end', common.mustCall(() => {
85+
server.close();
86+
client.destroy();
87+
}));
88+
89+
req3.resume();
90+
req3.end();
91+
}));

test/parallel/test-http2-compat-serverresponse-end.js

+2
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@ const {
1818
// Http2ServerResponse.end callback is called only the first time,
1919
// but may be invoked repeatedly without throwing errors.
2020
const server = createServer(mustCall((request, response) => {
21+
strictEqual(response.closed, false);
2122
response.end(mustCall(() => {
2223
server.close();
2324
}));
2425
response.end(mustNotCall());
26+
strictEqual(response.closed, true);
2527
}));
2628
server.listen(0, mustCall(() => {
2729
const { port } = server.address();

test/parallel/test-http2-compat-serverresponse-flushheaders.js

+3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ const server = h2.createServer();
1515
server.listen(0, common.mustCall(function() {
1616
const port = server.address().port;
1717
server.once('request', common.mustCall(function(request, response) {
18+
assert.strictEqual(response.headersSent, false);
1819
response.flushHeaders();
20+
assert.strictEqual(response.headersSent, true);
1921
response.flushHeaders(); // Idempotent
22+
2023
common.expectsError(() => {
2124
response.writeHead(400, { 'foo-bar': 'abc123' });
2225
}, {

test/parallel/test-http2-compat-serverresponse-headers.js

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ server.listen(0, common.mustCall(function() {
8080
response.getHeaders()[fake] = fake;
8181
assert.strictEqual(response.hasHeader(fake), false);
8282

83+
assert.strictEqual(response.sendDate, true);
84+
response.sendDate = false;
85+
assert.strictEqual(response.sendDate, false);
86+
8387
response.on('finish', common.mustCall(function() {
8488
server.close();
8589
}));

0 commit comments

Comments
 (0)