Skip to content

Commit

Permalink
Merge branch 'master' into node-http
Browse files Browse the repository at this point in the history
  • Loading branch information
kennethaasan committed Jan 9, 2020
2 parents 931d92c + c7363e5 commit 6b9303f
Show file tree
Hide file tree
Showing 15 changed files with 1,386 additions and 450 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -29,7 +29,7 @@ Object.assign(module.exports, {
disableNetConnect,
enableNetConnect,
// TODO-12.x Historically `nock.cleanAll()` has returned the nock global.
// The other global methods do noto do this, so it's not clear this was
// The other global methods do not do this, so it's not clear this was
// deliberate or is even helpful. This shim is included for backward
// compatibility and shoulud be replaced with an alias to `removeAll()`.
cleanAll() {
Expand Down
6 changes: 3 additions & 3 deletions lib/back.js
Expand Up @@ -71,7 +71,7 @@ function Back(fixtureName, options, nockedFn) {
} else if (arguments.length === 2) {
// If 2nd parameter is a function then `options` has been omitted
// otherwise `options` haven't been omitted but `nockedFn` was.
if (options instanceof Function) {
if (typeof options === 'function') {
nockedFn = options
options = {}
}
Expand All @@ -89,7 +89,7 @@ function Back(fixtureName, options, nockedFn) {
debug('context:', context)

// If nockedFn is a function then invoke it, otherwise return a promise resolving to nockDone.
if (nockedFn instanceof Function) {
if (typeof nockedFn === 'function') {
nockedFn.call(context, nockDone)
} else {
return Promise.resolve({ nockDone, context })
Expand Down Expand Up @@ -170,7 +170,7 @@ const record = {
if (context.isRecording) {
let outputs = recorder.outputs()

if (options.afterRecord instanceof Function) {
if (typeof options.afterRecord === 'function') {
outputs = options.afterRecord(outputs)
}

Expand Down
6 changes: 3 additions & 3 deletions lib/common.js
Expand Up @@ -319,7 +319,7 @@ const noDuplicatesHeaders = new Set([
*/
function addHeaderLine(headers, name, value) {
let values // code below expects `values` to be an array of strings
if (value instanceof Function) {
if (typeof value === 'function') {
// Function values are evaluated towards the end of the response, before that we use a placeholder
// string just to designate that the header exists. Useful when `Content-Type` is set with a function.
values = [value.name]
Expand Down Expand Up @@ -480,7 +480,7 @@ function isStream(obj) {
obj &&
typeof obj !== 'string' &&
!Buffer.isBuffer(obj) &&
obj.setEncoding instanceof Function
typeof obj.setEncoding === 'function'
)
}

Expand All @@ -504,7 +504,7 @@ function normalizeClientRequestArgs(input, options, cb) {
input = null
}

if (options instanceof Function) {
if (typeof options === 'function') {
cb = options
options = input || {}
} else {
Expand Down
4 changes: 3 additions & 1 deletion lib/intercept.js
Expand Up @@ -160,7 +160,9 @@ function interceptorsFor(options) {

// Keep the filtered scope (its key) to signal the rest of the module
// that this wasn't an exact but filtered match.
interceptor.__nock_filteredScope = interceptor.__nock_scopeKey
interceptors.forEach(ic => {
ic.__nock_filteredScope = ic.__nock_scopeKey
})
return interceptors
}
}
Expand Down
12 changes: 8 additions & 4 deletions lib/intercepted_request_router.js
Expand Up @@ -113,7 +113,11 @@ class InterceptedRequestRouter {
}
this.requestBodyBuffers.push(buffer)
}
if (callback instanceof Function) {
// can't use instanceof Function because some test runners
// run tests in vm.runInNewContext where Function is not same
// as that in the current context
// https://github.com/nock/nock/pull/1754#issuecomment-571531407
if (typeof callback === 'function') {
callback()
}
} else {
Expand All @@ -131,17 +135,17 @@ class InterceptedRequestRouter {
debug('req.end')
const { req } = this

if (chunk instanceof Function) {
if (typeof chunk === 'function') {
callback = chunk
chunk = null
} else if (encoding instanceof Function) {
} else if (typeof encoding === 'function') {
callback = encoding
encoding = null
}

if (!req.aborted && !this.playbackStarted) {
req.write(chunk, encoding, () => {
if (callback instanceof Function) {
if (typeof callback === 'function') {
callback()
}
this.startPlayback()
Expand Down
14 changes: 7 additions & 7 deletions lib/interceptor.js
Expand Up @@ -110,7 +110,7 @@ module.exports = class Interceptor {

reply(statusCode, body, rawHeaders) {
// support the format of only passing in a callback
if (statusCode instanceof Function) {
if (typeof statusCode === 'function') {
if (arguments.length > 1) {
// It's not very Javascript-y to throw an error for extra args to a function, but because
// of legacy behavior, this error was added to reduce confusion for those migrating.
Expand All @@ -126,7 +126,7 @@ module.exports = class Interceptor {
}

this.statusCode = statusCode || 200
if (body instanceof Function) {
if (typeof body === 'function') {
this.replyFunction = body
body = null
}
Expand Down Expand Up @@ -215,7 +215,7 @@ module.exports = class Interceptor {
}

if (reqHeader !== undefined && header !== undefined) {
if (reqHeader instanceof Function) {
if (typeof reqHeader === 'function') {
return reqHeader(header)
} else if (common.matchStringOrRegexp(header, reqHeader)) {
return true
Expand Down Expand Up @@ -250,7 +250,7 @@ module.exports = class Interceptor {

const requestMatchesFilter = ({ name, value: predicate }) => {
const headerValue = req.getHeader(name)
if (predicate instanceof Function) {
if (typeof predicate === 'function') {
return predicate(headerValue)
} else {
return common.matchStringOrRegexp(headerValue, predicate)
Expand Down Expand Up @@ -317,7 +317,7 @@ module.exports = class Interceptor {
matchKey = common.normalizeOrigin(proto, options.host, options.port)
}

if (this.uri instanceof Function) {
if (typeof this.uri === 'function') {
matches =
common.matchStringOrRegexp(matchKey, this.basePath) &&
// This is a false positive, as `uri` is not bound to `this`.
Expand Down Expand Up @@ -397,7 +397,7 @@ module.exports = class Interceptor {
debug('Interceptor queries: %j', this.queries)
debug(' Request queries: %j', reqQueries)

if (this.queries instanceof Function) {
if (typeof this.queries === 'function') {
return this.queries(reqQueries)
}

Expand Down Expand Up @@ -458,7 +458,7 @@ module.exports = class Interceptor {
return this
}

if (queries instanceof Function) {
if (typeof queries === 'function') {
this.queries = queries
return this
}
Expand Down
4 changes: 2 additions & 2 deletions lib/match_body.js
Expand Up @@ -26,7 +26,7 @@ module.exports = function matchBody(options, spec, body) {

// try to transform body to json
let json
if (typeof spec === 'object' || spec instanceof Function) {
if (typeof spec === 'object' || typeof spec === 'function') {
try {
json = JSON.parse(body)
} catch (err) {
Expand All @@ -39,7 +39,7 @@ module.exports = function matchBody(options, spec, body) {
}
}

if (spec instanceof Function) {
if (typeof spec === 'function') {
return spec.call(options, body)
}

Expand Down
2 changes: 1 addition & 1 deletion lib/playback_interceptor.js
Expand Up @@ -270,7 +270,7 @@ function playbackInterceptor({

// Evaluate functional headers.
common.forEachHeader(response.rawHeaders, (value, fieldName, i) => {
if (value instanceof Function) {
if (typeof value === 'function') {
response.rawHeaders[i + 1] = value(req, response, responseBody)
}
})
Expand Down
4 changes: 2 additions & 2 deletions lib/recorder.js
Expand Up @@ -344,10 +344,10 @@ function record(recOptions) {
const oldEnd = req.end
req.end = function(chunk, encoding, callback) {
debug('req.end')
if (chunk instanceof Function) {
if (typeof chunk === 'function') {
callback = chunk
chunk = null
} else if (encoding instanceof Function) {
} else if (typeof encoding === 'function') {
callback = encoding
encoding = null
}
Expand Down

0 comments on commit 6b9303f

Please sign in to comment.