From 789080ab2c12da4061262e7043f1c4cbdd30d79a Mon Sep 17 00:00:00 2001 From: Michael Coyne Date: Wed, 22 Jan 2014 20:58:04 -0500 Subject: [PATCH] feat($http) XHR progress events Add to $http and $httpBackend to open up progress events. Update specs and mocks to handle the new argument and to test for the $http promise notify method. Closes #1934 --- src/ng/http.js | 16 ++++- src/ng/httpBackend.js | 10 ++- src/ngMock/angular-mocks.js | 6 +- test/ng/httpBackendSpec.js | 64 ++++++++++--------- test/ng/httpSpec.js | 21 ++++--- test/ngMock/angular-mocksSpec.js | 105 ++++++++++++++++--------------- 6 files changed, 130 insertions(+), 92 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index f20d54fd60db..576aaaf10e1b 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -730,6 +730,13 @@ function $HttpProvider() { return promise; }; + promise.notify = function(fn) { + promise.then(null, null, function(event) { + fn(event, config); + }); + return promise; + }; + return promise; function transformResponse(response) { @@ -959,7 +966,7 @@ function $HttpProvider() { // if we won't have the response in cache, send the request to the backend if (isUndefined(cachedResp)) { - $httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout, + $httpBackend(config.method, url, reqData, progress, done, reqHeaders, config.timeout, config.withCredentials, config.responseType); } @@ -986,6 +993,13 @@ function $HttpProvider() { if (!$rootScope.$$phase) $rootScope.$apply(); } + /** + * Progress callback for $httpBackend + */ + function progress(event) { + deferred.notify(event); + } + /** * Resolves the raw $http promise. diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index f52e461157f0..d8c0e3e1b6df 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -36,7 +36,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc var ABORTED = -1; // TODO(vojta): fix the signature - return function(method, url, post, callback, headers, timeout, withCredentials, responseType) { + return function(method, url, post, progressback, callback, headers, timeout, withCredentials, responseType) { var status; $browser.$$incOutstandingRequestCount(); url = url || $browser.url(); @@ -97,6 +97,14 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc } }; + if (xhr.onprogress !== undefined) { + xhr.onprogress = progressback; + + if (xhr.upload !== undefined) { + xhr.upload.onprogress = progressback; + } + } + if (withCredentials) { xhr.withCredentials = true; } diff --git a/src/ngMock/angular-mocks.js b/src/ngMock/angular-mocks.js index 292ef4af34e0..2b91b954d6a0 100644 --- a/src/ngMock/angular-mocks.js +++ b/src/ngMock/angular-mocks.js @@ -1131,7 +1131,8 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { } // TODO(vojta): change params to: method, url, data, headers, callback - function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) { + function $httpBackend(method, url, data, progressback, callback, headers, timeout, + withCredentials) { var xhr = new MockXhr(), expectation = expectations[0], wasExpected = false; @@ -1190,7 +1191,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) { // if $browser specified, we do auto flush all requests ($browser ? $browser.defer : responsesPush)(wrapResponse(definition)); } else if (definition.passThrough) { - $delegate(method, url, data, callback, headers, timeout, withCredentials); + $delegate(method, url, data, progressback, callback, headers, timeout, withCredentials); } else throw new Error('No response defined !'); return; } @@ -1654,6 +1655,7 @@ function MockXhr() { }; this.abort = angular.noop; + this.onprogress = angular.noop; } diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index 2a3f60126737..115e820fc8be 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -59,7 +59,7 @@ describe('$httpBackend', function() { it('should do basics - open async xhr and send data', function() { - $backend('GET', '/some-url', 'some-data', noop); + $backend('GET', '/some-url', 'some-data', noop, noop); xhr = MockXhr.$$lastInstance; expect(xhr.$$method).toBe('GET'); @@ -80,7 +80,7 @@ describe('$httpBackend', function() { expect(status).toBe(204); }); - $backend('GET', 'URL', null, callback); + $backend('GET', 'URL', null, noop, callback); xhr = MockXhr.$$lastInstance; xhr.status = 1223; @@ -94,7 +94,7 @@ describe('$httpBackend', function() { // with readyState === 4 on mobile webkit caused by // xhrs that are resolved while the app is in the background (see #5426). it('should not process onreadystatechange callback with readyState == 4 more than once', function() { - $backend('GET', 'URL', null, callback); + $backend('GET', 'URL', null, noop, callback); xhr = MockXhr.$$lastInstance; xhr.status = 200; @@ -106,7 +106,7 @@ describe('$httpBackend', function() { }); it('should set only the requested headers', function() { - $backend('POST', 'URL', null, noop, {'X-header1': 'value1', 'X-header2': 'value2'}); + $backend('POST', 'URL', null, noop, noop, {'X-header1': 'value1', 'X-header2': 'value2'}); xhr = MockXhr.$$lastInstance; expect(xhr.$$reqHeaders).toEqual({ @@ -116,7 +116,7 @@ describe('$httpBackend', function() { }); it('should set requested headers even if they have falsy values', function() { - $backend('POST', 'URL', null, noop, { + $backend('POST', 'URL', null, noop, noop, { 'X-header1': 0, 'X-header2': '', 'X-header3': false, @@ -138,7 +138,7 @@ describe('$httpBackend', function() { expect(response).toBe(null); expect(headers).toBe(null); }); - $backend('GET', '/url', null, callback, {}, 2000); + $backend('GET', '/url', null, noop, callback, {}, 2000); xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); @@ -156,7 +156,7 @@ describe('$httpBackend', function() { expect(status).toBe(-1); }); - $backend('GET', '/url', null, callback, {}, 2000); + $backend('GET', '/url', null, noop, callback, {}, 2000); xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); @@ -177,7 +177,7 @@ describe('$httpBackend', function() { expect(status).toBe(-1); }); - $backend('GET', '/url', null, callback, {}, $timeout(noop, 2000)); + $backend('GET', '/url', null, noop, callback, {}, $timeout(noop, 2000)); xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); @@ -196,7 +196,7 @@ describe('$httpBackend', function() { expect(status).toBe(200); }); - $backend('GET', '/url', null, callback, {}, $timeout(noop, 2000)); + $backend('GET', '/url', null, noop, callback, {}, $timeout(noop, 2000)); xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); @@ -215,7 +215,7 @@ describe('$httpBackend', function() { expect(status).toBe(200); }); - $backend('GET', '/url', null, callback, {}, 2000); + $backend('GET', '/url', null, noop, callback, {}, 2000); xhr = MockXhr.$$lastInstance; spyOn(xhr, 'abort'); @@ -253,21 +253,20 @@ describe('$httpBackend', function() { }); $backend = createHttpBackend($browser, function() { return new SyncXhr() }); - $backend('GET', '/url', null, callback); + $backend('GET', '/url', null, noop, callback); expect(callback).toHaveBeenCalledOnce(); }); it('should set withCredentials', function() { - $backend('GET', '/some.url', null, callback, {}, null, true); + $backend('GET', '/some.url', null, noop, callback, {}, null, true); expect(MockXhr.$$lastInstance.withCredentials).toBe(true); }); describe('responseType', function() { - it('should set responseType and return xhr.response', function() { - $backend('GET', '/whatever', null, callback, {}, null, null, 'blob'); + $backend('GET', '/whatever', null, noop, callback, {}, null, null, 'blob'); var xhrInstance = MockXhr.$$lastInstance; expect(xhrInstance.responseType).toBe('blob'); @@ -287,7 +286,7 @@ describe('$httpBackend', function() { it('should read responseText if response was not defined', function() { // old browsers like IE8, don't support responseType, so they always respond with responseText - $backend('GET', '/whatever', null, callback, {}, null, null, 'blob'); + $backend('GET', '/whatever', null, noop, callback, {}, null, null, 'blob'); var xhrInstance = MockXhr.$$lastInstance; var responseText = '{"some": "object"}'; @@ -305,6 +304,13 @@ describe('$httpBackend', function() { }); }); + it('should call progress callback', function() { + $backend('POST', '/whatever', null, callback, noop, {}, null, null, 'blob'); + + MockXhr.$$lastInstance.onprogress(); + + expect(callback).toHaveBeenCalledOnce(); + }); describe('JSONP', function() { @@ -317,7 +323,7 @@ describe('$httpBackend', function() { expect(response).toBe('some-data'); }); - $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback); + $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop, callback); expect(fakeDocument.$$scripts.length).toBe(1); var script = fakeDocument.$$scripts.shift(), @@ -338,7 +344,7 @@ describe('$httpBackend', function() { it('should clean up the callback and remove the script', function() { - $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback); + $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop, callback); expect(fakeDocument.$$scripts.length).toBe(1); @@ -362,7 +368,7 @@ describe('$httpBackend', function() { if(msie<=8) { it('should attach onreadystatechange handler to the script object', function() { - $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop); + $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop, noop); expect(fakeDocument.$$scripts[0].onreadystatechange).toEqual(jasmine.any(Function)); @@ -377,7 +383,7 @@ describe('$httpBackend', function() { } else { it('should attach onload and onerror handlers to the script object', function() { - $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop); + $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop, noop); expect(fakeDocument.$$scripts[0].onload).toEqual(jasmine.any(Function)); expect(fakeDocument.$$scripts[0].onerror).toEqual(jasmine.any(Function)); @@ -397,7 +403,7 @@ describe('$httpBackend', function() { expect(response).toBeUndefined(); }); - $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback); + $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop, callback); expect(fakeDocument.$$scripts.length).toBe(1); var script = fakeDocument.$$scripts.shift(); @@ -412,11 +418,11 @@ describe('$httpBackend', function() { it('should set url to current location if not specified or empty string', function() { - $backend('JSONP', undefined, null, callback); + $backend('JSONP', undefined, null, noop, callback); expect(fakeDocument.$$scripts[0].src).toBe($browser.url()); fakeDocument.$$scripts.shift(); - $backend('JSONP', '', null, callback); + $backend('JSONP', '', null, noop, callback); expect(fakeDocument.$$scripts[0].src).toBe($browser.url()); }); @@ -426,7 +432,7 @@ describe('$httpBackend', function() { expect(status).toBe(-1); }); - $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback, null, 2000); + $backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, noop, callback, null, 2000); expect(fakeDocument.$$scripts.length).toBe(1); expect(fakeTimeout.delays[0]).toBe(2000); @@ -459,7 +465,7 @@ describe('$httpBackend', function() { it('should convert 0 to 200 if content', function() { $backend = createHttpBackend($browser, createMockXhr); - $backend('GET', 'someProtocol:///whatever/index.html', null, callback); + $backend('GET', 'someProtocol:///whatever/index.html', null, noop, callback); respond(0, 'SOME CONTENT'); expect(callback).toHaveBeenCalled(); @@ -470,7 +476,7 @@ describe('$httpBackend', function() { it('should convert 0 to 404 if no content', function() { $backend = createHttpBackend($browser, createMockXhr); - $backend('GET', 'someProtocol:///whatever/index.html', null, callback); + $backend('GET', 'someProtocol:///whatever/index.html', null, noop, callback); respond(0, ''); expect(callback).toHaveBeenCalled(); @@ -498,7 +504,7 @@ describe('$httpBackend', function() { $backend = createHttpBackend($browser, createMockXhr); - $backend('GET', '/whatever/index.html', null, callback); + $backend('GET', '/whatever/index.html', null, noop, callback); respond(0, ''); expect(callback).toHaveBeenCalled(); @@ -514,7 +520,7 @@ describe('$httpBackend', function() { $backend = createHttpBackend($browser, createMockXhr); // request to http:// - $backend('POST', 'http://rest_api/create_whatever', null, callback); + $backend('POST', 'http://rest_api/create_whatever', null, noop, callback); respond(201, ''); expect(callback).toHaveBeenCalled(); @@ -522,14 +528,14 @@ describe('$httpBackend', function() { // request to file:// - $backend('POST', 'file://rest_api/create_whatever', null, callback); + $backend('POST', 'file://rest_api/create_whatever', null, noop, callback); respond(201, ''); expect(callback).toHaveBeenCalled(); expect(callback.mostRecentCall.args[0]).toBe(201); // request to file:// with HTTP status >= 300 - $backend('POST', 'file://rest_api/create_whatever', null, callback); + $backend('POST', 'file://rest_api/create_whatever', null, noop, callback); respond(503, ''); expect(callback).toHaveBeenCalled(); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index a0417d9ad99c..61702a3beef0 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -489,8 +489,8 @@ describe('$http', function() { describe('success', function() { it('should allow http specific callbacks to be registered via "success"', function() { - $httpBackend.expect('GET', '/url').respond(207, 'my content', {'content-encoding': 'smurf'}); - $http({url: '/url', method: 'GET'}).success(function(data, status, headers, config) { + $httpBackend.expect('POST', '/url').respond(207, 'my content', {'content-encoding': 'smurf'}); + $http({url: '/url', method: 'POST'}).success(function(data, status, headers, config) { expect(data).toBe('my content'); expect(status).toBe(207); expect(headers()).toEqual({'content-encoding': 'smurf'}); @@ -504,8 +504,8 @@ describe('$http', function() { it('should return the original http promise', function() { - $httpBackend.expect('GET', '/url').respond(207, 'my content', {'content-encoding': 'smurf'}); - var httpPromise = $http({url: '/url', method: 'GET'}); + $httpBackend.expect('POST', '/url').respond(207, 'my content', {'content-encoding': 'smurf'}); + var httpPromise = $http({url: '/url', method: 'POST'}); expect(httpPromise.success(callback)).toBe(httpPromise); }); }); @@ -533,8 +533,15 @@ describe('$http', function() { expect(httpPromise.error(callback)).toBe(httpPromise); }); }); - }); + describe('notify', function() { + it('should return the original http promise', function() { + $httpBackend.expect('GET', '/url').respond(207, 'my content', {'content-encoding': 'smurf'}); + var httpPromise = $http({url: '/url', method: 'GET'}); + expect(httpPromise.notify(callback)).toBe(httpPromise); + }); + }); + }); describe('response headers', function() { @@ -1452,7 +1459,7 @@ describe('$http', function() { it('should pass timeout, withCredentials and responseType', function() { var $httpBackend = jasmine.createSpy('$httpBackend'); - $httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) { + $httpBackend.andCallFake(function(m, u, d, p, c, h, timeout, withCredentials, responseType) { expect(timeout).toBe(12345); expect(withCredentials).toBe(true); expect(responseType).toBe('json'); @@ -1477,7 +1484,7 @@ describe('$http', function() { it('should use withCredentials from default', function() { var $httpBackend = jasmine.createSpy('$httpBackend'); - $httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) { + $httpBackend.andCallFake(function(m, u, d, p, c, h, timeout, withCredentials, responseType) { expect(withCredentials).toBe(true); }); diff --git a/test/ngMock/angular-mocksSpec.js b/test/ngMock/angular-mocksSpec.js index fb602adc2ace..8759b3f21973 100644 --- a/test/ngMock/angular-mocksSpec.js +++ b/test/ngMock/angular-mocksSpec.js @@ -909,7 +909,7 @@ describe('ngMock', function() { expect(response).toBe('content'); }); - hb('GET', '/url1', null, callback); + hb('GET', '/url1', null, noop, callback); expect(callback).not.toHaveBeenCalled(); hb.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -928,14 +928,14 @@ describe('ngMock', function() { response.a = 'c'; }); - hb('GET', '/url1', null, callback); + hb('GET', '/url1', null, noop, callback); hb.flush(); expect(callback).toHaveBeenCalledOnce(); // Fire it again and verify that the returned mock data has not been // modified. callback.reset(); - hb('GET', '/url1', null, callback); + hb('GET', '/url1', null, noop, callback); hb.flush(); expect(callback).toHaveBeenCalledOnce(); expect(mockObject).toEqual({a: 'b'}); @@ -955,17 +955,17 @@ describe('ngMock', function() { hb.when('GET', '/url', null, {'X': 'val2'}).respond(202, 'content2'); hb.when('GET', '/url').respond(203, 'content3'); - hb('GET', '/url', null, function(status, response) { + hb('GET', '/url', null, noop, function(status, response) { expect(status).toBe(203); expect(response).toBe('content3'); }); - hb('GET', '/url', null, function(status, response) { + hb('GET', '/url', null, noop, function(status, response) { expect(status).toBe(201); expect(response).toBe('content1'); }, {'X': 'val1'}); - hb('GET', '/url', null, function(status, response) { + hb('GET', '/url', null, noop, function(status, response) { expect(status).toBe(202); expect(response).toBe('content2'); }, {'X': 'val2'}); @@ -978,12 +978,12 @@ describe('ngMock', function() { hb.when('GET', '/a/b', '{a: true}').respond(201, 'content1'); hb.when('GET', '/a/b').respond(202, 'content2'); - hb('GET', '/a/b', '{a: true}', function(status, response) { + hb('GET', '/a/b', '{a: true}', noop, function(status, response) { expect(status).toBe(201); expect(response).toBe('content1'); }); - hb('GET', '/a/b', null, function(status, response) { + hb('GET', '/a/b', null, noop, function(status, response) { expect(status).toBe(202); expect(response).toBe('content2'); }); @@ -996,17 +996,17 @@ describe('ngMock', 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) { + hb('GET', '/a/b', '{"a":1,"b":2}', noop, function(status, response) { expect(status).toBe(201); expect(response).toBe('content1'); }); - hb('GET', '/a/b', '{"b":2,"a":1}', function(status, response) { + hb('GET', '/a/b', '{"b":2,"a":1}', noop, function(status, response) { expect(status).toBe(201); expect(response).toBe('content1'); }); - hb('GET', '/a/b', null, function(status, response) { + hb('GET', '/a/b', null, noop, function(status, response) { expect(status).toBe(202); expect(response).toBe('content2'); }); @@ -1022,9 +1022,9 @@ describe('ngMock', function() { expect(response).toBe('c'); }); - hb('GET', '/some', null, callback, {}); - hb('GET', '/another', null, callback, {'X-Fake': 'Header'}); - hb('GET', '/third', 'some-data', callback, {}); + hb('GET', '/some', null, noop, callback, {}); + hb('GET', '/another', null, noop, callback, {'X-Fake': 'Header'}); + hb('GET', '/third', 'some-data', noop, callback, {}); hb.flush(); expect(callback).toHaveBeenCalled(); @@ -1035,8 +1035,8 @@ describe('ngMock', function() { hb.when('GET', '/url1').respond(200, 'first'); hb.when('GET', '/url2').respond(201, 'second'); - hb('GET', '/url2', null, callback); - hb('GET', '/url1', null, callback); + hb('GET', '/url2', null, noop, callback); + hb('GET', '/url1', null, noop, callback); hb.flush(); @@ -1049,7 +1049,7 @@ describe('ngMock', function() { describe('respond()', function() { it('should take values', function() { hb.expect('GET', '/url1').respond(200, 'first', {'header': 'val'}); - hb('GET', '/url1', undefined, callback); + hb('GET', '/url1', undefined, noop, callback); hb.flush(); expect(callback).toHaveBeenCalledOnceWith(200, 'first', 'header: val'); @@ -1060,7 +1060,7 @@ describe('ngMock', function() { return [301, m + u + ';' + d + ';a=' + h.a, {'Connection': 'keep-alive'}]; }); - hb('GET', '/some', 'data', callback, {a: 'b'}); + hb('GET', '/some', 'data', noop, callback, {a: 'b'}); hb.flush(); expect(callback).toHaveBeenCalledOnceWith(301, 'GET/some;data;a=b', 'Connection: keep-alive'); @@ -1074,8 +1074,8 @@ describe('ngMock', function() { hb.expect('GET', '/url1').respond('some-data'); hb.expect('GET', '/url2').respond('some-data', {'X-Header': 'true'}); - hb('GET', '/url1', null, callback); - hb('GET', '/url2', null, callback); + hb('GET', '/url1', null, noop, callback); + hb('GET', '/url2', null, noop, callback); hb.flush(); expect(callback).toHaveBeenCalled(); expect(callback.callCount).toBe(2); @@ -1086,8 +1086,8 @@ describe('ngMock', function() { hb.expect('GET', '/url1').respond(200, 'first'); hb.expect('GET', '/url2').respond('second'); - hb('GET', '/url1', null, callback); - hb('GET', '/url2', null, callback); + hb('GET', '/url1', null, noop, callback); + hb('GET', '/url2', null, noop, callback); hb.flush(); @@ -1104,7 +1104,7 @@ describe('ngMock', function() { hb.expect('GET', '/url2').respond(200, ''); expect(function() { - hb('GET', '/url2', null, noop, {}); + hb('GET', '/url2', null, noop, noop, {}); }).toThrow('Unexpected request: GET /url2\nExpected GET /url1'); }); @@ -1118,7 +1118,7 @@ describe('ngMock', function() { hb.when('GET', '/url').respond(200, 'when'); hb.expect('GET', '/url').respond(300, 'expect'); - hb('GET', '/url', null, callback, {}); + hb('GET', '/url', null, noop, callback, {}); hb.flush(); expect(callback).toHaveBeenCalledOnce(); }); @@ -1129,7 +1129,7 @@ describe('ngMock', function() { hb.expect('GET', '/match', undefined, {'Content-Type': 'application/json'}); expect(function() { - hb('GET', '/match', null, noop, {}); + hb('GET', '/match', null, noop, noop, {}); }).toThrow('Expected GET /match with different headers\n' + 'EXPECTED: {"Content-Type":"application/json"}\nGOT: {}'); }); @@ -1140,7 +1140,7 @@ describe('ngMock', function() { hb.expect('GET', '/match', 'some-data'); expect(function() { - hb('GET', '/match', 'different', noop, {}); + hb('GET', '/match', 'different', noop, noop, {}); }).toThrow('Expected GET /match with different data\n' + 'EXPECTED: some-data\nGOT: different'); }); @@ -1151,12 +1151,12 @@ describe('ngMock', function() { hb.expect('GET', '/match', {a: 1, b: 2}); expect(function() { - hb('GET', '/match', '{"a":1,"b":2}', noop, {}); + hb('GET', '/match', '{"a":1,"b":2}', noop, noop, {}); }).not.toThrow(); hb.expect('GET', '/match', {a: 1, b: 2}); expect(function() { - hb('GET', '/match', '{"b":2,"a":1}', noop, {}); + hb('GET', '/match', '{"b":2,"a":1}', noop, noop, {}); }).not.toThrow(); }); @@ -1166,7 +1166,7 @@ describe('ngMock', function() { hb.expect('GET', '/match', {a: 1, b: 2}); expect(function() { - hb('GET', '/match', '{"a":1,"b":3}', noop, {}); + hb('GET', '/match', '{"a":1,"b":3}', noop, noop, {}); }).toThrow('Expected GET /match with different data\n' + 'EXPECTED: {"a":1,"b":2}\nGOT: {"a":1,"b":3}'); }); @@ -1180,7 +1180,7 @@ describe('ngMock', function() { hb.when('GET', '/some').respond(201, 'data'); hb.expect('GET', '/some'); - hb('GET', '/some', null, callback); + hb('GET', '/some', null, noop, callback); hb.flush(); expect(callback).toHaveBeenCalled(); @@ -1192,8 +1192,8 @@ describe('ngMock', function() { describe('flush()', function() { it('flush() should flush requests fired during callbacks', function() { hb.when('GET').respond(200, ''); - hb('GET', '/some', null, function() { - hb('GET', '/other', null, callback); + hb('GET', '/some', null, noop, function() { + hb('GET', '/other', null, noop, callback); }); hb.flush(); @@ -1203,9 +1203,9 @@ describe('ngMock', function() { it('should flush given number of pending requests', function() { hb.when('GET').respond(200, ''); - hb('GET', '/some', null, callback); - hb('GET', '/some', null, callback); - hb('GET', '/some', null, callback); + hb('GET', '/some', null, noop, callback); + hb('GET', '/some', null, noop, callback); + hb('GET', '/some', null, noop, callback); hb.flush(2); expect(callback).toHaveBeenCalled(); @@ -1215,7 +1215,7 @@ describe('ngMock', function() { it('should throw exception when flushing more requests than pending', function() { hb.when('GET').respond(200, ''); - hb('GET', '/url', null, callback); + hb('GET', '/url', null, noop, callback); expect(function() {hb.flush(2);}).toThrow('No more pending request to flush !'); expect(callback).toHaveBeenCalledOnce(); @@ -1226,7 +1226,7 @@ describe('ngMock', function() { expect(function() {hb.flush();}).toThrow('No pending request to flush !'); hb.when('GET').respond(200, ''); - hb('GET', '/some', null, callback); + hb('GET', '/some', null, noop, callback); hb.flush(); expect(function() {hb.flush();}).toThrow('No pending request to flush !'); @@ -1237,7 +1237,7 @@ describe('ngMock', function() { hb.expect('GET', '/url1').respond(); hb.expect('GET', '/url2').respond(); - hb('GET', '/url1', null, angular.noop); + hb('GET', '/url1', null, noop, noop); expect(function() {hb.flush();}).toThrow('Unsatisfied requests: GET /url2'); }); }); @@ -1250,7 +1250,7 @@ describe('ngMock', function() { canceler = fn; }); - hb('GET', '/url1', null, callback, null, {then: then}); + hb('GET', '/url1', null, noop, callback, null, {then: then}); expect(typeof canceler).toBe('function'); canceler(); // simulate promise resolution @@ -1264,7 +1264,7 @@ describe('ngMock', function() { it('should throw an exception if no response defined', function() { hb.when('GET', '/test'); expect(function() { - hb('GET', '/test', null, callback); + hb('GET', '/test', null, noop, callback); }).toThrow('No response defined !'); }); @@ -1272,7 +1272,7 @@ describe('ngMock', function() { it('should throw an exception if no response for exception and no definition', function() { hb.expect('GET', '/url'); expect(function() { - hb('GET', '/url', null, callback); + hb('GET', '/url', null, noop, callback); }).toThrow('No response defined !'); }); @@ -1292,13 +1292,12 @@ describe('ngMock', function() { describe('verifyExpectations', function() { - it('should throw exception if not all expectations were satisfied', function() { hb.expect('POST', '/u1', 'ddd').respond(201, '', {}); hb.expect('GET', '/u2').respond(200, '', {}); hb.expect('POST', '/u3').respond(201, '', {}); - hb('POST', '/u1', 'ddd', noop, {}); + hb('POST', '/u1', 'ddd', noop, noop, {}); expect(function() {hb.verifyNoOutstandingExpectation();}). toThrow('Unsatisfied requests: GET /u2, POST /u3'); @@ -1328,7 +1327,7 @@ describe('ngMock', function() { it('should throw exception if not all requests were flushed', function() { hb.when('GET').respond(200); - hb('GET', '/some', null, noop, {}); + hb('GET', '/some', null, noop, noop, {}); expect(function() { hb.verifyNoOutstandingRequest(); @@ -1352,11 +1351,11 @@ describe('ngMock', function() { var cancelledClb = jasmine.createSpy('cancelled'); hb.expect('GET', '/url').respond(200, ''); - hb('GET', '/url', null, cancelledClb); + hb('GET', '/url', null, noop, cancelledClb); hb.resetExpectations(); hb.expect('GET', '/url').respond(300, ''); - hb('GET', '/url', null, callback, {}); + hb('GET', '/url', null, noop, callback, {}); hb.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -1368,10 +1367,10 @@ describe('ngMock', function() { var cancelledClb = jasmine.createSpy('cancelled'); hb.when('GET', '/url').respond(200, 'success'); - hb('GET', '/url', null, cancelledClb); + hb('GET', '/url', null, noop, cancelledClb); hb.resetExpectations(); - hb('GET', '/url', null, callback, {}); + hb('GET', '/url', null, noop, callback, {}); hb.flush(); expect(callback).toHaveBeenCalledOnce(); @@ -1386,7 +1385,7 @@ describe('ngMock', function() { var shortcut = prefix + method; it('should provide ' + shortcut + ' shortcut method', function() { hb[shortcut]('/foo').respond('bar'); - hb(method, '/foo', undefined, callback); + hb(method, '/foo', null, noop, callback); hb.flush(); expect(callback).toHaveBeenCalledOnceWith(200, 'bar', ''); }); @@ -1461,6 +1460,8 @@ describe('ngMock', function() { describe('ngMockE2E', function() { + var noop = angular.noop; + describe('$httpBackend', function() { var hb, realHttpBackend, callback; @@ -1480,10 +1481,10 @@ describe('ngMockE2E', function() { describe('passThrough()', function() { it('should delegate requests to the real backend when passThrough is invoked', function() { hb.when('GET', /\/passThrough\/.*/).passThrough(); - hb('GET', '/passThrough/23', null, callback, {}, null, true); + hb('GET', '/passThrough/23', null, noop, callback, {}, null, true); expect(realHttpBackend).toHaveBeenCalledOnceWith( - 'GET', '/passThrough/23', null, callback, {}, null, true); + 'GET', '/passThrough/23', null, noop, callback, {}, null, true); }); }); @@ -1491,7 +1492,7 @@ describe('ngMockE2E', function() { describe('autoflush', function() { it('should flush responses via $browser.defer', inject(function($browser) { hb.when('GET', '/foo').respond('bar'); - hb('GET', '/foo', null, callback); + hb('GET', '/foo', null, noop, callback); expect(callback).not.toHaveBeenCalled(); $browser.defer.flush();