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

Commit 106f90a

Browse files
sjurbapetebacondarwin
authored andcommitted
feat($http): add $xhrFactory service to enable creation of custom xhr objects
Closes #2318 Closes #9319 Closes #12159
1 parent 86e8088 commit 106f90a

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-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,
@@ -230,6 +231,7 @@ function publishExternalAPI(angular) {
230231
$httpParamSerializer: $HttpParamSerializerProvider,
231232
$httpParamSerializerJQLike: $HttpParamSerializerJQLikeProvider,
232233
$httpBackend: $HttpBackendProvider,
234+
$xhrFactory: $xhrFactoryProvider,
233235
$location: $LocationProvider,
234236
$log: $LogProvider,
235237
$parse: $ParseProvider,

src/ng/httpBackend.js

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,40 @@
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+
* Replace or decorate this service to create your own custom XMLHttpRequest objects.
11+
*
12+
* ```
13+
* angular.module('myApp', [])
14+
* .factory('$xhrFactory', function() {
15+
* return function createXhr(method, url) {
16+
* return new window.XMLHttpRequest({mozSystem: true});
17+
* };
18+
* });
19+
* ```
20+
*
21+
* @param {string} method HTTP method of the request (GET, POST, PUT, ..)
22+
* @param {string} url URL of the request.
23+
*/
24+
function $xhrFactoryProvider() {
25+
this.$get = function() {
26+
return function createXhr() {
27+
return new window.XMLHttpRequest();
28+
};
29+
};
530
}
631

732
/**
833
* @ngdoc service
934
* @name $httpBackend
1035
* @requires $window
1136
* @requires $document
37+
* @requires $xhrFactory
1238
*
1339
* @description
1440
* HTTP backend used by the {@link ng.$http service} that delegates to
@@ -21,8 +47,8 @@ function createXhr() {
2147
* $httpBackend} which can be trained with responses.
2248
*/
2349
function $HttpBackendProvider() {
24-
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
25-
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
50+
this.$get = ['$browser', '$window', '$document', '$xhrFactory', function($browser, $window, $document, $xhrFactory) {
51+
return createHttpBackend($browser, $xhrFactory, $browser.defer, $window.angular.callbacks, $document[0]);
2652
}];
2753
}
2854

@@ -46,7 +72,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
4672
});
4773
} else {
4874

49-
var xhr = createXhr();
75+
var xhr = createXhr(method, url);
5076

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

test/ng/httpBackendSpec.js

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

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

237244
describe('responseType', function() {
238245

0 commit comments

Comments
 (0)