Skip to content

Commit

Permalink
fix(define): Throw error when legacy reply is in wrong format
Browse files Browse the repository at this point in the history
Shockingly, `_.isNumber(NaN)` is `true` which means this conditional check didn’t even work as intended.

Testing error paths is important!
  • Loading branch information
paulmelnikow authored and gr2m committed May 2, 2019
1 parent aabc003 commit 5d2fb9f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
10 changes: 5 additions & 5 deletions lib/scope.js
Expand Up @@ -268,14 +268,14 @@ function load(path) {
}

function getStatusFromDefinition(nockDef) {
// Backward compatibility for when `status` was encoded as string in `reply`.
// Backward compatibility for when `status` was encoded as string in `reply`.
if (!_.isUndefined(nockDef.reply)) {
// Try parsing `reply` property.
const parsedReply = parseInt(nockDef.reply, 10)
if (_.isNumber(parsedReply)) {
return parsedReply
if (isNaN(parsedReply)) {
throw Error('`reply`, when present, must be a numeric string')
}
// TODO-coverage: `else` throw with an error.

return parsedReply
}

const DEFAULT_STATUS_OK = 200
Expand Down
18 changes: 18 additions & 0 deletions tests/test_define.js
Expand Up @@ -50,6 +50,24 @@ test('define() is backward compatible', t => {
req.end()
})

test('define() throws when reply is not a numeric string', t => {
t.throws(
() =>
nock.define([
{
scope: 'http://example.com:1451',
method: 'GET',
path: '/',
reply: 'frodo',
},
]),
{
message: '`reply`, when present, must be a numeric string',
}
)
t.end()
})

test('define() applies default status code when none is specified', async t => {
const body = '�'

Expand Down

0 comments on commit 5d2fb9f

Please sign in to comment.