Skip to content

Commit c047e73

Browse files
legendecasaduh95
authored andcommitted
inspector: inspect HTTP response body
PR-URL: #60572 Refs: #53946 Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Ryuhei Shima <shimaryuhei@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Kohei Ueno <kohei.ueno119@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 51e5030 commit c047e73

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

‎lib/internal/inspector/network_http.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
sniffMimeType,
1818
}=require('internal/inspector/network');
1919
const{ Network }=require('inspector');
20+
constEventEmitter=require('events');
2021

2122
constkRequestUrl=Symbol('kRequestUrl');
2223

@@ -120,6 +121,17 @@ function onClientResponseFinish({ request, response }) {
120121
},
121122
});
122123

124+
// Unlike response.on('data', ...), this does not put the stream into flowing mode.
125+
EventEmitter.prototype.on.call(response,'data',(chunk)=>{
126+
Network.dataReceived({
127+
requestId: request[kInspectorRequestId],
128+
timestamp: getMonotonicTime(),
129+
dataLength: chunk.byteLength,
130+
encodedDataLength: chunk.byteLength,
131+
data: chunk,
132+
});
133+
});
134+
123135
// Wait until the response body is consumed by user code.
124136
response.once('end',()=>{
125137
Network.loadingFinished({

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,27 @@ function verifyLoadingFailed({ method, params }) {
130130
assert.strictEqual(typeofparams.errorText,'string');
131131
}
132132

133+
functionverifyHttpResponse(response){
134+
assert.strictEqual(response.statusCode,200);
135+
constchunks=[];
136+
137+
// Verifies that the inspector does not put the response into flowing mode.
138+
assert.strictEqual(response.readableFlowing,null);
139+
// Verifies that the data listener may be added at a later time, and it can
140+
// still observe the data in full.
141+
queueMicrotask(()=>{
142+
response.on('data',(chunk)=>{
143+
chunks.push(chunk);
144+
});
145+
assert.strictEqual(response.readableFlowing,true);
146+
});
147+
148+
response.on('end',common.mustCall(()=>{
149+
constbody=Buffer.concat(chunks).toString();
150+
assert.strictEqual(body,'\nhello world\n');
151+
}));
152+
}
153+
133154
asyncfunctiontestHttpGet(){
134155
consturl=`http://127.0.0.1:${httpServer.address().port}/hello-world`;
135156
constrequestWillBeSentFuture=once(session,'Network.requestWillBeSent')
@@ -146,18 +167,20 @@ async function testHttpGet() {
146167
port: httpServer.address().port,
147168
path: '/hello-world',
148169
headers: requestHeaders
149-
},common.mustCall((res)=>{
150-
// Dump the response.
151-
res.on('data',()=>{});
152-
res.on('end',()=>{});
153-
}));
170+
},common.mustCall(verifyHttpResponse));
154171

155172
awaitrequestWillBeSentFuture;
156173
constresponseReceived=awaitresponseReceivedFuture;
157174
constloadingFinished=awaitloadingFinishedFuture;
158175

159176
constdelta=(loadingFinished.timestamp-responseReceived.timestamp)*1000;
160177
assert.ok(delta>kDelta);
178+
179+
constresponseBody=awaitsession.post('Network.getResponseBody',{
180+
requestId: responseReceived.requestId,
181+
});
182+
assert.strictEqual(responseBody.base64Encoded,false);
183+
assert.strictEqual(responseBody.body,'\nhello world\n');
161184
}
162185

163186
asyncfunctiontestHttpsGet(){
@@ -177,18 +200,20 @@ async function testHttpsGet() {
177200
path: '/hello-world',
178201
rejectUnauthorized: false,
179202
headers: requestHeaders,
180-
},common.mustCall((res)=>{
181-
// Dump the response.
182-
res.on('data',()=>{});
183-
res.on('end',()=>{});
184-
}));
203+
},common.mustCall(verifyHttpResponse));
185204

186205
awaitrequestWillBeSentFuture;
187206
constresponseReceived=awaitresponseReceivedFuture;
188207
constloadingFinished=awaitloadingFinishedFuture;
189208

190209
constdelta=(loadingFinished.timestamp-responseReceived.timestamp)*1000;
191210
assert.ok(delta>kDelta);
211+
212+
constresponseBody=awaitsession.post('Network.getResponseBody',{
213+
requestId: responseReceived.requestId,
214+
});
215+
assert.strictEqual(responseBody.base64Encoded,false);
216+
assert.strictEqual(responseBody.body,'\nhello world\n');
192217
}
193218

194219
asyncfunctiontestHttpError(){

0 commit comments

Comments
 (0)