Skip to content

Commit

Permalink
fix: Fix .matchHeader() with allowUnmocked (#1480)
Browse files Browse the repository at this point in the history
- Move header checks into `match()`.
- Rename internal method `matchIndependentOfBody()` to `matchAddress()`
  • Loading branch information
ww-daniel-mora authored and paulmelnikow committed Apr 15, 2019
1 parent 7e44769 commit d6667f0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 29 deletions.
4 changes: 2 additions & 2 deletions lib/intercept.js
Expand Up @@ -173,7 +173,7 @@ function interceptorsFor(options) {
matchingInterceptor = [
{
options: { allowUnmocked: true },
matchIndependentOfBody: function() {
matchAddress() {
return false
},
},
Expand Down Expand Up @@ -412,7 +412,7 @@ function activate() {
let allowUnmocked = false

matches = !!_.find(interceptors, function(interceptor) {
return interceptor.matchIndependentOfBody(options)
return interceptor.matchAddress(options)
})

allowUnmocked = !!_.find(interceptors, function(interceptor) {
Expand Down
40 changes: 13 additions & 27 deletions lib/interceptor.js
Expand Up @@ -227,19 +227,18 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
body = this.scope.transformRequestBodyFunction(body, this._requestBody)
}

const checkHeaders = function(header) {
if (_.isFunction(header.value)) {
return header.value(options.getHeader(header.name))
const requestMatchesFilter = ({ name, value: predicate }) => {
const headerValue = options.getHeader(name)
if (typeof predicate === 'function') {
return predicate(headerValue)
} else {
return common.matchStringOrRegexp(headerValue, predicate)
}
return common.matchStringOrRegexp(
options.getHeader(header.name),
header.value
)
}

if (
!this.scope.matchHeaders.every(checkHeaders) ||
!this.interceptorMatchHeaders.every(checkHeaders)
!this.scope.matchHeaders.every(requestMatchesFilter) ||
!this.interceptorMatchHeaders.every(requestMatchesFilter)
) {
this.scope.logger("headers don't match")
return false
Expand Down Expand Up @@ -375,9 +374,11 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
return matches
}

Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(
options
) {
/**
* Return true when the interceptor's method, protocol, host, port, and path
* match the provided options.
*/
Interceptor.prototype.matchAddress = function matchAddress(options) {
const isRegex = _.isRegExp(this.path)
const isRegexBasePath = _.isRegExp(this.scope.basePath)

Expand All @@ -393,21 +394,6 @@ Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(
if (this.scope.transformPathFunction) {
path = this.scope.transformPathFunction(path)
}

const checkHeaders = function(header) {
return (
options.getHeader &&
common.matchStringOrRegexp(options.getHeader(header.name), header.value)
)
}

if (
!this.scope.matchHeaders.every(checkHeaders) ||
!this.interceptorMatchHeaders.every(checkHeaders)
) {
return false
}

const comparisonKey = isRegex ? this.__nock_scopeKey : this._key
const matchKey = `${method} ${proto}://${options.host}${path}`

Expand Down
32 changes: 32 additions & 0 deletions tests/test_header_matching.js
Expand Up @@ -118,6 +118,21 @@ test('match header on scope with function: matches when match accepted', async t
scope.done()
})

test('match header on scope with function and allow unmocked: matches when match accepted', async t => {
const scope = nock('http://example.test', { allowUnmocked: true })
.get('/')
.matchHeader('x-my-headers', val => true)
.reply(200, 'Hello World!')

const { statusCode, body } = await got('http://example.test/', {
headers: { 'X-My-Headers': 456 },
})

t.equal(statusCode, 200)
t.equal(body, 'Hello World!')
scope.done()
})

test('match header on scope with function: does not match when match declined', async t => {
nock('http://example.test')
.get('/')
Expand Down Expand Up @@ -192,6 +207,23 @@ test('match header on interceptor with function: matches when match accepted', a
scope.done()
})

test('match header on interceptor with function: matches when match accepted', async t => {
const scope = nock('http://example.test', { allowUnmocked: true })
.matchHeader('x-my-headers', val => true)
// `.matchHeader()` is called on the interceptor. It precedes the call to
// `.get()`.
.get('/')
.reply(200, 'Hello World!')

const { statusCode, body } = await got('http://example.test/', {
headers: { 'X-My-Headers': 456 },
})

t.equal(statusCode, 200)
t.equal(body, 'Hello World!')
scope.done()
})

test('match header on interceptor with function: does not match when match declined', async t => {
nock('http://example.test')
.matchHeader('x-my-headers', val => false)
Expand Down

0 comments on commit d6667f0

Please sign in to comment.