From 8519af162dd0cd0edee1602c5fda100cc664e1d4 Mon Sep 17 00:00:00 2001 From: Richard Littauer Date: Fri, 18 Jan 2019 16:09:11 -0500 Subject: [PATCH] tests: Add tests for lib/recorder.getScope (#1381) I also added a test to check for alternative ports. Part of #1374. --- lib/common.js | 2 +- lib/recorder.js | 15 ++++++++------- tests/test_recorder.js | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 8 deletions(-) diff --git a/lib/common.js b/lib/common.js index 7f7a6fd7b..cc9f74bb8 100644 --- a/lib/common.js +++ b/lib/common.js @@ -9,7 +9,7 @@ const debug = require('debug')('nock.common') * @param {Object} options - a parsed options object of the request */ const normalizeRequestOptions = function(options) { - options.proto = options.proto || (options._https_ ? 'https' : 'http') + options.proto = options.proto || 'http' options.port = options.port || (options.proto === 'http' ? 80 : 443) if (options.host) { debug('options.host:', options.host) diff --git a/lib/recorder.js b/lib/recorder.js index 798b5c629..772860042 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -17,7 +17,7 @@ function getScope(options) { common.normalizeRequestOptions(options) const scope = [] - if (options._https_) { + if (options.proto === 'https') { scope.push('https://') } else { scope.push('http://') @@ -25,14 +25,11 @@ function getScope(options) { scope.push(options.host) - // If a non-standard port wasn't specified in options.host, include it from - // options.port. - // TODO-coverage: Add a test of this case. if ( options.host.indexOf(':') === -1 && options.port && - ((options._https_ && options.port.toString() !== '443') || - (!options._https_ && options.port.toString() !== '80')) + ((options.proto && options.port.toString() !== '443') || + (!options.proto && options.port.toString() !== '80')) ) { scope.push(':') scope.push(options.port) @@ -386,8 +383,12 @@ function record(rec_options) { debug('finished setting up intercepting') + // We override both the http and the https modules; when we are + // serializing the request, we need to know which was called. + // By stuffing the state, we can make sure that nock records + // the intended protocol. if (proto === 'https') { - options._https_ = true + options.proto = 'https' } }) diff --git a/tests/test_recorder.js b/tests/test_recorder.js index 037a54c3c..96c9b48c7 100644 --- a/tests/test_recorder.js +++ b/tests/test_recorder.js @@ -16,6 +16,42 @@ test('setup', function(t) { t.end() }) +test('when request port is different, use the alternate port', function(t) { + const server = http.createServer((request, response) => response.end()) + t.once('end', () => server.close()) + + nock.restore() + nock.recorder.clear() + nock.recorder.rec(true) + + server.listen( + { + port: 4302, + }, + () => { + http + .request( + { + method: 'POST', + host: 'localhost', + path: '/', + port: 4302, + }, + res => { + res.resume() + res.once('end', () => { + const recorded = nock.recorder.play() + t.equal(recorded.length, 1) + t.true(recorded[0].indexOf('localhost:4302') !== -1) + t.end() + }) + } + ) + .end() + } + ) +}) + test('recording turns off nock interception (backward compatibility behavior)', function(t) { // We ensure that there are no overrides. nock.restore()