Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/internal/inspector/network_http.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
sniffMimeType,
} = require('internal/inspector/network');
const { Network } = require('inspector');
const { Buffer } = require('buffer');
const EventEmitter = require('events');
const { kEmptyObject } = require('internal/util');

Expand Down Expand Up @@ -138,6 +139,9 @@ function onClientResponseFinish({ request, response }) {

// Unlike response.on('data', ...), this does not put the stream into flowing mode.
EventEmitter.prototype.on.call(response, 'data', (chunk) => {
if (typeof chunk === 'string') {
chunk = Buffer.from(chunk, response.readableEncoding);
}
Network.dataReceived({
requestId: request[kInspectorRequestId],
timestamp: getMonotonicTime(),
Expand Down
3 changes: 3 additions & 0 deletions lib/internal/inspector/network_http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ function onClientStreamFinish({ stream, headers }) {

// Unlike stream.on('data', ...), this does not put the stream into flowing mode.
EventEmitter.prototype.on.call(stream, 'data', (chunk) => {
if (typeof chunk === 'string') {
chunk = Buffer.from(chunk, stream.readableEncoding);
}
/**
* When a chunk of the response body has been received, cache it until `getResponseBody` request
* https://chromedevtools.github.io/devtools-protocol/1-3/Network/#method-getResponseBody or
Expand Down
32 changes: 32 additions & 0 deletions test/parallel/test-inspector-network-http.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,36 @@ async function testHttpGet() {
await assertResponseBody(responseReceived, '\nhello world\n');
}

async function testHttpGetWithSetEncoding() {
const url = `http://127.0.0.1:${httpServer.address().port}/hello-world`;
const {
requestWillBeSentFuture,
responseReceivedFuture,
loadingFinishedFuture,
} = createRequestTracker(url, getDefaultResponseExpect(url));

http.get({
host: '127.0.0.1',
port: httpServer.address().port,
path: '/hello-world',
headers: requestHeaders
}, common.mustCall((response) => {
response.setEncoding('utf8');
let body = '';
response.on('data', (chunk) => {
body += chunk;
});
response.on('end', common.mustCall(() => {
assert.strictEqual(body, '\nhello world\n');
}));
}));

await requestWillBeSentFuture;
const responseReceived = await responseReceivedFuture;
await loadingFinishedFuture;
await assertResponseBody(responseReceived, '\nhello world\n');
}

async function testHttpGetWithAbsoluteUrlPath() {
const url = `http://127.0.0.1:${httpServer.address().port}/hello-world`;
const {
Expand Down Expand Up @@ -383,6 +413,8 @@ async function testHttpsError() {
const testNetworkInspection = async () => {
await testHttpGet();
session.removeAllListeners();
await testHttpGetWithSetEncoding();
session.removeAllListeners();
await testHttpGetWithAbsoluteUrlPath();
session.removeAllListeners();
await testHttpPostWithAbsoluteUrlPath();
Expand Down
38 changes: 38 additions & 0 deletions test/parallel/test-inspector-network-http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ const handleStream = common.mustCallAtLeast((stream, headers) => {
}, kTimeout);
}));
break;
case '/set-encoding':
stream.respond(responseHeaders);
stream.end(serverResponse);
break;
case '/trigger-error':
stream.close(http2.constants.NGHTTP2_STREAM_CLOSED);
stream.on('error', common.expectsError({
Expand Down Expand Up @@ -283,6 +287,38 @@ async function testHttp2(secure = false) {
assert.ok(delta > kDelta);
}

async function testHttp2WithSetEncoding() {
const origin = `http://localhost:${http2Server.address().port}`;

const responseReceived = once(session, 'Network.responseReceived');
const loadingFinished = once(session, 'Network.loadingFinished');
session.on('Network.loadingFailed', common.mustNotCall());

const client = http2.connect(origin);
const request = client.request({
[http2.constants.HTTP2_HEADER_PATH]: '/set-encoding',
});
request.setEncoding('hex');
let body = '';
request.on('data', (chunk) => {
body += chunk;
});
request.on('end', common.mustCall(() => {
assert.strictEqual(body, Buffer.from(serverResponse).toString('hex'));
client.close();
}));
request.end();

const [ response ] = await responseReceived;
await loadingFinished;

const responseBody = await session.post('Network.getResponseBody', {
requestId: response.params.requestId,
});
assert.strictEqual(responseBody.base64Encoded, false);
assert.strictEqual(responseBody.body, serverResponse);
}

async function testHttp2Error(secure = false) {
const port = (secure ? http2SecureServer : http2Server).address().port;
const origin = (secure ? 'https' : 'http') + `://localhost:${port}`;
Expand Down Expand Up @@ -321,6 +357,8 @@ const testNetworkInspection = async () => {
session.removeAllListeners();
await testHttp2(true);
session.removeAllListeners();
await testHttp2WithSetEncoding();
session.removeAllListeners();
await testHttp2Error();
session.removeAllListeners();
await testHttp2Error(true);
Expand Down
Loading