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 allowUnmocked not working w/ regex host + request body match (fixes #2276) #2277

Merged
merged 1 commit into from Jan 11, 2022
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
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