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

Throw error when leading slash is not present in path #1391

Merged
merged 3 commits into from
Jan 25, 2019
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
8 changes: 8 additions & 0 deletions lib/interceptor.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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