Skip to content

Commit fa0a08a

Browse files
authored
fix: Fix crash when matching certain objects (#1714)
When matching objects with the same number of keys but different keys, should not match instead of crashing. Close #1713
1 parent d7b2c92 commit fa0a08a

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

lib/common.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,10 @@ function deepEqual(expected, actual) {
578578
}
579579

580580
if (Array.isArray(expected) || _.isPlainObject(expected)) {
581+
if (actual === undefined) {
582+
return false
583+
}
584+
581585
const expKeys = Object.keys(expected)
582586
if (expKeys.length !== Object.keys(actual).length) {
583587
return false

tests/test_body_match.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,25 @@ test("doesn't match body with mismatching keys", t => {
214214
)
215215
})
216216

217+
// https://github.com/nock/nock/issues/1713
218+
test("doesn't match body with same number of keys but different keys", t => {
219+
nock('http://example.test')
220+
.post('/', { a: {} })
221+
.reply()
222+
223+
mikealRequest(
224+
{
225+
url: 'http://example.test',
226+
method: 'post',
227+
json: { b: 123 },
228+
},
229+
function(err) {
230+
assert.ok(err)
231+
t.end()
232+
}
233+
)
234+
})
235+
217236
test('match body with form multipart', t => {
218237
nock('http://example.test')
219238
.post(

0 commit comments

Comments
 (0)