Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ec5f67d

Browse files
committedJun 18, 2015
feat(http): add $xhrFactory service to enable creation of custom XMLHTTPRequest objects
Closes angular#2318
1 parent c611492 commit ec5f67d

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed
 

‎src/AngularPublic.js

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
$HttpParamSerializerProvider,
7373
$HttpParamSerializerJQLikeProvider,
7474
$HttpBackendProvider,
75+
$xhrFactoryProvider,
7576
$LocationProvider,
7677
$LogProvider,
7778
$ParseProvider,
@@ -233,6 +234,7 @@ function publishExternalAPI(angular) {
233234
$httpParamSerializer: $HttpParamSerializerProvider,
234235
$httpParamSerializerJQLike: $HttpParamSerializerJQLikeProvider,
235236
$httpBackend: $HttpBackendProvider,
237+
$xhrFactory: $xhrFactoryProvider,
236238
$location: $LocationProvider,
237239
$log: $LogProvider,
238240
$parse: $ParseProvider,

‎src/ng/httpBackend.js

+32-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
11
'use strict';
22

3-
function createXhr() {
4-
return new window.XMLHttpRequest();
3+
/**
4+
* @ngdoc service
5+
* @name $xhrFactory
6+
*
7+
* @description
8+
* Factory function used to create XMLHttpRequest objects.
9+
*
10+
* Decorate this service to create your own custom XMLHttpRequest objects.
11+
*
12+
* ```
13+
* .config(function($provide) {
14+
* $provide.decorator('$xhrFactory', function() {
15+
* return function createXhr(method, url) {
16+
* return new window.XMLHttpRequest({mozSystem: true});
17+
* };
18+
* });
19+
* });
20+
* ```
21+
*
22+
* @param {string} method HTTP method of the request (GET, POST, PUT, ..)
23+
* @param {string} url URL of the request.
24+
*/
25+
function $xhrFactoryProvider() {
26+
this.$get = function() {
27+
return function createXhr() {
28+
return new window.XMLHttpRequest();
29+
};
30+
};
531
}
632

733
/**
834
* @ngdoc service
935
* @name $httpBackend
1036
* @requires $window
1137
* @requires $document
38+
* @requires $xhrFactory
1239
*
1340
* @description
1441
* HTTP backend used by the {@link ng.$http service} that delegates to
@@ -21,8 +48,8 @@ function createXhr() {
2148
* $httpBackend} which can be trained with responses.
2249
*/
2350
function $HttpBackendProvider() {
24-
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
25-
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
51+
this.$get = ['$browser', '$window', '$document', '$xhrFactory', function($browser, $window, $document, $xhrFactory) {
52+
return createHttpBackend($browser, $xhrFactory, $browser.defer, $window.angular.callbacks, $document[0]);
2653
}];
2754
}
2855

@@ -46,7 +73,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
4673
});
4774
} else {
4875

49-
var xhr = createXhr();
76+
var xhr = createXhr(method, url);
5077

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

‎test/ng/httpBackendSpec.js

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

234+
it('should call $xhrFactory with method and url', function() {
235+
var mockXhrFactory = jasmine.createSpy('mockXhrFactory').andCallFake(createMockXhr);
236+
$backend = createHttpBackend($browser, mockXhrFactory, $browser.defer, callbacks, fakeDocument);
237+
$backend('GET', '/some-url', 'some-data', noop);
238+
expect(mockXhrFactory).toHaveBeenCalledWith('GET', '/some-url');
239+
});
240+
234241

235242
describe('responseType', function() {
236243

0 commit comments

Comments
 (0)