Skip to content

Commit 061f1a2

Browse files
committed
inspector: fix tests and inline
1 parent 06183cd commit 061f1a2

File tree

2 files changed

+116
-120
lines changed

2 files changed

+116
-120
lines changed

test/parallel/test-inspector-network-http-compressed.js

Lines changed: 54 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ const handleRequest = (req, res) => {
6868
res.writeHead(200);
6969
res.end(plainTextBody);
7070
break;
71+
case '/invalid-gzip':
72+
// Send invalid data with gzip content-encoding to trigger decompression error
73+
setResponseHeaders(res, 'gzip');
74+
res.writeHead(200);
75+
res.end('this is not valid gzip data');
76+
break;
7177
default:
7278
assert.fail(`Unexpected path: ${path}`);
7379
}
@@ -103,6 +109,36 @@ function verifyLoadingFinished({ method, params }) {
103109
return params;
104110
}
105111

112+
async function testInvalidCompressedResponse(server) {
113+
const port = server.address().port;
114+
const protocol = server === httpsServer ? 'https' : 'http';
115+
const path = '/invalid-gzip';
116+
const url = `${protocol}://127.0.0.1:${port}${path}`;
117+
118+
const responseReceivedFuture = once(session, 'Network.responseReceived')
119+
.then(([event]) => verifyResponseReceived(event, { url }));
120+
121+
const client = protocol === 'https' ? https : http;
122+
123+
await new Promise((resolve) => {
124+
const req = client.get({
125+
host: '127.0.0.1',
126+
port,
127+
path,
128+
rejectUnauthorized: false,
129+
}, (res) => {
130+
// Consume the response to trigger the decompression error in inspector
131+
res.on('data', () => {});
132+
res.on('end', resolve);
133+
});
134+
req.on('error', resolve);
135+
});
136+
137+
await responseReceivedFuture;
138+
// Note: loadingFinished is not emitted when decompression fails,
139+
// but this test ensures the error handler is triggered for coverage.
140+
}
141+
106142
async function testCompressedResponse(server, encoding, path) {
107143
const port = server.address().port;
108144
const protocol = server === httpsServer ? 'https' : 'http';
@@ -164,89 +200,47 @@ async function testCompressedResponse(server, encoding, path) {
164200
assert.strictEqual(responseBody.body, plainTextBody);
165201
}
166202

167-
async function testGzipHttp() {
168-
await testCompressedResponse(httpServer, 'gzip', '/gzip');
169-
}
170-
171-
async function testGzipHttps() {
172-
await testCompressedResponse(httpsServer, 'gzip', '/gzip');
173-
}
174-
175-
async function testXGzipHttp() {
176-
await testCompressedResponse(httpServer, 'x-gzip', '/x-gzip');
177-
}
178-
179-
async function testXGzipHttps() {
180-
await testCompressedResponse(httpsServer, 'x-gzip', '/x-gzip');
181-
}
182-
183-
async function testDeflateHttp() {
184-
await testCompressedResponse(httpServer, 'deflate', '/deflate');
185-
}
186-
187-
async function testDeflateHttps() {
188-
await testCompressedResponse(httpsServer, 'deflate', '/deflate');
189-
}
190-
191-
async function testBrotliHttp() {
192-
await testCompressedResponse(httpServer, 'br', '/br');
193-
}
194-
195-
async function testBrotliHttps() {
196-
await testCompressedResponse(httpsServer, 'br', '/br');
197-
}
198-
199-
async function testZstdHttp() {
200-
await testCompressedResponse(httpServer, 'zstd', '/zstd');
201-
}
202-
203-
async function testZstdHttps() {
204-
await testCompressedResponse(httpsServer, 'zstd', '/zstd');
205-
}
206-
207-
async function testPlainHttp() {
208-
await testCompressedResponse(httpServer, null, '/plain');
209-
}
210-
211-
async function testPlainHttps() {
212-
await testCompressedResponse(httpsServer, null, '/plain');
213-
}
214-
215203
const testNetworkInspection = async () => {
216204
// Test gzip
217-
await testGzipHttp();
205+
await testCompressedResponse(httpServer, 'gzip', '/gzip');
218206
session.removeAllListeners();
219-
await testGzipHttps();
207+
await testCompressedResponse(httpsServer, 'gzip', '/gzip');
220208
session.removeAllListeners();
221209

222210
// Test x-gzip (alternate gzip encoding)
223-
await testXGzipHttp();
211+
await testCompressedResponse(httpServer, 'x-gzip', '/x-gzip');
224212
session.removeAllListeners();
225-
await testXGzipHttps();
213+
await testCompressedResponse(httpsServer, 'x-gzip', '/x-gzip');
226214
session.removeAllListeners();
227215

228216
// Test deflate
229-
await testDeflateHttp();
217+
await testCompressedResponse(httpServer, 'deflate', '/deflate');
230218
session.removeAllListeners();
231-
await testDeflateHttps();
219+
await testCompressedResponse(httpsServer, 'deflate', '/deflate');
232220
session.removeAllListeners();
233221

234222
// Test brotli
235-
await testBrotliHttp();
223+
await testCompressedResponse(httpServer, 'br', '/br');
236224
session.removeAllListeners();
237-
await testBrotliHttps();
225+
await testCompressedResponse(httpsServer, 'br', '/br');
238226
session.removeAllListeners();
239227

240228
// Test zstd
241-
await testZstdHttp();
229+
await testCompressedResponse(httpServer, 'zstd', '/zstd');
242230
session.removeAllListeners();
243-
await testZstdHttps();
231+
await testCompressedResponse(httpsServer, 'zstd', '/zstd');
244232
session.removeAllListeners();
245233

246234
// Test plain (no compression)
247-
await testPlainHttp();
235+
await testCompressedResponse(httpServer, null, '/plain');
236+
session.removeAllListeners();
237+
await testCompressedResponse(httpsServer, null, '/plain');
238+
session.removeAllListeners();
239+
240+
// Test invalid compressed data (triggers decompression error handler)
241+
await testInvalidCompressedResponse(httpServer);
248242
session.removeAllListeners();
249-
await testPlainHttps();
243+
await testInvalidCompressedResponse(httpsServer);
250244
session.removeAllListeners();
251245
};
252246

test/parallel/test-inspector-network-http2-compressed.js

Lines changed: 62 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ const handleStream = common.mustCallAtLeast((stream, headers) => {
6565
stream.respond(responseHeaders);
6666
stream.end(plainTextBody);
6767
break;
68+
case '/invalid-gzip':
69+
// Send invalid data with gzip content-encoding to trigger decompression error
70+
responseHeaders[http2.constants.HTTP2_HEADER_CONTENT_ENCODING] = 'gzip';
71+
stream.respond(responseHeaders);
72+
stream.end('this is not valid gzip data');
73+
break;
6874
default:
6975
assert.fail(`Unexpected path: ${path}`);
7076
}
@@ -102,6 +108,44 @@ function verifyLoadingFinished({ method, params }) {
102108
return params;
103109
}
104110

111+
async function testInvalidCompressedResponse(server) {
112+
const port = server.address().port;
113+
const secure = server === http2SecureServer;
114+
const origin = (secure ? 'https' : 'http') + `://localhost:${port}`;
115+
const path = '/invalid-gzip';
116+
const url = `${origin}${path}`;
117+
118+
const responseReceivedFuture = once(session, 'Network.responseReceived')
119+
.then(([event]) => verifyResponseReceived(event, { url }));
120+
121+
await new Promise((resolve) => {
122+
const client = http2.connect(origin, {
123+
rejectUnauthorized: false,
124+
});
125+
126+
const req = client.request({
127+
[http2.constants.HTTP2_HEADER_PATH]: path,
128+
[http2.constants.HTTP2_HEADER_METHOD]: 'GET',
129+
});
130+
131+
// Consume the response to trigger the decompression error in inspector
132+
req.on('data', () => {});
133+
req.on('end', () => {
134+
client.close();
135+
resolve();
136+
});
137+
req.on('error', () => {
138+
client.close();
139+
resolve();
140+
});
141+
req.end();
142+
});
143+
144+
await responseReceivedFuture;
145+
// Note: loadingFinished is not emitted when decompression fails,
146+
// but this test ensures the error handler is triggered for coverage.
147+
}
148+
105149
async function testCompressedResponse(server, encoding, path) {
106150
const port = server.address().port;
107151
const secure = server === http2SecureServer;
@@ -179,89 +223,47 @@ async function testCompressedResponse(server, encoding, path) {
179223
assert.strictEqual(responseBody.body, plainTextBody);
180224
}
181225

182-
async function testGzipHttp2() {
183-
await testCompressedResponse(http2Server, 'gzip', '/gzip');
184-
}
185-
186-
async function testGzipHttp2Secure() {
187-
await testCompressedResponse(http2SecureServer, 'gzip', '/gzip');
188-
}
189-
190-
async function testXGzipHttp2() {
191-
await testCompressedResponse(http2Server, 'x-gzip', '/x-gzip');
192-
}
193-
194-
async function testXGzipHttp2Secure() {
195-
await testCompressedResponse(http2SecureServer, 'x-gzip', '/x-gzip');
196-
}
197-
198-
async function testDeflateHttp2() {
199-
await testCompressedResponse(http2Server, 'deflate', '/deflate');
200-
}
201-
202-
async function testDeflateHttp2Secure() {
203-
await testCompressedResponse(http2SecureServer, 'deflate', '/deflate');
204-
}
205-
206-
async function testBrotliHttp2() {
207-
await testCompressedResponse(http2Server, 'br', '/br');
208-
}
209-
210-
async function testBrotliHttp2Secure() {
211-
await testCompressedResponse(http2SecureServer, 'br', '/br');
212-
}
213-
214-
async function testZstdHttp2() {
215-
await testCompressedResponse(http2Server, 'zstd', '/zstd');
216-
}
217-
218-
async function testZstdHttp2Secure() {
219-
await testCompressedResponse(http2SecureServer, 'zstd', '/zstd');
220-
}
221-
222-
async function testPlainHttp2() {
223-
await testCompressedResponse(http2Server, null, '/plain');
224-
}
225-
226-
async function testPlainHttp2Secure() {
227-
await testCompressedResponse(http2SecureServer, null, '/plain');
228-
}
229-
230226
const testNetworkInspection = async () => {
231227
// Test gzip
232-
await testGzipHttp2();
228+
await testCompressedResponse(http2Server, 'gzip', '/gzip');
233229
session.removeAllListeners();
234-
await testGzipHttp2Secure();
230+
await testCompressedResponse(http2SecureServer, 'gzip', '/gzip');
235231
session.removeAllListeners();
236232

237233
// Test x-gzip (alternate gzip encoding)
238-
await testXGzipHttp2();
234+
await testCompressedResponse(http2Server, 'x-gzip', '/x-gzip');
239235
session.removeAllListeners();
240-
await testXGzipHttp2Secure();
236+
await testCompressedResponse(http2SecureServer, 'x-gzip', '/x-gzip');
241237
session.removeAllListeners();
242238

243239
// Test deflate
244-
await testDeflateHttp2();
240+
await testCompressedResponse(http2Server, 'deflate', '/deflate');
245241
session.removeAllListeners();
246-
await testDeflateHttp2Secure();
242+
await testCompressedResponse(http2SecureServer, 'deflate', '/deflate');
247243
session.removeAllListeners();
248244

249245
// Test brotli
250-
await testBrotliHttp2();
246+
await testCompressedResponse(http2Server, 'br', '/br');
251247
session.removeAllListeners();
252-
await testBrotliHttp2Secure();
248+
await testCompressedResponse(http2SecureServer, 'br', '/br');
253249
session.removeAllListeners();
254250

255251
// Test zstd
256-
await testZstdHttp2();
252+
await testCompressedResponse(http2Server, 'zstd', '/zstd');
257253
session.removeAllListeners();
258-
await testZstdHttp2Secure();
254+
await testCompressedResponse(http2SecureServer, 'zstd', '/zstd');
259255
session.removeAllListeners();
260256

261257
// Test plain (no compression)
262-
await testPlainHttp2();
258+
await testCompressedResponse(http2Server, null, '/plain');
259+
session.removeAllListeners();
260+
await testCompressedResponse(http2SecureServer, null, '/plain');
261+
session.removeAllListeners();
262+
263+
// Test invalid compressed data (triggers decompression error handler)
264+
await testInvalidCompressedResponse(http2Server);
263265
session.removeAllListeners();
264-
await testPlainHttp2Secure();
266+
await testInvalidCompressedResponse(http2SecureServer);
265267
session.removeAllListeners();
266268
};
267269

0 commit comments

Comments
 (0)