-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
- Loading branch information
Showing
3 changed files
with
109 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(); | ||
}); | ||
}); | ||
}); |