Skip to content

Commit e0d0292

Browse files
committed
feat ($http): Add custom createXhr factory to config object
Closes angular#2318
1 parent 8caf180 commit e0d0292

File tree

4 files changed

+68
-4
lines changed

4 files changed

+68
-4
lines changed

src/ng/http.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,8 @@ function $HttpProvider() {
743743
* for more information.
744744
* - **responseType** - `{string}` - see
745745
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
746+
* - **createXhr** - `{function(method)}` - a function that constructs the XHR object. Use this
747+
* to create customized or non-standard XHR objects.
746748
*
747749
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
748750
* standard `then` method and two http specific methods: `success` and `error`. The `then`
@@ -861,7 +863,8 @@ function $HttpProvider() {
861863
method: 'get',
862864
transformRequest: defaults.transformRequest,
863865
transformResponse: defaults.transformResponse,
864-
paramSerializer: defaults.paramSerializer
866+
paramSerializer: defaults.paramSerializer,
867+
createXhr: defaults.createXhr
865868
}, requestConfig);
866869

867870
config.headers = mergeHeaders(requestConfig);
@@ -1176,7 +1179,7 @@ function $HttpProvider() {
11761179
}
11771180

11781181
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
1179-
config.withCredentials, config.responseType);
1182+
config.withCredentials, config.responseType, config.createXhr);
11801183
}
11811184

11821185
return promise;

src/ng/httpBackend.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function $HttpBackendProvider() {
2828

2929
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
3030
// TODO(vojta): fix the signature
31-
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
31+
return function(method, url, post, callback, headers, timeout, withCredentials, responseType, customCreateXhr) {
3232
$browser.$$incOutstandingRequestCount();
3333
url = url || $browser.url();
3434

@@ -46,7 +46,12 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
4646
});
4747
} else {
4848

49-
var xhr = createXhr();
49+
var xhr;
50+
if (customCreateXhr && typeof customCreateXhr === 'function') {
51+
xhr = customCreateXhr(method);
52+
} else {
53+
xhr = createXhr();
54+
}
5055

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

test/ng/httpBackendSpec.js

+5
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,11 @@ describe('$httpBackend', function() {
231231
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
232232
});
233233

234+
it('should use custom createXhr', function() {
235+
var createXhr = jasmine.createSpy('createXhr').andReturn(new MockXhr());
236+
$backend('GET', '/whatever', null, callback, {}, null, null, null, createXhr);
237+
expect(createXhr).toHaveBeenCalledOnceWith('GET');
238+
});
234239

235240
describe('responseType', function() {
236241

test/ng/httpSpec.js

+51
Original file line numberDiff line numberDiff line change
@@ -1896,6 +1896,57 @@ describe('$http', function() {
18961896

18971897
$httpBackend.verifyNoOutstandingExpectation = noop;
18981898
});
1899+
1900+
it('should pass createXhr', function() {
1901+
var $httpBackend = jasmine.createSpy('$httpBackend');
1902+
var dummyCreateXhr = function() {};
1903+
1904+
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1905+
expect(createXhr).toBe(dummyCreateXhr);
1906+
});
1907+
1908+
module(function($provide) {
1909+
$provide.value('$httpBackend', $httpBackend);
1910+
});
1911+
1912+
inject(function($http, $rootScope) {
1913+
$http({
1914+
method: 'GET',
1915+
url: 'some.html',
1916+
createXhr: dummyCreateXhr
1917+
});
1918+
$rootScope.$digest();
1919+
expect($httpBackend).toHaveBeenCalledOnce();
1920+
});
1921+
1922+
$httpBackend.verifyNoOutstandingExpectation = noop;
1923+
});
1924+
1925+
it('should use createXhr from default', function() {
1926+
var $httpBackend = jasmine.createSpy('$httpBackend');
1927+
var dummyCreateXhr = function() {};
1928+
1929+
$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
1930+
expect(createXhr).toBe(dummyCreateXhr);
1931+
});
1932+
1933+
module(function($provide) {
1934+
$provide.value('$httpBackend', $httpBackend);
1935+
});
1936+
1937+
inject(function($http, $rootScope) {
1938+
$http.defaults.createXhr = dummyCreateXhr;
1939+
$http({
1940+
method: 'GET',
1941+
url: 'some.html'
1942+
});
1943+
$rootScope.$digest();
1944+
expect($httpBackend).toHaveBeenCalledOnce();
1945+
});
1946+
1947+
$httpBackend.verifyNoOutstandingExpectation = noop;
1948+
});
1949+
18991950
});
19001951

19011952

0 commit comments

Comments
 (0)