Skip to content

Commit

Permalink
Add comments and tests for socket aliases.
Browse files Browse the repository at this point in the history
  • Loading branch information
mastermatt committed Jun 20, 2019
1 parent e1fe113 commit d37cf74
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@ function RequestOverrider(req, options, interceptors, remove, cb) {
return getHeader(req, name)
}

// ClientRequest.connection is an alias for ClientRequest.socket
// IncomingMessage.connection is an alias for IncomingMessage.socket
// The same Socket is shared between the request and response to mimic native behavior.
req.socket = req.connection = response.socket = response.connection = Socket({
proto: options.proto,
})
Expand Down
15 changes: 15 additions & 0 deletions tests/test_request_overrider.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,21 @@ test('request emits socket', t => {
})
})

test('socket is shared and aliased correctly', t => {
nock('http://example.test')
.get('/')
.reply()

const req = http.get('http://example.test')

req.once('response', function(res) {
t.is(req.socket, req.connection)
t.is(req.socket, res.socket)
t.is(res.socket, res.connection)
t.end()
})
})

test('socket emits connect and secureConnect', t => {
t.plan(3)

Expand Down

0 comments on commit d37cf74

Please sign in to comment.