Skip to content

Commit

Permalink
http: expose host and protocol on ClientRequest
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
wenningplus authored and codebytere committed Jun 27, 2020
1 parent ed26e8e commit a24b8df
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
21 changes: 21 additions & 0 deletions doc/api/http.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,6 +751,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
Expand Down
2 changes: 2 additions & 0 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ function ClientRequest(input, options, cb) {
this.parser = null;
this.maxHeadersCount = null;
this.reusedSocket = false;
this.host = host;
this.protocol = protocol;

let called = false;

Expand Down
23 changes: 23 additions & 0 deletions test/parallel/test-http-outgoing-properties.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}));
}

0 comments on commit a24b8df

Please sign in to comment.