From 3c34b2b752b9983e8d15ae8f91d438617361f234 Mon Sep 17 00:00:00 2001 From: Kaspar Falkenberg Date: Sun, 3 Aug 2014 16:48:53 +0300 Subject: [PATCH 1/3] refactor(mocks) : changed the implementations of expect any URL from undefined/null to * --- src/ngMock/angular-mocks.js | 3 ++- test/ng/httpSpec.js | 12 +++++------ test/ngMock/angular-mocksSpec.js | 35 +++++++++++++++++++++++++------- test/ngResource/resourceSpec.js | 2 +- 4 files changed, 37 insertions(+), 15 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 2dd4fef5ea3f..16c96b7deae8 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1548,7 +1548,8 @@ function MockHttpExpectation(method, url, data, headers) { }; this.matchUrl = function(u) { - if (!url) return true; + if (!url) return false; + if (url === "*") 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..3f7a7ec868c5 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', '*').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', '*').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', '*').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', '*').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', '*').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', '*').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..0691d0b23a88 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1010,6 +1010,14 @@ describe('ngMock', function() { }); + it('should not allow undefined URL', function(){ + hb.when('GET').respond(200, ''); + expect(function() { + hb('GET', '/xxx'); + }).toThrow('Unexpected request: GET /xxx\nNo more request expected'); + }); + + 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 +1084,7 @@ describe('ngMock', function() { it('should match only method', function() { - hb.when('GET').respond(202, 'c'); + hb.when('GET', '*').respond(202, 'c'); callback.andCallFake(function(status, response) { expect(status).toBe(202); expect(response).toBe('c'); @@ -1184,6 +1192,19 @@ describe('ngMock', function() { }); + it('should allow * as any url', function(){ + hb.expect('GET', '*').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.expect('GET', '/match', undefined, {'Content-Type': 'application/json'}); @@ -1207,7 +1228,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', '*').respond(200, '', {}); hb.expect('GET', '/match', {a: 1, b: 2}); expect(function() { @@ -1251,7 +1272,7 @@ describe('ngMock', function() { describe('flush()', function() { it('flush() should flush requests fired during callbacks', function() { - hb.when('GET').respond(200, ''); + hb.when('GET', '*').respond(200, ''); hb('GET', '/some', null, function() { hb('GET', '/other', null, callback); }); @@ -1262,7 +1283,7 @@ describe('ngMock', function() { it('should flush given number of pending requests', function() { - hb.when('GET').respond(200, ''); + hb.when('GET', '*').respond(200, ''); hb('GET', '/some', null, callback); hb('GET', '/some', null, callback); hb('GET', '/some', null, callback); @@ -1274,7 +1295,7 @@ describe('ngMock', function() { it('should throw exception when flushing more requests than pending', function() { - hb.when('GET').respond(200, ''); + hb.when('GET', '*').respond(200, ''); hb('GET', '/url', null, callback); expect(function() {hb.flush(2);}).toThrow('No more pending request to flush !'); @@ -1285,7 +1306,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', '*').respond(200, ''); hb('GET', '/some', null, callback); hb.flush(); @@ -1387,7 +1408,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', '*').respond(200); hb('GET', '/some', null, noop, {}); expect(function() { diff --git a/test/ngResource/resourceSpec.js b/test/ngResource/resourceSpec.js index 6e58976355bd..974a0f9c4a44 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', '*').respond('{}'); $httpBackend.expect('GET', '/1/1'); R.get({id:1}); From 9fc70e7882c34c92aa5c95de9215e71f737dce31 Mon Sep 17 00:00:00 2001 From: Kaspar Falkenberg Date: Mon, 4 Aug 2014 09:28:14 +0300 Subject: [PATCH 2/3] refactor(mocks) : changed * to null, since * is valid URL char --- src/ngMock/angular-mocks.js | 4 ++-- test/ng/httpSpec.js | 12 ++++++------ test/ngMock/angular-mocksSpec.js | 18 +++++++++--------- test/ngResource/resourceSpec.js | 2 +- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 16c96b7deae8..a55d57b2c215 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1548,8 +1548,8 @@ function MockHttpExpectation(method, url, data, headers) { }; this.matchUrl = function(u) { - if (!url) return false; - if (url === "*") return true; + if (url === undefined) return false; + 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 3f7a7ec868c5..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 0691d0b23a88..4022b543aa54 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1084,7 +1084,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'); @@ -1192,8 +1192,8 @@ describe('ngMock', function() { }); - it('should allow * as any url', function(){ - hb.expect('GET', '*').respond(200, ''); + it('should allow null as any url', function(){ + hb.expect('GET', null).respond(200, ''); expect(function() { hb('GET', '/url'); }).not.toThrow(); @@ -1228,7 +1228,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() { @@ -1272,7 +1272,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); }); @@ -1283,7 +1283,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); @@ -1295,7 +1295,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 !'); @@ -1306,7 +1306,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(); @@ -1408,7 +1408,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 974a0f9c4a44..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}); From ecd3dc1009110dc206eef77c4ddae181536ebc67 Mon Sep 17 00:00:00 2001 From: Kaspar Falkenberg Date: Mon, 4 Aug 2014 19:57:38 +0300 Subject: [PATCH 3/3] refactor(mocks) : changed Expect to throw new Error instead of return false --- src/ngMock/angular-mocks.js | 3 ++- test/ngMock/angular-mocksSpec.js | 11 +++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index a55d57b2c215..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,6 @@ function MockHttpExpectation(method, url, data, headers) { }; this.matchUrl = function(u) { - if (url === undefined) return false; if (url === null) return true; if (angular.isFunction(url.test)) return url.test(u); if (angular.isFunction(url)) return url(u); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index 4022b543aa54..cf142b62e930 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -1011,10 +1011,9 @@ describe('ngMock', function() { it('should not allow undefined URL', function(){ - hb.when('GET').respond(200, ''); expect(function() { - hb('GET', '/xxx'); - }).toThrow('Unexpected request: GET /xxx\nNo more request expected'); + hb.when('GET').respond(200, ''); + }).toThrow('No URL defined!'); }); @@ -1206,7 +1205,7 @@ describe('ngMock', function() { 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() { @@ -1217,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() { @@ -1243,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() {