http: expose host and protocol on ClientRequest

Allow host and protocol to be inspected.

PR-URL: https://github.com/nodejs/node/pull/33803
Fixes: https://github.com/nodejs/node/issues/2461
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
wenningplus authored and Anna Henningsen committed 2020-09-22 23:46:42 +02:00
1 parent be6aee9f53
commit 388d125a64
3 files changed
+46

No files matched your search

+21
View File
@@ -688,6 +688,27 @@ added: v0.4.0
* {string} The request path.
### `request.method`
<!-- YAML
added: v0.1.97
-->
* {string} The request method.
### `request.host`
<!-- YAML
added: REPLACEME
-->
* {string} The request host.
### `request.protocol`
<!-- YAML
added: REPLACEME
-->
* {string} The request protocol.
### `request.removeHeader(name)`
<!-- YAML
added: v1.6.0
+2
View File
@@ -213,6 +213,8 @@ function ClientRequest(input, options, cb) {
this.parser = null;
this.maxHeadersCount = null;
this.reusedSocket = false;
this.host = host;
this.protocol = protocol;
let called = false;
@@ -51,3 +51,26 @@ const OutgoingMessage = http.OutgoingMessage;
msg.write('asd');
assert.strictEqual(msg.writableLength, 7);
}
{
const server = http.createServer((req, res) => {
res.end();
server.close();
});
server.listen(0);
server.on('listening', common.mustCall(() => {
const req = http.request({
port: server.address().port,
method: 'GET',
path: '/'
});
assert.strictEqual(req.path, '/');
assert.strictEqual(req.method, 'GET');
assert.strictEqual(req.host, 'localhost');
assert.strictEqual(req.protocol, 'http:');
req.end();
}));
}