Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 4d16472

Browse files
Corey BurrowsIgorMinar
Corey Burrows
authored andcommitted
fix(ngMock): fixes httpBackend expectation with body object
Fixes an issue with httpBackend expectations where a given body object may not match the actual request body if its keys are serialized in a different order. Closes #4956
1 parent 9e89a31 commit 4d16472

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

src/ngMock/angular-mocks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1572,7 +1572,7 @@ function MockHttpExpectation(method, url, data, headers) {
15721572
if (angular.isUndefined(data)) return true;
15731573
if (data && angular.isFunction(data.test)) return data.test(d);
15741574
if (data && angular.isFunction(data)) return data(d);
1575-
if (data && !angular.isString(data)) return angular.toJson(data) == d;
1575+
if (data && !angular.isString(data)) return angular.equals(data, angular.fromJson(d));
15761576
return data == d;
15771577
};
15781578

test/ngMock/angular-mocksSpec.js

+49
Original file line numberDiff line numberDiff line change
@@ -941,6 +941,29 @@ describe('ngMock', function() {
941941
});
942942

943943

944+
it('should match data object if specified', function() {
945+
hb.when('GET', '/a/b', {a: 1, b: 2}).respond(201, 'content1');
946+
hb.when('GET', '/a/b').respond(202, 'content2');
947+
948+
hb('GET', '/a/b', '{"a":1,"b":2}', function(status, response) {
949+
expect(status).toBe(201);
950+
expect(response).toBe('content1');
951+
});
952+
953+
hb('GET', '/a/b', '{"b":2,"a":1}', function(status, response) {
954+
expect(status).toBe(201);
955+
expect(response).toBe('content1');
956+
});
957+
958+
hb('GET', '/a/b', null, function(status, response) {
959+
expect(status).toBe(202);
960+
expect(response).toBe('content2');
961+
});
962+
963+
hb.flush();
964+
});
965+
966+
944967
it('should match only method', function() {
945968
hb.when('GET').respond(202, 'c');
946969
callback.andCallFake(function(status, response) {
@@ -1072,6 +1095,32 @@ describe('ngMock', function() {
10721095
});
10731096

10741097

1098+
it ('should not throw an exception when parsed body is equal to expected body object', function() {
1099+
hb.when('GET').respond(200, '', {});
1100+
1101+
hb.expect('GET', '/match', {a: 1, b: 2});
1102+
expect(function() {
1103+
hb('GET', '/match', '{"a":1,"b":2}', noop, {});
1104+
}).not.toThrow();
1105+
1106+
hb.expect('GET', '/match', {a: 1, b: 2});
1107+
expect(function() {
1108+
hb('GET', '/match', '{"b":2,"a":1}', noop, {});
1109+
}).not.toThrow();
1110+
});
1111+
1112+
1113+
it ('should throw exception when only parsed body differs from expected body object', function() {
1114+
hb.when('GET').respond(200, '', {});
1115+
hb.expect('GET', '/match', {a: 1, b: 2});
1116+
1117+
expect(function() {
1118+
hb('GET', '/match', '{"a":1,"b":3}', noop, {});
1119+
}).toThrow('Expected GET /match with different data\n' +
1120+
'EXPECTED: {"a":1,"b":2}\nGOT: {"a":1,"b":3}');
1121+
});
1122+
1123+
10751124
it("should use when's respond() when no expect() respond is defined", function() {
10761125
callback.andCallFake(function(status, response) {
10771126
expect(status).toBe(201);

0 commit comments

Comments
 (0)