From 17ad3608b9e7f1793ae9fdb6881a85de94358696 Mon Sep 17 00:00:00 2001 From: Kenneth Aasan Date: Fri, 10 Jan 2020 15:48:56 -0800 Subject: [PATCH] #1836 fixes expected Node behaviour for http.get --- lib/common.js | 6 +++++- tests/test_common.js | 27 +++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/common.js b/lib/common.js index 995a06239..30d8340f8 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..c786f7368 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('correct node behavior', 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 + }) +})