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

Commit 315872b

Browse files
committed
Refactored to use the config object instead of using timeout, withCredentials, responseType, etc
1 parent 29046d2 commit 315872b

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

src/ng/http.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -931,8 +931,7 @@ function $HttpProvider() {
931931
reqHeaders[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
932932
}
933933

934-
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
935-
config.withCredentials, config.responseType, config.createXhr);
934+
$httpBackend(config.method, url, reqData, done, reqHeaders, config);
936935
}
937936

938937
return promise;

src/ng/httpBackend.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,16 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
4040
var ABORTED = -1;
4141

4242
// TODO(vojta): fix the signature
43-
return function(method, url, post, callback, headers, timeout, withCredentials, responseType, customCreateXhr) {
43+
return function(method, url, post, callback, headers, config) {
4444
var status;
45+
if (!config) {
46+
config = {};
47+
}
48+
var timeout = config.timeout,
49+
withCredentials = config.withCredentials,
50+
responseType = config.responseType,
51+
customCreateXhr = config.createXhr;
52+
4553
$browser.$$incOutstandingRequestCount();
4654
url = url || $browser.url();
4755

@@ -60,7 +68,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
6068
} else {
6169

6270
var xhr;
63-
if (customCreateXhr && typeof customCreateXhr ==='function') {
71+
if (customCreateXhr && typeof customCreateXhr === 'function') {
6472
xhr = customCreateXhr(method);
6573
} else {
6674
xhr = createXhr(method);

src/ngMock/angular-mocks.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -1102,11 +1102,18 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11021102
}
11031103

11041104
// TODO(vojta): change params to: method, url, data, headers, callback
1105-
function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) {
1105+
function $httpBackend(method, url, data, callback, headers, config) {
11061106
var xhr = new MockXhr(),
11071107
expectation = expectations[0],
11081108
wasExpected = false;
11091109

1110+
if (!config) {
1111+
config = {};
1112+
}
1113+
1114+
var timeout = config.timeout,
1115+
withCredentials = config.withCredentials;
1116+
11101117
function prettyPrint(data) {
11111118
return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
11121119
? data
@@ -1162,7 +1169,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11621169
// if $browser specified, we do auto flush all requests
11631170
($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
11641171
} else if (definition.passThrough) {
1165-
$delegate(method, url, data, callback, headers, timeout, withCredentials);
1172+
$delegate(method, url, data, callback, headers, config);
11661173
} else throw new Error('No response defined !');
11671174
return;
11681175
}

test/ng/httpBackendSpec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ describe('$httpBackend', function() {
9797
expect(statusText).toBe((!msie || msie >= 10) ? 'OK' : '');
9898
});
9999

100-
$backend('GET', '/url', null, callback, {}, 2000);
100+
$backend('GET', '/url', null, callback, {}, {timeout: 2000});
101101
xhr = MockXhr.$$lastInstance;
102102
spyOn(xhr, 'abort');
103103

@@ -187,7 +187,7 @@ describe('$httpBackend', function() {
187187
expect(response).toBe(null);
188188
expect(headers).toBe(null);
189189
});
190-
$backend('GET', '/url', null, callback, {}, 2000);
190+
$backend('GET', '/url', null, callback, {}, {timeout: 2000});
191191
xhr = MockXhr.$$lastInstance;
192192
spyOn(xhr, 'abort');
193193

@@ -205,7 +205,7 @@ describe('$httpBackend', function() {
205205
expect(status).toBe(-1);
206206
});
207207

208-
$backend('GET', '/url', null, callback, {}, 2000);
208+
$backend('GET', '/url', null, callback, {}, {timeout: 2000});
209209
xhr = MockXhr.$$lastInstance;
210210
spyOn(xhr, 'abort');
211211

@@ -226,7 +226,7 @@ describe('$httpBackend', function() {
226226
expect(status).toBe(-1);
227227
});
228228

229-
$backend('GET', '/url', null, callback, {}, $timeout(noop, 2000));
229+
$backend('GET', '/url', null, callback, {}, {timeout: $timeout(noop, 2000)});
230230
xhr = MockXhr.$$lastInstance;
231231
spyOn(xhr, 'abort');
232232

@@ -245,7 +245,7 @@ describe('$httpBackend', function() {
245245
expect(status).toBe(200);
246246
});
247247

248-
$backend('GET', '/url', null, callback, {}, $timeout(noop, 2000));
248+
$backend('GET', '/url', null, callback, {}, {timeout: $timeout(noop, 2000)});
249249
xhr = MockXhr.$$lastInstance;
250250
spyOn(xhr, 'abort');
251251

@@ -264,7 +264,7 @@ describe('$httpBackend', function() {
264264
expect(status).toBe(200);
265265
});
266266

267-
$backend('GET', '/url', null, callback, {}, 2000);
267+
$backend('GET', '/url', null, callback, {}, {timeout: 2000});
268268
xhr = MockXhr.$$lastInstance;
269269
spyOn(xhr, 'abort');
270270

@@ -308,7 +308,7 @@ describe('$httpBackend', function() {
308308

309309

310310
it('should set withCredentials', function() {
311-
$backend('GET', '/some.url', null, callback, {}, null, true);
311+
$backend('GET', '/some.url', null, callback, {}, {withCredentials: true});
312312
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
313313
});
314314

@@ -321,7 +321,7 @@ describe('$httpBackend', function() {
321321
describe('responseType', function() {
322322

323323
it('should set responseType and return xhr.response', function() {
324-
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');
324+
$backend('GET', '/whatever', null, callback, {}, {responseType: 'blob'});
325325

326326
var xhrInstance = MockXhr.$$lastInstance;
327327
expect(xhrInstance.responseType).toBe('blob');
@@ -341,7 +341,7 @@ describe('$httpBackend', function() {
341341
it('should read responseText if response was not defined', function() {
342342
// old browsers like IE8, don't support responseType, so they always respond with responseText
343343

344-
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');
344+
$backend('GET', '/whatever', null, callback, {}, {responseType: 'blob'});
345345

346346
var xhrInstance = MockXhr.$$lastInstance;
347347
var responseText = '{"some": "object"}';
@@ -416,7 +416,7 @@ describe('$httpBackend', function() {
416416
expect(status).toBe(-1);
417417
});
418418

419-
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback, null, 2000);
419+
$backend('JSONP', 'http://example.org/path?cb=JSON_CALLBACK', null, callback, null, {timeout: 2000});
420420
expect(fakeDocument.$$scripts.length).toBe(1);
421421
expect(fakeTimeout.delays[0]).toBe(2000);
422422

test/ng/httpSpec.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -1456,10 +1456,10 @@ describe('$http', function() {
14561456
it('should pass timeout, withCredentials and responseType', function() {
14571457
var $httpBackend = jasmine.createSpy('$httpBackend');
14581458

1459-
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
1460-
expect(timeout).toBe(12345);
1461-
expect(withCredentials).toBe(true);
1462-
expect(responseType).toBe('json');
1459+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1460+
expect(config.timeout).toBe(12345);
1461+
expect(config.withCredentials).toBe(true);
1462+
expect(config.responseType).toBe('json');
14631463
});
14641464

14651465
module(function($provide) {
@@ -1484,8 +1484,8 @@ describe('$http', function() {
14841484
it('should use withCredentials from default', function() {
14851485
var $httpBackend = jasmine.createSpy('$httpBackend');
14861486

1487-
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
1488-
expect(withCredentials).toBe(true);
1487+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1488+
expect(config.withCredentials).toBe(true);
14891489
});
14901490

14911491
module(function($provide) {
@@ -1511,8 +1511,8 @@ describe('$http', function() {
15111511
var $httpBackend = jasmine.createSpy('$httpBackend');
15121512
var dummyCreateXhr = function() {};
15131513

1514-
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1515-
expect(createXhr).toBe(dummyCreateXhr);
1514+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1515+
expect(config.createXhr).toBe(dummyCreateXhr);
15161516
});
15171517

15181518
module(function($provide) {
@@ -1536,8 +1536,8 @@ describe('$http', function() {
15361536
var $httpBackend = jasmine.createSpy('$httpBackend');
15371537
var dummyCreateXhr = function() {};
15381538

1539-
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1540-
expect(createXhr).toBe(dummyCreateXhr);
1539+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1540+
expect(config.createXhr).toBe(dummyCreateXhr);
15411541
});
15421542

15431543
module(function($provide) {

test/ngMock/angular-mocksSpec.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,7 @@ describe('ngMock', function() {
13051305
canceler = fn;
13061306
});
13071307

1308-
hb('GET', '/url1', null, callback, null, {then: then});
1308+
hb('GET', '/url1', null, callback, null, {timeout: {then: then}});
13091309
expect(typeof canceler).toBe('function');
13101310

13111311
canceler(); // simulate promise resolution
@@ -1547,10 +1547,10 @@ describe('ngMockE2E', function() {
15471547
describe('passThrough()', function() {
15481548
it('should delegate requests to the real backend when passThrough is invoked', function() {
15491549
hb.when('GET', /\/passThrough\/.*/).passThrough();
1550-
hb('GET', '/passThrough/23', null, callback, {}, null, true);
1550+
hb('GET', '/passThrough/23', null, callback, {}, {withCredentials: true});
15511551

15521552
expect(realHttpBackend).toHaveBeenCalledOnceWith(
1553-
'GET', '/passThrough/23', null, callback, {}, null, true);
1553+
'GET', '/passThrough/23', null, callback, {}, {withCredentials: true});
15541554
});
15551555
});
15561556

0 commit comments

Comments
 (0)