diff --git a/lib/common.js b/lib/common.js index 59f075928..cbfb6fc38 100644 --- a/lib/common.js +++ b/lib/common.js @@ -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 } diff --git a/tests/test_common.js b/tests/test_common.js index 807bca867..a86abdda5 100644 --- a/tests/test_common.js +++ b/tests/test_common.js @@ -512,3 +512,30 @@ test('testing timers are deleted correctly', t => { t.end() }) }) + +test('test correct node behavior by making sure that http.request is not called when using http.get', t => { + const scope = nock('http://example.test') + .get('/') + .reply() + + const reqSpy = sinon.spy(http.request) + const origHttpReq = http.request + + http.request = reqSpy + + http.get('http://example.test', res => { + t.equal(res.statusCode, 200) + + res.on('data', reqSpy) + + res.on('end', () => { + t.equal(reqSpy.called, false) + t.end() + }) + }) + + t.on('end', () => { + scope.done() + http.request = origHttpReq + }) +})