Skip to content

Commit

Permalink
refactor(interceptor): separate hostname matching
Browse files Browse the repository at this point in the history
  • Loading branch information
mastermatt authored and gr2m committed Jul 17, 2019
1 parent 2a54482 commit 60a055b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
11 changes: 5 additions & 6 deletions lib/interceptor.js
Expand Up @@ -224,16 +224,11 @@ Interceptor.prototype.reqheaderMatches = function reqheaderMatches(
return false
}

Interceptor.prototype.match = function match(options, body, hostNameOnly) {
Interceptor.prototype.match = function match(options, body) {
if (debug.enabled) {
debug('match %s, body = %s', stringify(options), stringify(body))
}

// TODO move this use case to its own method
if (hostNameOnly) {
return options.hostname === this.scope.urlParts.hostname
}

const method = (options.method || 'GET').toUpperCase()
let { path } = options
let matches
Expand Down Expand Up @@ -427,6 +422,10 @@ Interceptor.prototype.matchAddress = function matchAddress(options) {
return comparisonKey === matchKey
}

Interceptor.prototype.matchHostName = function matchHostName(options) {
return options.hostname === this.scope.urlParts.hostname
}

Interceptor.prototype.filteringPath = function filteringPath(...args) {
this.scope.filteringPath(...args)
return this
Expand Down
30 changes: 14 additions & 16 deletions lib/request_overrider.js
Expand Up @@ -213,7 +213,7 @@ function RequestOverrider(req, options, interceptors, remove) {
const end = function() {
debug('ending')
ended = true
let requestBody, responseBody, responseBuffers, interceptor
let requestBody, responseBody, responseBuffers

let continued = false

Expand Down Expand Up @@ -241,24 +241,22 @@ function RequestOverrider(req, options, interceptors, remove) {
setRequestHeaders(req, options, interceptor)
})

interceptor = _.find(interceptors, function(interceptor) {
return interceptor.match(options, requestBody)
})
const interceptor = interceptors.find(i => i.match(options, requestBody))

if (!interceptor) {
globalEmitter.emit('no match', req, options, requestBody)
// Try to find a hostname match
interceptor = _.find(interceptors, function(interceptor) {
return interceptor.match(options, requestBody, true)
})
if (interceptor && req instanceof ClientRequest) {
if (interceptor.options.allowUnmocked) {
const newReq = new ClientRequest(options)
propagate(newReq, req)
// We send the raw buffer as we received it, not as we interpreted it.
newReq.end(requestBodyBuffer)
return
}

// Try to find a hostname match that allows unmocked
const allowUnmocked = interceptors.some(
i => i.matchHostName(options) && i.options.allowUnmocked
)

if (allowUnmocked && req instanceof ClientRequest) {
const newReq = new ClientRequest(options)
propagate(newReq, req)
// We send the raw buffer as we received it, not as we interpreted it.
newReq.end(requestBodyBuffer)
return
}

const err = new Error(
Expand Down

0 comments on commit 60a055b

Please sign in to comment.