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: allowMocked when using a callback for the path #1877

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion lib/interceptor.js
Expand Up @@ -355,6 +355,7 @@ module.exports = class Interceptor {
* match the provided options.
*/
matchOrigin(options) {
const isPathFn = typeof this.path === 'function';
const isRegex = this.path instanceof RegExp
const isRegexBasePath = this.scope.basePath instanceof RegExp

Expand All @@ -370,9 +371,13 @@ module.exports = class Interceptor {
if (this.scope.transformPathFunction) {
path = this.scope.transformPathFunction(path)
}
const comparisonKey = isRegex ? this.__nock_scopeKey : this._key
const comparisonKey = isPathFn || isRegex ? this.__nock_scopeKey : this._key
const matchKey = `${method} ${proto}://${options.host}${path}`

if(isPathFn) {
return !!(matchKey.match(comparisonKey) && this.path(path))
}

if (isRegex && !isRegexBasePath) {
return !!matchKey.match(comparisonKey) && this.path.test(path)
}
Expand Down
13 changes: 13 additions & 0 deletions tests/test_allow_unmocked.js
@@ -1,6 +1,7 @@
'use strict'

const http = require('http')
const { expect } = require('chai')
const url = require('url')
const mikealRequest = require('request')
const { test } = require('tap')
Expand Down Expand Up @@ -233,6 +234,18 @@ test('match hostname using regexp with allowUnmocked (issue-1076)', t => {
})
})

// https://github.com/nock/nock/issues/1867
test('match path using callback with allowUnmocked', async t => {
const scope = nock('http://example.test', { allowUnmocked: true })
.get((uri => uri.endsWith('bar')))
.reply()

const { statusCode } = await got('http://example.test/foo/bar')
expect(statusCode).to.equal(200)

scope.done()
})

// https://github.com/nock/nock/issues/835
test('match multiple paths to domain using regexp with allowUnmocked', async t => {
const server = http.createServer((request, response) => {
Expand Down