diff options
| author | Corey Burrows | 2013-11-14 08:42:35 -0600 | 
|---|---|---|
| committer | Igor Minar | 2013-11-21 22:57:15 -0800 | 
| commit | 4d16472b918a3482942d76f1e273a5aa01f65e83 (patch) | |
| tree | e5400e5e61e6550dc4c41469f1f458e9b6ced6ec | |
| parent | 9e89a31b129e40c805178535c244899ffafb77d8 (diff) | |
| download | angular.js-4d16472b918a3482942d76f1e273a5aa01f65e83.tar.bz2 | |
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
| -rw-r--r-- | src/ngMock/angular-mocks.js | 2 | ||||
| -rw-r--r-- | test/ngMock/angular-mocksSpec.js | 49 | 
2 files changed, 50 insertions, 1 deletions
| diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index e13675d7..cf01d3c3 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1572,7 +1572,7 @@ function MockHttpExpectation(method, url, data, headers) {      if (angular.isUndefined(data)) return true;      if (data && angular.isFunction(data.test)) return data.test(d);      if (data && angular.isFunction(data)) return data(d); -    if (data && !angular.isString(data)) return angular.toJson(data) == d; +    if (data && !angular.isString(data)) return angular.equals(data, angular.fromJson(d));      return data == d;    }; diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 15ebc758..f96112fe 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -941,6 +941,29 @@ describe('ngMock', function() {      }); +    it('should match data object if specified', function() { +      hb.when('GET', '/a/b', {a: 1, b: 2}).respond(201, 'content1'); +      hb.when('GET', '/a/b').respond(202, 'content2'); + +      hb('GET', '/a/b', '{"a":1,"b":2}', function(status, response) { +        expect(status).toBe(201); +        expect(response).toBe('content1'); +      }); + +      hb('GET', '/a/b', '{"b":2,"a":1}', function(status, response) { +        expect(status).toBe(201); +        expect(response).toBe('content1'); +      }); + +      hb('GET', '/a/b', null, function(status, response) { +        expect(status).toBe(202); +        expect(response).toBe('content2'); +      }); + +      hb.flush(); +    }); + +      it('should match only method', function() {        hb.when('GET').respond(202, 'c');        callback.andCallFake(function(status, response) { @@ -1072,6 +1095,32 @@ describe('ngMock', function() {        }); +      it ('should not throw an exception when parsed body is equal to expected body object', function() { +        hb.when('GET').respond(200, '', {}); + +        hb.expect('GET', '/match', {a: 1, b: 2}); +        expect(function() { +          hb('GET', '/match', '{"a":1,"b":2}', noop, {}); +        }).not.toThrow(); + +        hb.expect('GET', '/match', {a: 1, b: 2}); +        expect(function() { +          hb('GET', '/match', '{"b":2,"a":1}', noop, {}); +        }).not.toThrow(); +      }); + + +      it ('should throw exception when only parsed body differs from expected body object', function() { +        hb.when('GET').respond(200, '', {}); +        hb.expect('GET', '/match', {a: 1, b: 2}); + +        expect(function() { +          hb('GET', '/match', '{"a":1,"b":3}', noop, {}); +        }).toThrow('Expected GET /match with different data\n' + +                   'EXPECTED: {"a":1,"b":2}\nGOT:      {"a":1,"b":3}'); +      }); + +        it("should use when's respond() when no expect() respond is defined", function() {          callback.andCallFake(function(status, response) {            expect(status).toBe(201); | 
