Skip to content

Commit d6667f0

Browse files
ww-daniel-morapaulmelnikow
authored andcommitted
fix: Fix .matchHeader() with allowUnmocked (#1480)
- Move header checks into `match()`. - Rename internal method `matchIndependentOfBody()` to `matchAddress()`
1 parent 7e44769 commit d6667f0

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

lib/intercept.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ function interceptorsFor(options) {
173173
matchingInterceptor = [
174174
{
175175
options: { allowUnmocked: true },
176-
matchIndependentOfBody: function() {
176+
matchAddress() {
177177
return false
178178
},
179179
},
@@ -412,7 +412,7 @@ function activate() {
412412
let allowUnmocked = false
413413

414414
matches = !!_.find(interceptors, function(interceptor) {
415-
return interceptor.matchIndependentOfBody(options)
415+
return interceptor.matchAddress(options)
416416
})
417417

418418
allowUnmocked = !!_.find(interceptors, function(interceptor) {

lib/interceptor.js

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -227,19 +227,18 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
227227
body = this.scope.transformRequestBodyFunction(body, this._requestBody)
228228
}
229229

230-
const checkHeaders = function(header) {
231-
if (_.isFunction(header.value)) {
232-
return header.value(options.getHeader(header.name))
230+
const requestMatchesFilter = ({ name, value: predicate }) => {
231+
const headerValue = options.getHeader(name)
232+
if (typeof predicate === 'function') {
233+
return predicate(headerValue)
234+
} else {
235+
return common.matchStringOrRegexp(headerValue, predicate)
233236
}
234-
return common.matchStringOrRegexp(
235-
options.getHeader(header.name),
236-
header.value
237-
)
238237
}
239238

240239
if (
241-
!this.scope.matchHeaders.every(checkHeaders) ||
242-
!this.interceptorMatchHeaders.every(checkHeaders)
240+
!this.scope.matchHeaders.every(requestMatchesFilter) ||
241+
!this.interceptorMatchHeaders.every(requestMatchesFilter)
243242
) {
244243
this.scope.logger("headers don't match")
245244
return false
@@ -375,9 +374,11 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
375374
return matches
376375
}
377376

378-
Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(
379-
options
380-
) {
377+
/**
378+
* Return true when the interceptor's method, protocol, host, port, and path
379+
* match the provided options.
380+
*/
381+
Interceptor.prototype.matchAddress = function matchAddress(options) {
381382
const isRegex = _.isRegExp(this.path)
382383
const isRegexBasePath = _.isRegExp(this.scope.basePath)
383384

@@ -393,21 +394,6 @@ Interceptor.prototype.matchIndependentOfBody = function matchIndependentOfBody(
393394
if (this.scope.transformPathFunction) {
394395
path = this.scope.transformPathFunction(path)
395396
}
396-
397-
const checkHeaders = function(header) {
398-
return (
399-
options.getHeader &&
400-
common.matchStringOrRegexp(options.getHeader(header.name), header.value)
401-
)
402-
}
403-
404-
if (
405-
!this.scope.matchHeaders.every(checkHeaders) ||
406-
!this.interceptorMatchHeaders.every(checkHeaders)
407-
) {
408-
return false
409-
}
410-
411397
const comparisonKey = isRegex ? this.__nock_scopeKey : this._key
412398
const matchKey = `${method} ${proto}://${options.host}${path}`
413399

tests/test_header_matching.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ test('match header on scope with function: matches when match accepted', async t
118118
scope.done()
119119
})
120120

121+
test('match header on scope with function and allow unmocked: matches when match accepted', async t => {
122+
const scope = nock('http://example.test', { allowUnmocked: true })
123+
.get('/')
124+
.matchHeader('x-my-headers', val => true)
125+
.reply(200, 'Hello World!')
126+
127+
const { statusCode, body } = await got('http://example.test/', {
128+
headers: { 'X-My-Headers': 456 },
129+
})
130+
131+
t.equal(statusCode, 200)
132+
t.equal(body, 'Hello World!')
133+
scope.done()
134+
})
135+
121136
test('match header on scope with function: does not match when match declined', async t => {
122137
nock('http://example.test')
123138
.get('/')
@@ -192,6 +207,23 @@ test('match header on interceptor with function: matches when match accepted', a
192207
scope.done()
193208
})
194209

210+
test('match header on interceptor with function: matches when match accepted', async t => {
211+
const scope = nock('http://example.test', { allowUnmocked: true })
212+
.matchHeader('x-my-headers', val => true)
213+
// `.matchHeader()` is called on the interceptor. It precedes the call to
214+
// `.get()`.
215+
.get('/')
216+
.reply(200, 'Hello World!')
217+
218+
const { statusCode, body } = await got('http://example.test/', {
219+
headers: { 'X-My-Headers': 456 },
220+
})
221+
222+
t.equal(statusCode, 200)
223+
t.equal(body, 'Hello World!')
224+
scope.done()
225+
})
226+
195227
test('match header on interceptor with function: does not match when match declined', async t => {
196228
nock('http://example.test')
197229
.matchHeader('x-my-headers', val => false)

0 commit comments

Comments
 (0)