Skip to content

Commit 978e8b1

Browse files
committed
Naive approach, no refactoring.
1 parent b119251 commit 978e8b1

File tree

4 files changed

+68
-6
lines changed

4 files changed

+68
-6
lines changed

src/ng/http.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,8 @@ function $HttpProvider() {
572572
* for more information.
573573
* - **responseType** - `{string}` - see
574574
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
575+
* - **createXhr** - `{function(method)}` - a function that constructs the XHR object. Use this
576+
* to create customized or non-standard XHR objects.
575577
*
576578
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
577579
* standard `then` method and two http specific methods: `success` and `error`. The `then`
@@ -684,7 +686,8 @@ function $HttpProvider() {
684686
var config = {
685687
method: 'get',
686688
transformRequest: defaults.transformRequest,
687-
transformResponse: defaults.transformResponse
689+
transformResponse: defaults.transformResponse,
690+
createXhr: defaults.createXhr
688691
};
689692
var headers = mergeHeaders(requestConfig);
690693

@@ -991,7 +994,7 @@ function $HttpProvider() {
991994
}
992995

993996
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
994-
config.withCredentials, config.responseType);
997+
config.withCredentials, config.responseType, config.createXhr);
995998
}
996999

9971000
return promise;

src/ng/httpBackend.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ 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) {
43+
return function(method, url, post, callback, headers, timeout, withCredentials, responseType, customCreateXhr) {
4444
var status;
4545
$browser.$$incOutstandingRequestCount();
4646
url = url || $browser.url();
@@ -59,7 +59,12 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
5959
});
6060
} else {
6161

62-
var xhr = createXhr(method);
62+
var xhr;
63+
if (customCreateXhr && typeof customCreateXhr ==='function') {
64+
xhr = customCreateXhr(method);
65+
} else {
66+
xhr = createXhr(method);
67+
}
6368

6469
xhr.open(method, url, true);
6570
forEach(headers, function(value, key) {

test/ng/httpBackendSpec.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ describe('$httpBackend', function() {
312312
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
313313
});
314314

315+
it('should use custom createXhr', function(){
316+
var createXhr = jasmine.createSpy('createXhr').andReturn(new MockXhr());
317+
$backend('GET', '/whatever', null, callback, {}, {createXhr: createXhr});
318+
expect(createXhr).toHaveBeenCalledOnceWith('GET');
319+
});
315320

316321
describe('responseType', function() {
317322

@@ -541,4 +546,3 @@ describe('$httpBackend', function() {
541546
});
542547
});
543548
});
544-

test/ng/httpSpec.js

+51-1
Original file line numberDiff line numberDiff line change
@@ -1499,7 +1499,6 @@ describe('$http', function() {
14991499
$httpBackend.verifyNoOutstandingExpectation = noop;
15001500
});
15011501

1502-
15031502
it('should use withCredentials from default', function() {
15041503
var $httpBackend = jasmine.createSpy('$httpBackend');
15051504

@@ -1525,6 +1524,57 @@ describe('$http', function() {
15251524

15261525
$httpBackend.verifyNoOutstandingExpectation = noop;
15271526
});
1527+
1528+
it('should pass createXhr', function() {
1529+
var $httpBackend = jasmine.createSpy('$httpBackend');
1530+
var dummyCreateXhr = function() {};
1531+
1532+
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1533+
expect(createXhr).toBe(dummyCreateXhr);
1534+
});
1535+
1536+
module(function($provide) {
1537+
$provide.value('$httpBackend', $httpBackend);
1538+
});
1539+
1540+
inject(function($http, $rootScope) {
1541+
$http({
1542+
method: 'GET',
1543+
url: 'some.html',
1544+
createXhr: dummyCreateXhr
1545+
});
1546+
$rootScope.$digest();
1547+
expect($httpBackend).toHaveBeenCalledOnce();
1548+
});
1549+
1550+
$httpBackend.verifyNoOutstandingExpectation = noop;
1551+
});
1552+
1553+
it('should use createXhr from default', function() {
1554+
var $httpBackend = jasmine.createSpy('$httpBackend');
1555+
var dummyCreateXhr = function() {};
1556+
1557+
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1558+
expect(createXhr).toBe(dummyCreateXhr);
1559+
});
1560+
1561+
module(function($provide) {
1562+
$provide.value('$httpBackend', $httpBackend);
1563+
});
1564+
1565+
inject(function($http, $rootScope) {
1566+
$http.defaults.createXhr = dummyCreateXhr;
1567+
$http({
1568+
method: 'GET',
1569+
url: 'some.html'
1570+
});
1571+
$rootScope.$digest();
1572+
expect($httpBackend).toHaveBeenCalledOnce();
1573+
});
1574+
1575+
$httpBackend.verifyNoOutstandingExpectation = noop;
1576+
});
1577+
15281578
});
15291579

15301580

0 commit comments

Comments
 (0)