Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix .matchHeader() with allowUnmocked #1480

Merged
merged 13 commits into from
Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ function interceptorsFor(options) {
matchingInterceptor = [
{
options: { allowUnmocked: true },
matchIndependentOfBody: function() {
matchAddress() {
return false
},
},
Expand Down Expand Up @@ -396,7 +396,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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()
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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()
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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