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(intercept): Better error message when options is falsy #1440

Merged
merged 6 commits into from
Apr 13, 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
24 changes: 20 additions & 4 deletions lib/intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,12 +249,16 @@ function overrideClientRequest() {
// Define the overriding client request that nock uses internally.
function OverriddenClientRequest(options, cb) {
if (!options) {
// In principle, there is no reason we couldn't support this. However it
// doesn't work, and fixing it seems low priority. Giving an explicit
// error seems nicer than crashing with a weird stack trace.
// As weird as it is, it's possible to call `http.request` without
// options, and it makes a request to localhost or somesuch. We should
// support it too, for parity. However it doesn't work today, and fixing
// it seems low priority. Giving an explicit error is nicer than
// crashing with a weird stack trace. `http[s].request()`, nock's other
// client-facing entry point, makes a similar check.
// https://github.com/nock/nock/pull/1386
// https://github.com/nock/nock/pull/1440
throw Error(
'Creating a client request with empty `options` is not supported in Nock'
'Creating a ClientRequest with empty `options` is not supported in Nock'
)
}

Expand Down Expand Up @@ -386,6 +390,18 @@ function activate() {
options = urlParse(options)
} else if (options instanceof URL) {
options = urlParse(options.toString())
} else if (!options) {
// As weird as it is, it's possible to call `http.request` without
// options, and it makes a request to localhost or somesuch. We should
// support it too, for parity. However it doesn't work today, and fixing
// it seems low priority. Giving an explicit error is nicer than
// crashing with a weird stack trace. `new ClientRequest()`, nock's
// other client-facing entry point, makes a similar check.
// https://github.com/nock/nock/pull/1386
// https://github.com/nock/nock/pull/1440
throw Error(
'Making a request with empty `options` is not supported in Nock'
)
}
options.proto = proto

Expand Down
2 changes: 1 addition & 1 deletion tests/test_clientrequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ test('can use ClientRequest using POST', t => {
test('creating ClientRequest with empty options throws expected error', t => {
t.throws(() => new http.ClientRequest(), {
message:
'Creating a client request with empty `options` is not supported in Nock',
'Creating a ClientRequest with empty `options` is not supported in Nock',
})

t.end()
Expand Down
7 changes: 7 additions & 0 deletions tests/test_intercept.js
Original file line number Diff line number Diff line change
Expand Up @@ -1701,6 +1701,13 @@ test('data is sent with flushHeaders', t => {
.flushHeaders()
})

test('should throw expected error when creating request with missing options', t => {
t.throws(() => http.request(), {
message: 'Making a request with empty `options` is not supported in Nock',
})
t.end()
})

test('teardown', t => {
let leaks = Object.keys(global).splice(globalCount, Number.MAX_VALUE)

Expand Down