Skip to content

Commit

Permalink
tests: Add tests for lib/recorder.getScope (#1381)
Browse files Browse the repository at this point in the history
I also added a test to check for alternative ports.

Part of #1374.
  • Loading branch information
RichardLitt authored and gr2m committed Sep 4, 2019
1 parent ca0ed16 commit 8519af1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/common.js
Expand Up @@ -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)
Expand Down
15 changes: 8 additions & 7 deletions lib/recorder.js
Expand Up @@ -17,22 +17,19 @@ function getScope(options) {
common.normalizeRequestOptions(options)

const scope = []
if (options._https_) {
if (options.proto === 'https') {
scope.push('https://')
} else {
scope.push('http://')
}

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)
Expand Down Expand Up @@ -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'
}
})

Expand Down
36 changes: 36 additions & 0 deletions tests/test_recorder.js
Expand Up @@ -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()
Expand Down

0 comments on commit 8519af1

Please sign in to comment.