From 5d2fb9fc1e496e2701e4c70ed0ecbb6f9ed6b089 Mon Sep 17 00:00:00 2001 From: Paul Melnikow Date: Wed, 1 May 2019 18:07:45 -0400 Subject: [PATCH] fix(define): Throw error when legacy reply is in wrong format MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Shockingly, `_.isNumber(NaN)` is `true` which means this conditional check didn’t even work as intended. Testing error paths is important! --- lib/scope.js | 10 +++++----- tests/test_define.js | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lib/scope.js b/lib/scope.js index fd0b867b0..35a45fe06 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -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 diff --git a/tests/test_define.js b/tests/test_define.js index 54d57a97c..84c113f01 100644 --- a/tests/test_define.js +++ b/tests/test_define.js @@ -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 = '�'