Skip to content

Commit b7f9f13

Browse files
TLPcoderpaulmelnikow
authored andcommitted
feat: Throw errors for URL paths without leading slashes, which will never match (#1744)
Fix #1730
1 parent 7a568dd commit b7f9f13

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/interceptor.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ module.exports = class Interceptor {
3131
constructor(scope, uri, method, requestBody, interceptorOptions) {
3232
const uriIsStr = typeof uri === 'string'
3333
// Check for leading slash. Uri can be either a string or a regexp, but
34-
// we only need to check strings.
35-
if (uriIsStr && /^[^/*]/.test(uri)) {
34+
// When enabled filteringScope ignores the passed URL entirely so we skip validation.
35+
36+
if (
37+
!scope.scopeOptions.filteringScope &&
38+
uriIsStr &&
39+
!uri.startsWith('/') &&
40+
!uri.startsWith('*')
41+
) {
3642
throw Error(
3743
`Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: ${uri})`
3844
)
@@ -224,7 +230,7 @@ module.exports = class Interceptor {
224230
}
225231

226232
const method = (options.method || 'GET').toUpperCase()
227-
let { path = '' } = options
233+
let { path = '/' } = options
228234
let matches
229235
let matchKey
230236
const { proto } = options

tests/test_intercept.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,7 +1300,7 @@ test('match domain and path using regexp', t => {
13001300
// https://github.com/nock/nock/issues/1003
13011301
test('correctly parse request without specified path', t => {
13021302
const scope1 = nock('https://example.test')
1303-
.get('')
1303+
.get('/')
13041304
.reply(200)
13051305

13061306
https
@@ -1317,7 +1317,7 @@ test('correctly parse request without specified path', t => {
13171317

13181318
test('data is sent with flushHeaders', t => {
13191319
const scope1 = nock('https://example.test')
1320-
.get('')
1320+
.get('/')
13211321
.reply(200, 'this is data')
13221322

13231323
https
@@ -1334,6 +1334,28 @@ test('data is sent with flushHeaders', t => {
13341334
.flushHeaders()
13351335
})
13361336

1337+
// https://github.com/nock/nock/issues/1730
1338+
test('URL path without leading slash throws expected error', t => {
1339+
t.throws(() => nock('http://example.test').get(''), {
1340+
message:
1341+
"Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything) (got: )",
1342+
})
1343+
1344+
t.end()
1345+
})
1346+
1347+
test('wildcard param URL should not throw error', t => {
1348+
nock('http://example.test').get('*')
1349+
1350+
t.end()
1351+
})
1352+
1353+
test('with filteringScope, URL path without leading slash does not throw error', t => {
1354+
nock('http://example.test', { filteringScope: () => {} }).get('')
1355+
1356+
t.end()
1357+
})
1358+
13371359
test('no new keys were added to the global namespace', t => {
13381360
const leaks = Object.keys(global).filter(
13391361
key => !acceptableGlobalKeys.has(key)

0 commit comments

Comments
 (0)