Skip to content

Commit

Permalink
fix: throw error when leading slash is not present in path (#1391)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kevinnio authored and gr2m committed Jan 25, 2019
1 parent 1624c52 commit 28b2d43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/interceptor.js
Expand Up @@ -18,6 +18,14 @@ try {
module.exports = Interceptor

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

this.scope = scope
this.interceptorMatchHeaders = []

Expand Down
5 changes: 5 additions & 0 deletions tests/test_intercept.js
Expand Up @@ -106,6 +106,11 @@ test("when request's content-type is json: reply callback's requestBody should a
scope.done()
})

test("when the path doesn't include a leading slash it raises an error", function(t) {
t.plan(1)
t.throws(() => nock('http://example.test').get('no-leading-slash'))
})

test("when request has no content-type header: reply callback's requestBody should not automatically parse to JSON", async t => {
const requestBodyFixture = {
id: 1,
Expand Down

0 comments on commit 28b2d43

Please sign in to comment.