Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Correct behavior when other libraries override http.get and http.request #1868

Merged
merged 3 commits into from Feb 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/common.js
Expand Up @@ -99,7 +99,11 @@ function overrideRequests(newRequest) {
}
// https://nodejs.org/api/http.html#http_http_get_options_callback
module.get = function(input, options, callback) {
const req = module.request(input, options, callback)
const req = newRequest(proto, overriddenGet.bind(module), [
input,
options,
callback,
])
req.end()
return req
}
Expand Down
57 changes: 57 additions & 0 deletions tests/test_request_overrider.js
Expand Up @@ -684,3 +684,60 @@ test('Request with `Expect: 100-continue` triggers continue event', t => {
req.end(exampleRequestBody)
})
})

// https://github.com/nock/nock/issues/1836
test('when http.get and http.request have been overridden before nock overrides them, http.get calls through to the expected method', async t => {
// TODO Investigate why this is needed when it's also in the `afterEach()`
// hook in ./setup.
t.on('end', () => {
nock.restore()
sinon.restore()
})

// Obtain the original `http.request()` and stub it out, as a library might.
nock.restore()
const overriddenRequest = sinon.stub(http, 'request').callThrough()
const overriddenGet = sinon.stub(http, 'get').callThrough()

// Let Nock override them again.
nock.activate()

const server = http.createServer((request, response) => {
response.writeHead(200)
response.end()
})
await new Promise(resolve => server.listen(resolve))

const req = http.get(`http://localhost:${server.address().port}`)
expect(overriddenGet).to.have.been.calledOnce()
expect(overriddenRequest).not.to.have.been.called()

req.abort()
server.close()
})

// https://github.com/nock/nock/issues/1836
test('when http.get and http.request have been overridden before nock overrides them, http.request calls through to the expected method', async t => {
t.on('end', () => {
nock.restore()
sinon.restore()
})

// Obtain the original `http.request()` and stub it out, as a library might.
nock.restore()
const overriddenRequest = sinon.stub(http, 'request').callThrough()
const overriddenGet = sinon.stub(http, 'get').callThrough()

// Let Nock override them again.
nock.activate()

const req = http.request({
host: 'localhost',
path: '/',
port: 1234,
})
expect(overriddenRequest).to.have.been.calledOnce()
expect(overriddenGet).not.to.have.been.called()

req.abort()
})