Skip to content

Commit 28b2d43

Browse files
kevinniogr2m
authored andcommitted
fix: throw error when leading slash is not present in path (#1391)
Given #1391 (comment), we decided to make nock raise an error whenever the user forgets to add a leading slash into the intercepted path, instead of adding it itself.
1 parent 1624c52 commit 28b2d43

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

lib/interceptor.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ try {
1818
module.exports = Interceptor
1919

2020
function Interceptor(scope, uri, method, requestBody, interceptorOptions) {
21+
// Check for leading slash. Uri can be either a string or a regexp, but
22+
// we only need to check strings.
23+
if (typeof uri === 'string' && /^[^/*]/.test(uri)) {
24+
throw Error(
25+
"Non-wildcard URL path strings must begin with a slash (otherwise they won't match anything)"
26+
)
27+
}
28+
2129
this.scope = scope
2230
this.interceptorMatchHeaders = []
2331

tests/test_intercept.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ test("when request's content-type is json: reply callback's requestBody should a
106106
scope.done()
107107
})
108108

109+
test("when the path doesn't include a leading slash it raises an error", function(t) {
110+
t.plan(1)
111+
t.throws(() => nock('http://example.test').get('no-leading-slash'))
112+
})
113+
109114
test("when request has no content-type header: reply callback's requestBody should not automatically parse to JSON", async t => {
110115
const requestBodyFixture = {
111116
id: 1,

0 commit comments

Comments
 (0)