-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: add diagnostics channel for http client
PR-URL: #43580 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Stephen Belanger <admin@stephenbelanger.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
- Loading branch information
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const http = require('http'); | ||
const net = require('net'); | ||
const dc = require('diagnostics_channel'); | ||
|
||
const onClientRequestStart = dc.channel('http.client.request.start'); | ||
const onClientResponseFinish = dc.channel('http.client.response.finish'); | ||
const onServerRequestStart = dc.channel('http.server.request.start'); | ||
const onServerResponseFinish = dc.channel('http.server.response.finish'); | ||
|
||
const isHTTPServer = (server) => server instanceof http.Server; | ||
const isIncomingMessage = (object) => object instanceof http.IncomingMessage; | ||
const isOutgoingMessage = (object) => object instanceof http.OutgoingMessage; | ||
const isNetSocket = (socket) => socket instanceof net.Socket; | ||
|
||
onClientRequestStart.subscribe(common.mustCall(({ request }) => { | ||
assert.strictEqual(isOutgoingMessage(request), true); | ||
})); | ||
|
||
onClientResponseFinish.subscribe(common.mustCall(({ request, response }) => { | ||
assert.strictEqual(isOutgoingMessage(request), true); | ||
assert.strictEqual(isIncomingMessage(response), true); | ||
})); | ||
|
||
onServerRequestStart.subscribe(common.mustCall(({ | ||
request, | ||
response, | ||
socket, | ||
server, | ||
}) => { | ||
assert.strictEqual(isIncomingMessage(request), true); | ||
assert.strictEqual(isOutgoingMessage(response), true); | ||
assert.strictEqual(isNetSocket(socket), true); | ||
assert.strictEqual(isHTTPServer(server), true); | ||
})); | ||
|
||
onServerResponseFinish.subscribe(common.mustCall(({ | ||
request, | ||
response, | ||
socket, | ||
server, | ||
}) => { | ||
assert.strictEqual(isIncomingMessage(request), true); | ||
assert.strictEqual(isOutgoingMessage(response), true); | ||
assert.strictEqual(isNetSocket(socket), true); | ||
assert.strictEqual(isHTTPServer(server), true); | ||
})); | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
res.end('done'); | ||
})); | ||
|
||
server.listen(() => { | ||
const { port } = server.address(); | ||
http.get(`http://localhost:${port}`, (res) => { | ||
res.resume(); | ||
res.on('end', () => { | ||
server.close(); | ||
}); | ||
}); | ||
}); |