Skip to content

Commit 7e44769

Browse files
authored
fix(intercept): Better error message when options is falsy (#1440)
1 parent f0c16ab commit 7e44769

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

lib/intercept.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,16 @@ function overrideClientRequest() {
249249
// Define the overriding client request that nock uses internally.
250250
function OverriddenClientRequest(options, cb) {
251251
if (!options) {
252-
// In principle, there is no reason we couldn't support this. However it
253-
// doesn't work, and fixing it seems low priority. Giving an explicit
254-
// error seems nicer than crashing with a weird stack trace.
252+
// As weird as it is, it's possible to call `http.request` without
253+
// options, and it makes a request to localhost or somesuch. We should
254+
// support it too, for parity. However it doesn't work today, and fixing
255+
// it seems low priority. Giving an explicit error is nicer than
256+
// crashing with a weird stack trace. `http[s].request()`, nock's other
257+
// client-facing entry point, makes a similar check.
255258
// https://github.com/nock/nock/pull/1386
259+
// https://github.com/nock/nock/pull/1440
256260
throw Error(
257-
'Creating a client request with empty `options` is not supported in Nock'
261+
'Creating a ClientRequest with empty `options` is not supported in Nock'
258262
)
259263
}
260264

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

tests/test_clientrequest.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ test('can use ClientRequest using POST', t => {
7373
test('creating ClientRequest with empty options throws expected error', t => {
7474
t.throws(() => new http.ClientRequest(), {
7575
message:
76-
'Creating a client request with empty `options` is not supported in Nock',
76+
'Creating a ClientRequest with empty `options` is not supported in Nock',
7777
})
7878

7979
t.end()

tests/test_intercept.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1701,6 +1701,13 @@ test('data is sent with flushHeaders', t => {
17011701
.flushHeaders()
17021702
})
17031703

1704+
test('should throw expected error when creating request with missing options', t => {
1705+
t.throws(() => http.request(), {
1706+
message: 'Making a request with empty `options` is not supported in Nock',
1707+
})
1708+
t.end()
1709+
})
1710+
17041711
test('teardown', t => {
17051712
let leaks = Object.keys(global).splice(globalCount, Number.MAX_VALUE)
17061713

0 commit comments

Comments
 (0)