Skip to content

Commit 7156fd6

Browse files
committed
[#863] Fix matchBody for object and array comparison
1 parent 934f1c3 commit 7156fd6

File tree

2 files changed

+40
-44
lines changed

2 files changed

+40
-44
lines changed

lib/match_body.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ function deepEqualExtended(spec, body) {
5959
}
6060
if (spec && (spec.constructor === Object || spec.constructor === Array) && body) {
6161
var keys = Object.keys(spec);
62+
var actualKeys = Object.keys(body);
63+
if (actualKeys.length !== keys.length) {
64+
return false;
65+
}
66+
6267
for (var i = 0; i < keys.length; i++) {
6368
if (!deepEqualExtended(spec[keys[i]], body[keys[i]])) {
6469
return false;

tests/test_body_match.js

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,55 @@ var test = require('tap').test;
55
var mikealRequest = require('request');
66
var assert = require('assert');
77

8-
test('match body with regex', function (t) {
9-
8+
var testBodyMatch = function (expected, actual, shouldMatch, t) {
109
nock('http://encodingsareus.com')
11-
.post('/', {auth: {passwd: /a.+/}})
10+
.post('/', expected)
1211
.reply(200);
1312

1413
mikealRequest({
1514
url: 'http://encodingsareus.com/',
1615
method: 'post',
17-
json: {
18-
auth: {
19-
passwd: 'abc'
20-
}
21-
},
16+
json: actual,
2217
}, function(err, res) {
23-
if (err) throw err;
24-
assert.equal(res.statusCode, 200);
25-
t.end();
18+
shouldMatch
19+
? assert.equal(res.statusCode, 200)
20+
: assert(err && /No match for request/.test(err.message), 'nock should not intercept the request');
21+
t.end();
2622
});
23+
}
2724

28-
});
2925

30-
test('match body with regex inside array', function (t) {
26+
test('body matching', function (t) {
27+
t.test('match body with regex', function (t2) {
28+
testBodyMatch({auth: {passwd: /a.+/}}, {auth: {passwd: 'abc'}}, true, t2);
29+
});
3130

32-
nock('http://encodingsareus.com')
33-
.post('/', {items: [{name: /t.+/}]})
34-
.reply(200);
31+
t.test('match body with regex inside array', function (t2) {
32+
testBodyMatch({items: [{name: /t.+/}]}, {items: [{name: 'test'}]}, true, t2);
33+
});
3534

36-
mikealRequest({
37-
url: 'http://encodingsareus.com/',
38-
method: 'post',
39-
json: {
40-
items: [{
41-
name: 'test'
42-
}]
43-
},
44-
}, function(err, res) {
45-
if (err) throw err;
46-
assert.equal(res.statusCode, 200);
47-
t.end();
35+
t.test('failing body matching with regex inside array', function (t2) {
36+
testBodyMatch({items: [{name: /o.+/}]}, {items: [{name: 'test'}]}, false, t2);
4837
});
49-
})
5038

51-
test('match body with empty object inside', function (t) {
39+
t.test('failing body matching with different array length', function (t2) {
40+
testBodyMatch({items: [1, 2]}, {items: [{items: [1, 2, 3]}]}, false, t2);
41+
});
5242

53-
nock('http://encodingsareus.com')
54-
.post('/', { obj: {}})
55-
.reply(200);
43+
t.test('match body with empty object inside', function (t2) {
44+
testBodyMatch({ obj: {}}, {obj: {}}, true, t2);
45+
});
5646

57-
mikealRequest({
58-
url: 'http://encodingsareus.com/',
59-
method: 'post',
60-
json: {
61-
obj: {}
62-
},
63-
}, function(err, res) {
64-
if (err) throw err;
65-
assert.equal(res.statusCode, 200);
66-
t.end();
47+
t.test('match body with identical objects', function (t2) {
48+
testBodyMatch({ obj: {b:1, a:1}}, {obj: {b:1, a:1}}, true, t2);
6749
});
68-
})
50+
51+
t.test('failing body matching with different objects', function (t2) {
52+
testBodyMatch({ obj: {a:1}}, {obj: {b:1, a:1}}, false, t2);
53+
});
54+
55+
t.test('failing body matching with different nested objects', function (t2) {
56+
testBodyMatch({ obj: {a{b:1}}}, {obj: {a: {b:1, c:1}}}, false, t2);
57+
});
58+
t.end();
59+
});

0 commit comments

Comments
 (0)