Skip to content

Commit dc71a3b

Browse files
paulmelnikowgr2m
authored andcommitted
feat(request_overrider): Set IncomingMessage.client for parity with real requests
`ClientMessage.socket` is an instance of our `socket` class. As that class’ constructor sets `authorized` to `true` on `https` requests, the code here that sets `response.socket.authorized` is pointless. `ClientMessage.client` was deprecated in iojs 2.2 and per request/request#1615 but is still aliased: https://github.com/nodejs/node/blob/2e613a9c301165d121b19b86e382860323abc22f/lib/_http_incoming.js#L67 The old code, which sets `response.client.authorized`, is being replaced with new code which aliases `response.client`. This ensures that `response.client` and `response.socket` point to the same thing, which provides parity with a non-overridden request. This may help with compatibility with libraries which depend on that undocumented property, such as some very old versions of `request`. This also ensures that `response.client.authorized` remains set, without needing to set it directly. This is being changed now to remove two bits of unreachable code in the conditional.
1 parent 6d2a312 commit dc71a3b

File tree

3 files changed

+13
-11
lines changed

3 files changed

+13
-11
lines changed

lib/request_overrider.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -471,17 +471,17 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
471471
return
472472
}
473473

474-
/// response.client.authorized = true
475-
/// fixes https://github.com/pgte/nock/issues/158
476-
response.client = _.extend(response.client || {}, {
477-
authorized: true,
478-
})
479-
480-
// Account for updates to Node.js response interface
481-
// cf https://github.com/request/request/pull/1615
482-
response.socket = _.extend(response.socket || {}, {
483-
authorized: true,
484-
})
474+
// `IncomingMessage.client` is an undocumented alias for
475+
// `IncomingMessage.socket`. Assigning it here may help with
476+
// compatibility, including with very old versions of `request` which
477+
// inspect `response.client.authorized`. Modern versions of request
478+
// inspect `response.socket.authorized` which is set to true in our
479+
// `Socket` constructor.
480+
// https://github.com/pgte/nock/issues/158
481+
// https://github.com/request/request/pull/1615
482+
// https://nodejs.org/api/http.html#http_response_socket
483+
// https://github.com/nodejs/node/blob/2e613a9c301165d121b19b86e382860323abc22f/lib/_http_incoming.js#L67
484+
response.client = response.socket
485485

486486
// Evaluate functional headers.
487487
const evaluatedHeaders = {}

lib/socket.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function Socket(options) {
1414
EventEmitter.apply(this)
1515

1616
if (options.proto === 'https') {
17+
// https://github.com/nock/nock/issues/158
1718
this.authorized = true
1819
}
1920

tests/test_intercept.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3828,6 +3828,7 @@ test('mocking succeeds even when host request header is not specified', t => {
38283828
)
38293829
})
38303830

3831+
// https://github.com/nock/nock/issues/158
38313832
test('mikeal/request with strictSSL: true', t => {
38323833
nock('https://strictssl.com')
38333834
.post('/what')

0 commit comments

Comments
 (0)