Skip to content

Commit c981483

Browse files
apapirovskimcollina
authored andcommitted
http2: cleanup of h2 compat layer, add tests
Remove unnecessary variable assignments, remove unreachable code pathways, remove path getter and setter, and other very minor cleanup. Only remove reference to stream on nextTick so that users and libraries can access it in the finish event. Fixes: #15313 Refs: expressjs/express#3390 PR-URL: #15254 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent da057db commit c981483

8 files changed

+158
-138
lines changed

lib/internal/http2/compat.js

+111-127
Large diffs are not rendered by default.

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ server.listen(0, common.mustCall(function() {
2121
'foo-bar': 'abc123'
2222
};
2323

24+
assert.strictEqual(request.path, undefined);
2425
assert.strictEqual(request.method, expected[':method']);
2526
assert.strictEqual(request.scheme, expected[':scheme']);
26-
assert.strictEqual(request.path, expected[':path']);
2727
assert.strictEqual(request.url, expected[':path']);
2828
assert.strictEqual(request.authority, expected[':authority']);
2929

@@ -41,11 +41,6 @@ server.listen(0, common.mustCall(function() {
4141

4242
request.url = '/one';
4343
assert.strictEqual(request.url, '/one');
44-
assert.strictEqual(request.path, '/one');
45-
46-
request.path = '/two';
47-
assert.strictEqual(request.url, '/two');
48-
assert.strictEqual(request.path, '/two');
4944

5045
response.on('finish', common.mustCall(function() {
5146
server.close();

test/parallel/test-http2-compat-serverrequest.js

-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ server.listen(0, common.mustCall(function() {
1515
const port = server.address().port;
1616
server.once('request', common.mustCall(function(request, response) {
1717
const expected = {
18-
statusCode: null,
1918
version: '2.0',
2019
httpVersionMajor: 2,
2120
httpVersionMinor: 0
@@ -24,7 +23,6 @@ server.listen(0, common.mustCall(function() {
2423
assert.strictEqual(request.closed, false);
2524
assert.strictEqual(request.code, h2.constants.NGHTTP2_NO_ERROR);
2625

27-
assert.strictEqual(request.statusCode, expected.statusCode);
2826
assert.strictEqual(request.httpVersion, expected.version);
2927
assert.strictEqual(request.httpVersionMajor, expected.httpVersionMajor);
3028
assert.strictEqual(request.httpVersionMinor, expected.httpVersionMinor);

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const server = http2.createServer(common.mustCall((req, res) => {
2626
assert.strictEqual(res.closed, true);
2727
}));
2828

29-
if (req.path !== '/') {
29+
if (req.url !== '/') {
3030
nextError = errors.shift();
3131
}
3232
res.destroy(nextError);

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

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ const assert = require('assert');
88
const h2 = require('http2');
99

1010
// Http2ServerResponse.finished
11-
1211
const server = h2.createServer();
1312
server.listen(0, common.mustCall(function() {
1413
const port = server.address().port;
1514
server.once('request', common.mustCall(function(request, response) {
1615
response.on('finish', common.mustCall(function() {
16+
assert.ok(request.stream !== undefined);
17+
assert.ok(response.stream !== undefined);
1718
server.close();
19+
process.nextTick(common.mustCall(() => {
20+
assert.strictEqual(request.stream, undefined);
21+
assert.strictEqual(response.stream, undefined);
22+
}));
1823
}));
1924
assert.strictEqual(response.finished, false);
2025
response.end();

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

+33
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@ server.listen(0, common.mustCall(function() {
4242
response.removeHeader(denormalised);
4343
assert.strictEqual(response.hasHeader(denormalised), false);
4444

45+
common.expectsError(
46+
() => response.hasHeader(),
47+
{
48+
code: 'ERR_INVALID_ARG_TYPE',
49+
type: TypeError,
50+
message: 'The "name" argument must be of type string'
51+
}
52+
);
53+
common.expectsError(
54+
() => response.getHeader(),
55+
{
56+
code: 'ERR_INVALID_ARG_TYPE',
57+
type: TypeError,
58+
message: 'The "name" argument must be of type string'
59+
}
60+
);
61+
common.expectsError(
62+
() => response.removeHeader(),
63+
{
64+
code: 'ERR_INVALID_ARG_TYPE',
65+
type: TypeError,
66+
message: 'The "name" argument must be of type string'
67+
}
68+
);
69+
4570
[
4671
':status',
4772
':method',
@@ -70,6 +95,14 @@ server.listen(0, common.mustCall(function() {
7095
type: TypeError,
7196
message: 'Value must not be undefined or null'
7297
}));
98+
common.expectsError(
99+
() => response.setHeader(), // header name undefined
100+
{
101+
code: 'ERR_INVALID_ARG_TYPE',
102+
type: TypeError,
103+
message: 'The "name" argument must be of type string'
104+
}
105+
);
73106

74107
response.setHeader(real, expectedValue);
75108
const expectedHeaderNames = [real];

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

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ server.listen(0, common.mustCall(function() {
2222

2323
response.on('finish', common.mustCall(function() {
2424
server.close();
25+
process.nextTick(common.mustCall(() => {
26+
common.expectsError(() => { response.writeHead(300); }, {
27+
code: 'ERR_HTTP2_STREAM_CLOSED'
28+
});
29+
}));
2530
}));
2631
response.end();
2732
}));

test/parallel/test-http2-createwritereq.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const testsToRun = Object.keys(encodings).length;
3030
let testsFinished = 0;
3131

3232
const server = http2.createServer(common.mustCall((req, res) => {
33-
const testEncoding = encodings[req.path.slice(1)];
33+
const testEncoding = encodings[req.url.slice(1)];
3434

3535
req.on('data', common.mustCall((chunk) => assert.ok(
3636
Buffer.from(testString, testEncoding).equals(chunk)

0 commit comments

Comments
 (0)