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

Commit 29046d2

Browse files
committed
Naive approach, no refactoring.
1 parent 02c0ed2 commit 29046d2

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
@@ -512,6 +512,8 @@ function $HttpProvider() {
512512
* for more information.
513513
* - **responseType** - `{string}` - see
514514
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
515+
* - **createXhr** - `{function(method)}` - a function that constructs the XHR object. Use this
516+
* to create customized or non-standard XHR objects.
515517
*
516518
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
517519
* standard `then` method and two http specific methods: `success` and `error`. The `then`
@@ -623,7 +625,8 @@ function $HttpProvider() {
623625
var config = {
624626
method: 'get',
625627
transformRequest: defaults.transformRequest,
626-
transformResponse: defaults.transformResponse
628+
transformResponse: defaults.transformResponse,
629+
createXhr: defaults.createXhr
627630
};
628631
var headers = mergeHeaders(requestConfig);
629632

@@ -929,7 +932,7 @@ function $HttpProvider() {
929932
}
930933

931934
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
932-
config.withCredentials, config.responseType);
935+
config.withCredentials, config.responseType, config.createXhr);
933936
}
934937

935938
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
@@ -1481,7 +1481,6 @@ describe('$http', function() {
14811481
$httpBackend.verifyNoOutstandingExpectation = noop;
14821482
});
14831483

1484-
14851484
it('should use withCredentials from default', function() {
14861485
var $httpBackend = jasmine.createSpy('$httpBackend');
14871486

@@ -1507,4 +1506,55 @@ describe('$http', function() {
15071506

15081507
$httpBackend.verifyNoOutstandingExpectation = noop;
15091508
});
1509+
1510+
it('should pass createXhr', function() {
1511+
var $httpBackend = jasmine.createSpy('$httpBackend');
1512+
var dummyCreateXhr = function() {};
1513+
1514+
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1515+
expect(createXhr).toBe(dummyCreateXhr);
1516+
});
1517+
1518+
module(function($provide) {
1519+
$provide.value('$httpBackend', $httpBackend);
1520+
});
1521+
1522+
inject(function($http, $rootScope) {
1523+
$http({
1524+
method: 'GET',
1525+
url: 'some.html',
1526+
createXhr: dummyCreateXhr
1527+
});
1528+
$rootScope.$digest();
1529+
expect($httpBackend).toHaveBeenCalledOnce();
1530+
});
1531+
1532+
$httpBackend.verifyNoOutstandingExpectation = noop;
1533+
});
1534+
1535+
it('should use createXhr from default', function() {
1536+
var $httpBackend = jasmine.createSpy('$httpBackend');
1537+
var dummyCreateXhr = function() {};
1538+
1539+
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1540+
expect(createXhr).toBe(dummyCreateXhr);
1541+
});
1542+
1543+
module(function($provide) {
1544+
$provide.value('$httpBackend', $httpBackend);
1545+
});
1546+
1547+
inject(function($http, $rootScope) {
1548+
$http.defaults.createXhr = dummyCreateXhr;
1549+
$http({
1550+
method: 'GET',
1551+
url: 'some.html'
1552+
});
1553+
$rootScope.$digest();
1554+
expect($httpBackend).toHaveBeenCalledOnce();
1555+
});
1556+
1557+
$httpBackend.verifyNoOutstandingExpectation = noop;
1558+
});
1559+
15101560
});

0 commit comments

Comments
 (0)