diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 2dd4fef5ea3f..986110ac6da1 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1539,6 +1539,8 @@ function MockHttpExpectation(method, url, data, headers) { this.data = data; this.headers = headers; + if (url === undefined) throw new Error('No URL defined!'); + this.match = function(m, u, d, h) { if (method != m) return false; if (!this.matchUrl(u)) return false; @@ -1548,7 +1550,7 @@ function MockHttpExpectation(method, url, data, headers) { }; this.matchUrl = function(u) { - if (!url) return true; + if (url === null) return true; if (angular.isFunction(url.test)) return url.test(u); if (angular.isFunction(url)) return url(u); return url == u; diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index c1c33ffb8788..a72e10471c9a 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -890,7 +890,7 @@ describe('$http', function() { describe('scope.$apply', function() { it('should $apply after success callback', function() { - $httpBackend.when('GET').respond(200); + $httpBackend.when('GET', null).respond(200); $http({method: 'GET', url: '/some'}); $httpBackend.flush(); expect($rootScope.$apply).toHaveBeenCalledOnce(); @@ -898,7 +898,7 @@ describe('$http', function() { it('should $apply after error callback', function() { - $httpBackend.when('GET').respond(404); + $httpBackend.when('GET', null).respond(404); $http({method: 'GET', url: '/some'}); $httpBackend.flush(); expect($rootScope.$apply).toHaveBeenCalledOnce(); @@ -906,7 +906,7 @@ describe('$http', function() { it('should $apply even if exception thrown during callback', inject(function($exceptionHandler){ - $httpBackend.when('GET').respond(200); + $httpBackend.when('GET', null).respond(200); callback.andThrow('error in callback'); $http({method: 'GET', url: '/some'}).then(callback); @@ -1402,7 +1402,7 @@ describe('$http', function() { describe('pendingRequests', function() { it('should be an array of pending requests', function() { - $httpBackend.when('GET').respond(200); + $httpBackend.when('GET', null).respond(200); expect($http.pendingRequests.length).toBe(0); $http({method: 'get', url: '/some'}); @@ -1415,7 +1415,7 @@ describe('$http', function() { it('should update pending requests even when served from cache', inject(function($rootScope) { - $httpBackend.when('GET').respond(200); + $httpBackend.when('GET', null).respond(200); $http({method: 'get', url: '/cached', cache: true}); $http({method: 'get', url: '/cached', cache: true}); @@ -1436,7 +1436,7 @@ describe('$http', function() { it('should remove the request before firing callbacks', function() { - $httpBackend.when('GET').respond(200); + $httpBackend.when('GET', null).respond(200); $http({method: 'get', url: '/url'}).success(function() { expect($http.pendingRequests.length).toBe(0); }); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 35b394e47aac..cf142b62e930 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1010,6 +1010,13 @@ describe('ngMock', function() { }); + it('should not allow undefined URL', function(){ + expect(function() { + hb.when('GET').respond(200, ''); + }).toThrow('No URL defined!'); + }); + + it('should match headers if specified', function() { hb.when('GET', '/url', null, {'X': 'val1'}).respond(201, 'content1'); hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2'); @@ -1076,7 +1083,7 @@ describe('ngMock', function() { it('should match only method', function() { - hb.when('GET').respond(202, 'c'); + hb.when('GET', null).respond(202, 'c'); callback.andCallFake(function(status, response) { expect(status).toBe(202); expect(response).toBe('c'); @@ -1184,8 +1191,21 @@ describe('ngMock', function() { }); + it('should allow null as any url', function(){ + hb.expect('GET', null).respond(200, ''); + expect(function() { + hb('GET', '/url'); + }).not.toThrow(); + + hb.expect('GET', '/url1').respond(200, ''); + expect(function() { + hb('GET', '/url2'); + }).toThrow('Unexpected request: GET /url2\nExpected GET /url1'); + }); + + it ('should throw exception when only headers differs from expectation', function() { - hb.when('GET').respond(200, '', {}); + hb.when('GET', null).respond(200, '', {}); hb.expect('GET', '/match', undefined, {'Content-Type': 'application/json'}); expect(function() { @@ -1196,7 +1216,7 @@ describe('ngMock', function() { it ('should throw exception when only data differs from expectation', function() { - hb.when('GET').respond(200, '', {}); + hb.when('GET', null).respond(200, '', {}); hb.expect('GET', '/match', 'some-data'); expect(function() { @@ -1207,7 +1227,7 @@ 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.when('GET', null).respond(200, '', {}); hb.expect('GET', '/match', {a: 1, b: 2}); expect(function() { @@ -1222,7 +1242,7 @@ describe('ngMock', function() { it ('should throw exception when only parsed body differs from expected body object', function() { - hb.when('GET').respond(200, '', {}); + hb.when('GET', null).respond(200, '', {}); hb.expect('GET', '/match', {a: 1, b: 2}); expect(function() { @@ -1251,7 +1271,7 @@ describe('ngMock', function() { describe('flush()', function() { it('flush() should flush requests fired during callbacks', function() { - hb.when('GET').respond(200, ''); + hb.when('GET', null).respond(200, ''); hb('GET', '/some', null, function() { hb('GET', '/other', null, callback); }); @@ -1262,7 +1282,7 @@ describe('ngMock', function() { it('should flush given number of pending requests', function() { - hb.when('GET').respond(200, ''); + hb.when('GET', null).respond(200, ''); hb('GET', '/some', null, callback); hb('GET', '/some', null, callback); hb('GET', '/some', null, callback); @@ -1274,7 +1294,7 @@ describe('ngMock', function() { it('should throw exception when flushing more requests than pending', function() { - hb.when('GET').respond(200, ''); + hb.when('GET', null).respond(200, ''); hb('GET', '/url', null, callback); expect(function() {hb.flush(2);}).toThrow('No more pending request to flush !'); @@ -1285,7 +1305,7 @@ describe('ngMock', function() { it('should throw exception when no request to flush', function() { expect(function() {hb.flush();}).toThrow('No pending request to flush !'); - hb.when('GET').respond(200, ''); + hb.when('GET', null).respond(200, ''); hb('GET', '/some', null, callback); hb.flush(); @@ -1387,7 +1407,7 @@ describe('ngMock', function() { describe('verifyRequests', function() { it('should throw exception if not all requests were flushed', function() { - hb.when('GET').respond(200); + hb.when('GET', null).respond(200); hb('GET', '/some', null, noop, {}); expect(function() { diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 6e58976355bd..c9c35d77f9f3 100644 --- a/test/ngResource/resourceSpec.js +++ b/test/ngResource/resourceSpec.js @@ -394,7 +394,7 @@ describe("resource", function() { it('should handle multiple params with same name', function() { var R = $resource('/:id/:id'); - $httpBackend.when('GET').respond('{}'); + $httpBackend.when('GET', null).respond('{}'); $httpBackend.expect('GET', '/1/1'); R.get({id:1});