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

Commit a1f8682

Browse files
committed
Refactored to use the config object instead of using timeout, withCredentials, responseType, etc
1 parent 978e8b1 commit a1f8682

File tree

6 files changed

+45
-31
lines changed

6 files changed

+45
-31
lines changed

src/ng/http.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,7 @@ function $HttpProvider() {
993993
reqHeaders[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
994994
}
995995

996-
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
997-
config.withCredentials, config.responseType, config.createXhr);
996+
$httpBackend(config.method, url, reqData, done, reqHeaders, config);
998997
}
999998

1000999
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
@@ -1125,11 +1125,18 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11251125
}
11261126

11271127
// TODO(vojta): change params to: method, url, data, headers, callback
1128-
function $httpBackend(method, url, data, callback, headers, timeout, withCredentials) {
1128+
function $httpBackend(method, url, data, callback, headers, config) {
11291129
var xhr = new MockXhr(),
11301130
expectation = expectations[0],
11311131
wasExpected = false;
11321132

1133+
if (!config) {
1134+
config = {};
1135+
}
1136+
1137+
var timeout = config.timeout,
1138+
withCredentials = config.withCredentials;
1139+
11331140
function prettyPrint(data) {
11341141
return (angular.isString(data) || angular.isFunction(data) || data instanceof RegExp)
11351142
? data
@@ -1185,7 +1192,7 @@ function createHttpBackendMock($rootScope, $delegate, $browser) {
11851192
// if $browser specified, we do auto flush all requests
11861193
($browser ? $browser.defer : responsesPush)(wrapResponse(definition));
11871194
} else if (definition.passThrough) {
1188-
$delegate(method, url, data, callback, headers, timeout, withCredentials);
1195+
$delegate(method, url, data, callback, headers, config);
11891196
} else throw new Error('No response defined !');
11901197
return;
11911198
}

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
@@ -1474,10 +1474,10 @@ describe('$http', function() {
14741474
it('should pass timeout, withCredentials and responseType', function() {
14751475
var $httpBackend = jasmine.createSpy('$httpBackend');
14761476

1477-
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
1478-
expect(timeout).toBe(12345);
1479-
expect(withCredentials).toBe(true);
1480-
expect(responseType).toBe('json');
1477+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1478+
expect(config.timeout).toBe(12345);
1479+
expect(config.withCredentials).toBe(true);
1480+
expect(config.responseType).toBe('json');
14811481
});
14821482

14831483
module(function($provide) {
@@ -1502,8 +1502,8 @@ describe('$http', function() {
15021502
it('should use withCredentials from default', function() {
15031503
var $httpBackend = jasmine.createSpy('$httpBackend');
15041504

1505-
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
1506-
expect(withCredentials).toBe(true);
1505+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1506+
expect(config.withCredentials).toBe(true);
15071507
});
15081508

15091509
module(function($provide) {
@@ -1529,8 +1529,8 @@ describe('$http', function() {
15291529
var $httpBackend = jasmine.createSpy('$httpBackend');
15301530
var dummyCreateXhr = function() {};
15311531

1532-
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1533-
expect(createXhr).toBe(dummyCreateXhr);
1532+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1533+
expect(config.createXhr).toBe(dummyCreateXhr);
15341534
});
15351535

15361536
module(function($provide) {
@@ -1554,8 +1554,8 @@ describe('$http', function() {
15541554
var $httpBackend = jasmine.createSpy('$httpBackend');
15551555
var dummyCreateXhr = function() {};
15561556

1557-
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1558-
expect(createXhr).toBe(dummyCreateXhr);
1557+
$httpBackend.andCallFake(function(m, u, d, c, h, config) {
1558+
expect(config.createXhr).toBe(dummyCreateXhr);
15591559
});
15601560

15611561
module(function($provide) {

test/ngMock/angular-mocksSpec.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1282,7 +1282,7 @@ describe('ngMock', function() {
12821282
canceler = fn;
12831283
});
12841284

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

12881288
canceler(); // simulate promise resolution
@@ -1524,19 +1524,19 @@ describe('ngMockE2E', function() {
15241524
describe('passThrough()', function() {
15251525
it('should delegate requests to the real backend when passThrough is invoked', function() {
15261526
hb.when('GET', /\/passThrough\/.*/).passThrough();
1527-
hb('GET', '/passThrough/23', null, callback, {}, null, true);
1527+
hb('GET', '/passThrough/23', null, callback, {}, {withCredentials: true});
15281528

15291529
expect(realHttpBackend).toHaveBeenCalledOnceWith(
1530-
'GET', '/passThrough/23', null, callback, {}, null, true);
1530+
'GET', '/passThrough/23', null, callback, {}, {withCredentials: true});
15311531
});
15321532

15331533
it('should be able to override a respond definition with passThrough', function() {
15341534
var definition = hb.when('GET', /\/passThrough\/.*/).respond('override me');
15351535
definition.passThrough();
1536-
hb('GET', '/passThrough/23', null, callback, {}, null, true);
1536+
hb('GET', '/passThrough/23', null, callback, {}, {withCredentials: true});
15371537

15381538
expect(realHttpBackend).toHaveBeenCalledOnceWith(
1539-
'GET', '/passThrough/23', null, callback, {}, null, true);
1539+
'GET', '/passThrough/23', null, callback, {}, {withCredentials: true});
15401540
});
15411541

15421542
it('should be able to override a respond definition with passThrough', inject(function($browser) {

0 commit comments

Comments
 (0)