Skip to content

Commit

Permalink
fix: allowUnocked not working with regex host + request body match (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonpage committed Jan 11, 2022
1 parent 4cb9fb0 commit ac7b4fd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/interceptor.js
Expand Up @@ -413,6 +413,12 @@ module.exports = class Interceptor {
}

matchHostName(options) {
const { basePath } = this.scope

if (basePath instanceof RegExp) {
return basePath.test(options.hostname)
}

return options.hostname === this.scope.urlParts.hostname
}

Expand Down
19 changes: 19 additions & 0 deletions tests/test_allow_unmocked.js
Expand Up @@ -141,6 +141,25 @@ describe('allowUnmocked option', () => {
scope.done()
})

it('allow unmocked passthrough with regex host & mismatched bodies', async () => {
const { origin } = await startHttpServer((request, response) => {
response.writeHead(200)
response.write('{"message":"server response"}')
response.end()
})

nock(/localhost/, { allowUnmocked: true })
.post('/post', { some: 'other data' })
.reply(404, '{"message":"server response"}')

const { body, statusCode } = await got.post(`${origin}/post`, {
json: { some: 'data' },
responseType: 'json',
})
expect(statusCode).to.equal(200)
expect(body).to.deep.equal({ message: 'server response' })
})

// https://github.com/nock/nock/issues/1867
it('match path using callback with allowUnmocked', async () => {
const scope = nock('http://example.test', { allowUnmocked: true })
Expand Down

0 comments on commit ac7b4fd

Please sign in to comment.