Skip to content

Commit 5d2fb9f

Browse files
paulmelnikowgr2m
authored andcommitted
fix(define): Throw error when legacy reply is in wrong format
Shockingly, `_.isNumber(NaN)` is `true` which means this conditional check didn’t even work as intended. Testing error paths is important!
1 parent aabc003 commit 5d2fb9f

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

lib/scope.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,14 @@ function load(path) {
268268
}
269269

270270
function getStatusFromDefinition(nockDef) {
271-
// Backward compatibility for when `status` was encoded as string in `reply`.
271+
// Backward compatibility for when `status` was encoded as string in `reply`.
272272
if (!_.isUndefined(nockDef.reply)) {
273-
// Try parsing `reply` property.
274273
const parsedReply = parseInt(nockDef.reply, 10)
275-
if (_.isNumber(parsedReply)) {
276-
return parsedReply
274+
if (isNaN(parsedReply)) {
275+
throw Error('`reply`, when present, must be a numeric string')
277276
}
278-
// TODO-coverage: `else` throw with an error.
277+
278+
return parsedReply
279279
}
280280

281281
const DEFAULT_STATUS_OK = 200

tests/test_define.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,24 @@ test('define() is backward compatible', t => {
5050
req.end()
5151
})
5252

53+
test('define() throws when reply is not a numeric string', t => {
54+
t.throws(
55+
() =>
56+
nock.define([
57+
{
58+
scope: 'http://example.com:1451',
59+
method: 'GET',
60+
path: '/',
61+
reply: 'frodo',
62+
},
63+
]),
64+
{
65+
message: '`reply`, when present, must be a numeric string',
66+
}
67+
)
68+
t.end()
69+
})
70+
5371
test('define() applies default status code when none is specified', async t => {
5472
const body = '�'
5573

0 commit comments

Comments
 (0)