Skip to content

Commit

Permalink
Merge 8c33395 into 2c4edba
Browse files Browse the repository at this point in the history
  • Loading branch information
mastermatt committed Jul 15, 2019
2 parents 2c4edba + 8c33395 commit b71e055
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
11 changes: 4 additions & 7 deletions lib/interceptor.js
Expand Up @@ -187,8 +187,7 @@ Interceptor.prototype.reqheaderMatches = function reqheaderMatches(
// actual request. This because 'host' may get inserted by Nock itself and
// then get recorded. NOTE: We use lower-case header field names throughout
// Nock. See https://github.com/nock/nock/pull/196.
// TODO-coverage: This looks very special. Worth an investigation.
if (key === 'host' && (_.isUndefined(header) || _.isUndefined(reqHeader))) {
if (key === 'host' && (header === undefined || reqHeader === undefined)) {
return true
}

Expand Down Expand Up @@ -246,11 +245,9 @@ Interceptor.prototype.match = function match(options, body, hostNameOnly) {
return false
}

const reqHeadersMatch =
!this.reqheaders ||
Object.keys(this.reqheaders).every(
this.reqheaderMatches.bind(this, options)
)
const reqHeadersMatch = Object.keys(this.reqheaders).every(key =>
this.reqheaderMatches(options, key)
)

if (!reqHeadersMatch) {
return false
Expand Down
46 changes: 46 additions & 0 deletions tests/test_header_matching.js
Expand Up @@ -576,3 +576,49 @@ test('multiple interceptors override headers from unrelated request', async t =>
})
t.is(res2.statusCode, 200)
})

// The next three tests cover the special case for the Host header where it's only used for
// matching if it's defined on the scope and the request. See https://github.com/nock/nock/pull/196
test('Host header is used for matching if defined on the scope and request', async t => {
const scope = nock('http://example.test', {
reqheaders: { host: 'example.test' },
})
.get('/')
.reply()

const { statusCode } = await got('http://example.test/', {
headers: { Host: 'example.test' },
})

t.is(statusCode, 200)
scope.done()
})

test('Host header is ignored during matching if not defined on the request', async t => {
const scope = nock('http://example.test', {
reqheaders: { host: 'example.test' },
})
.get('/')
.reply()

const { statusCode } = await got('http://example.test/')

t.is(statusCode, 200)
scope.done()
})

test('Host header is used to reject a match if defined on the scope and request', async t => {
nock('http://example.test', {
reqheaders: { host: 'example.test' },
})
.get('/')
.reply()

await assertRejects(
got('http://example.test/', {
headers: { Host: 'some.other.domain.test' },
}),
Error,
'Nock: No match for request'
)
})

0 comments on commit b71e055

Please sign in to comment.