Skip to content

Commit

Permalink
Merge pull request #719 from ltegman/fix/body-spec-regex-in-array
Browse files Browse the repository at this point in the history
Handle arrays like objects in match_body
  • Loading branch information
vrinek committed Oct 16, 2016
2 parents bc565eb + c4c5689 commit 4a266e8
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) {
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 4a266e8

Please sign in to comment.