diff --git a/lib/intercept.js b/lib/intercept.js index 3fdeaefe7..1bf368f2a 100644 --- a/lib/intercept.js +++ b/lib/intercept.js @@ -134,6 +134,11 @@ function removeAll() { allInterceptors = {} } +/** + * Return all the Interceptors whose Scopes match against the base path of the provided options. + * + * @returns {Interceptor[]} + */ function interceptorsFor(options) { common.normalizeRequestOptions(options) diff --git a/lib/interceptor.js b/lib/interceptor.js index b1b41f131..ed6e30744 100644 --- a/lib/interceptor.js +++ b/lib/interceptor.js @@ -58,10 +58,10 @@ function Interceptor(scope, uri, method, requestBody, interceptorOptions) { // We use lower-case header field names throughout Nock. this.reqheaders = common.headersFieldNamesToLowerCase( - (scope.scopeOptions && scope.scopeOptions.reqheaders) || {} + scope.scopeOptions.reqheaders || {} ) this.badheaders = common.headersFieldsArrayToLowerCase( - (scope.scopeOptions && scope.scopeOptions.badheaders) || [] + scope.scopeOptions.badheaders || [] ) this.delayInMs = 0 @@ -93,7 +93,7 @@ Interceptor.prototype.replyWithError = function replyWithError(errorMessage) { _.defaults(this.options, this.scope.scopeOptions) - this.scope.add(this._key, this, this.scope, this.scopeOptions) + this.scope.add(this._key, this) return this.scope } @@ -169,7 +169,7 @@ Interceptor.prototype.reply = function reply(statusCode, body, rawHeaders) { this.body = body - this.scope.add(this._key, this, this.scope, this.scopeOptions) + this.scope.add(this._key, this) return this.scope } @@ -195,6 +195,7 @@ Interceptor.prototype.reqheaderMatches = function reqheaderMatches( ) { const reqHeader = this.reqheaders[key] let header = options.headers[key] + if (header && typeof header !== 'string' && header.toString) { header = header.toString() } @@ -260,19 +261,15 @@ Interceptor.prototype.match = function match(options, body) { } if ( - this.scope.scopeOptions && this.scope.scopeOptions.conditionally && !this.scope.scopeOptions.conditionally() ) { return false } - function reqheaderContains(header) { - return _.has(options.headers, header) - } - - const reqContainsBadHeaders = - this.badheaders && _.some(this.badheaders, reqheaderContains) + const reqContainsBadHeaders = this.badheaders.some( + header => header in options.headers + ) if (reqContainsBadHeaders) { return false @@ -528,7 +525,7 @@ Interceptor.prototype.times = function times(newCounter) { * @see {@link times} * @public * @example - * nock('http://zombo.com).get('/').once.reply(200, 'Ok'); + * nock('http://zombo.com).get('/').once().reply(200, 'Ok'); */ Interceptor.prototype.once = function once() { return this.times(1) @@ -540,7 +537,7 @@ Interceptor.prototype.once = function once() { * @see {@link times} * @public * @example - * nock('http://zombo.com).get('/').twice.reply(200, 'Ok'); + * nock('http://zombo.com).get('/').twice().reply(200, 'Ok'); */ Interceptor.prototype.twice = function twice() { return this.times(2) @@ -552,7 +549,7 @@ Interceptor.prototype.twice = function twice() { * @see {@link times} * @public * @example - * nock('http://zombo.com).get('/').thrice.reply(200, 'Ok'); + * nock('http://zombo.com).get('/').thrice().reply(200, 'Ok'); */ Interceptor.prototype.thrice = function thrice() { return this.times(3) diff --git a/lib/recorder.js b/lib/recorder.js index 6bcc501a8..df1b12d2e 100644 --- a/lib/recorder.js +++ b/lib/recorder.js @@ -1,7 +1,6 @@ 'use strict' const debug = require('debug')('nock.recorder') -const _ = require('lodash') const qs = require('qs') const { inspect } = require('util') @@ -27,7 +26,7 @@ const getBodyFromChunks = function(chunks, headers) { // of hex strings so that the responses can be mocked one by one. if (headers && common.isContentEncoded(headers)) { return { - body: _.map(chunks, chunk => chunk.toString('hex')), + body: chunks.map(chunk => chunk.toString('hex')), } } @@ -174,13 +173,15 @@ function generateRequestAndResponse( // to finish (which is the point of the test). let currentRecordingId = 0 -function record(recOptions) { - // Set the new current recording ID and capture its value in this instance of record(). - currentRecordingId = currentRecordingId + 1 - const thisRecordingId = currentRecordingId - - debug('start recording', thisRecordingId, JSON.stringify(recOptions)) +const defaultRecordOptions = { + dont_print: false, + enable_reqheaders_recording: false, + logging: console.log, + output_objects: false, + use_separator: true, +} +function record(recOptions) { // Trying to start recording with recording already in progress implies an error // in the recording configuration (double recording makes no sense and used to lead // to duplicates in output) @@ -190,22 +191,28 @@ function record(recOptions) { recordingInProgress = true - // Originally the parameters was a dont_print boolean flag. - // To keep the existing code compatible we take that case into account. - const optionsIsObject = typeof recOptions === 'object' - const dontPrint = - (typeof recOptions === 'boolean' && recOptions) || - (optionsIsObject && recOptions.dont_print) - const outputObjects = optionsIsObject && recOptions.output_objects - const enableReqHeadersRecording = - optionsIsObject && recOptions.enable_reqheaders_recording - // eslint-disable-next-line no-console - const logging = (optionsIsObject && recOptions.logging) || console.log - let useSeparator = true - if (optionsIsObject && _.has(recOptions, 'use_separator')) { - useSeparator = recOptions.use_separator + // Set the new current recording ID and capture its value in this instance of record(). + currentRecordingId = currentRecordingId + 1 + const thisRecordingId = currentRecordingId + + // Originally the parameter was a dont_print boolean flag. + // To keep the existing code compatible we take that case into account. + if (typeof recOptions === 'boolean') { + recOptions = { dont_print: recOptions } } + recOptions = { ...defaultRecordOptions, ...recOptions } + + debug('start recording', thisRecordingId, recOptions) + + const { + dont_print: dontPrint, + enable_reqheaders_recording: enableReqHeadersRecording, + logging, + output_objects: outputObjects, + use_separator: useSeparator, + } = recOptions + debug(thisRecordingId, 'restoring overridden requests before new overrides') // To preserve backward compatibility (starting recording wasn't throwing if nock was already active) // we restore any requests that may have been overridden by other parts of nock (e.g. intercept)