Skip to content

Commit

Permalink
refactor: default options on recorder, remove dead code (#1641)
Browse files Browse the repository at this point in the history
* chore: update some docstrs

* refactor(recorder): clean up default options using spread and deconstructing syntax

* refactor: remove unnecessary lodash usage

For #1285

* refactor(interceptor): remove dead code
  • Loading branch information
mastermatt authored and gr2m committed Sep 4, 2019
1 parent 90fff26 commit 3f7490a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
5 changes: 5 additions & 0 deletions lib/intercept.js
Expand Up @@ -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)

Expand Down
25 changes: 11 additions & 14 deletions lib/interceptor.js
Expand Up @@ -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
Expand Down Expand Up @@ -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
}

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

Expand All @@ -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()
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
51 changes: 29 additions & 22 deletions 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')

Expand All @@ -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')),
}
}

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

0 comments on commit 3f7490a

Please sign in to comment.