Skip to content

Commit

Permalink
Handle arrays like objects in match_body
Browse files Browse the repository at this point in the history
  • Loading branch information
ltegman committed Oct 13, 2016
1 parent bc565eb commit c4c5689
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/match_body.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ function deepEqualExtended(spec, body) {
if (spec && spec.constructor === RegExp) {
return spec.test(body);
}
if (spec && spec.constructor === Object && body) {
if (spec && spec.constructor === Object || spec.constructor === Array && body) {

This comment has been minimized.

Copy link
@mickaeltr

mickaeltr Oct 24, 2016

This caused a regression. deepEqualExtended now fails when "spec" is null or undefined:

Uncaught TypeError: Cannot read property 'constructor' of null
at deepEqualExtended (…/node_modules/nock/lib/match_body.js:60:50)
      at deepEqualExtended (…/node_modules/nock/lib/match_body.js:63:12)
      at deepEqualExtended (…/node_modules/nock/lib/match_body.js:63:12)
      at Object.matchBody (…/node_modules/nock/lib/match_body.js:53:10)
      at Interceptor.match (…/node_modules/nock/lib/interceptor.js:325:30)

Also see issue #731

This comment has been minimized.

Copy link
@aredridel

aredridel Oct 26, 2016

+1

var keys = Object.keys(spec);
for (var i = 0; i < keys.length; i++) {
if (!deepEqualExtended(spec[keys[i]], body[keys[i]])) {
Expand Down
21 changes: 21 additions & 0 deletions tests/test_body_match.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,24 @@ test('match body with regex', function (t) {
});

});

test('match body with regex inside array', function (t) {

nock('http://encodingsareus.com')
.post('/', {items: [{name: /t.+/}]})
.reply(200);

mikealRequest({
url: 'http://encodingsareus.com/',
method: 'post',
json: {
items: [{
name: 'test'
}]
},
}, function(err, res) {
if (err) throw err;
assert.equal(res.statusCode, 200);
t.end();
});
})

0 comments on commit c4c5689

Please sign in to comment.