Skip to content

Commit a24b8df

Browse files
wenningpluscodebytere
wenningplus
authored andcommitted
http: expose host and protocol on ClientRequest
Allow host and protocol to be inspected. PR-URL: #33803 Fixes: #2461 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent ed26e8e commit a24b8df

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

doc/api/http.md

+21
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,27 @@ added: v0.4.0
751751

752752
* {string} The request path.
753753

754+
### `request.method`
755+
<!-- YAML
756+
added: v0.1.97
757+
-->
758+
759+
* {string} The request method.
760+
761+
### `request.host`
762+
<!-- YAML
763+
added: REPLACEME
764+
-->
765+
766+
* {string} The request host.
767+
768+
### `request.protocol`
769+
<!-- YAML
770+
added: REPLACEME
771+
-->
772+
773+
* {string} The request protocol.
774+
754775
### `request.removeHeader(name)`
755776
<!-- YAML
756777
added: v1.6.0

lib/_http_client.js

+2
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ function ClientRequest(input, options, cb) {
221221
this.parser = null;
222222
this.maxHeadersCount = null;
223223
this.reusedSocket = false;
224+
this.host = host;
225+
this.protocol = protocol;
224226

225227
let called = false;
226228

test/parallel/test-http-outgoing-properties.js

+23
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,26 @@ const OutgoingMessage = http.OutgoingMessage;
5151
msg.write('asd');
5252
assert.strictEqual(msg.writableLength, 7);
5353
}
54+
55+
{
56+
const server = http.createServer((req, res) => {
57+
res.end();
58+
server.close();
59+
});
60+
61+
server.listen(0);
62+
63+
server.on('listening', common.mustCall(() => {
64+
const req = http.request({
65+
port: server.address().port,
66+
method: 'GET',
67+
path: '/'
68+
});
69+
70+
assert.strictEqual(req.path, '/');
71+
assert.strictEqual(req.method, 'GET');
72+
assert.strictEqual(req.host, 'localhost');
73+
assert.strictEqual(req.protocol, 'http:');
74+
req.end();
75+
}));
76+
}

0 commit comments

Comments
 (0)