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

feat ($http): Add custom createXhr factory to config object #9319

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -743,6 +743,8 @@ function $HttpProvider() {
* for more information.
* - **responseType** - `{string}` - see
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
* - **createXhr** - `{function(method)}` - a function that constructs the XHR object. Use this
* to create customized or non-standard XHR objects.
*
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
* standard `then` method and two http specific methods: `success` and `error`. The `then`
Expand Down Expand Up @@ -861,7 +863,8 @@ function $HttpProvider() {
method: 'get',
transformRequest: defaults.transformRequest,
transformResponse: defaults.transformResponse,
paramSerializer: defaults.paramSerializer
paramSerializer: defaults.paramSerializer,
createXhr: defaults.createXhr
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can set it on the globals object as well to configure it globally. I think that's what jquery does as well..

xhr (default: ActiveXObject when available (IE), the XMLHttpRequest otherwise)
Type: Function()
Callback for creating the XMLHttpRequest object. 
Defaults to the ActiveXObject when available (IE), the XMLHttpRequest otherwise. 
Override to provide your own implementation for XMLHttpRequest or enhancements to the factory.

And,

settings
Type: PlainObject
A set of key/value pairs that configure the Ajax request. All settings are optional. 
A default can be set for any option with $.ajaxSetup(). 
See jQuery.ajax( settings ) below for a complete list of all settings.

}, requestConfig);

config.headers = mergeHeaders(requestConfig);
Expand Down Expand Up @@ -1176,7 +1179,7 @@ function $HttpProvider() {
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even though this is smaller and saner, i'm not sure if we can actually make this change. @jeffbcross wdyt?


$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials, config.responseType);
config.withCredentials, config.responseType, config.createXhr);
}

return promise;
Expand Down
9 changes: 7 additions & 2 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function $HttpBackendProvider() {

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

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

var xhr = createXhr();
var xhr;
if (customCreateXhr && typeof customCreateXhr === 'function') {
xhr = customCreateXhr(method);
} else {
xhr = createXhr();
}

xhr.open(method, url, true);
forEach(headers, function(value, key) {
Expand Down
5 changes: 5 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ describe('$httpBackend', function() {
expect(MockXhr.$$lastInstance.withCredentials).toBe(true);
});

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

describe('responseType', function() {

Expand Down
51 changes: 51 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1896,6 +1896,57 @@ describe('$http', function() {

$httpBackend.verifyNoOutstandingExpectation = noop;
});

it('should pass createXhr', function() {
var $httpBackend = jasmine.createSpy('$httpBackend');
var dummyCreateXhr = function() {};

$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
expect(createXhr).toBe(dummyCreateXhr);
});

module(function($provide) {
$provide.value('$httpBackend', $httpBackend);
});

inject(function($http, $rootScope) {
$http({
method: 'GET',
url: 'some.html',
createXhr: dummyCreateXhr
});
$rootScope.$digest();
expect($httpBackend).toHaveBeenCalledOnce();
});

$httpBackend.verifyNoOutstandingExpectation = noop;
});

it('should use createXhr from default', function() {
var $httpBackend = jasmine.createSpy('$httpBackend');
var dummyCreateXhr = function() {};

$httpBackend.andCallFake(function(m, u, d, c, h, t, wc, rt, createXhr) {
expect(createXhr).toBe(dummyCreateXhr);
});

module(function($provide) {
$provide.value('$httpBackend', $httpBackend);
});

inject(function($http, $rootScope) {
$http.defaults.createXhr = dummyCreateXhr;
$http({
method: 'GET',
url: 'some.html'
});
$rootScope.$digest();
expect($httpBackend).toHaveBeenCalledOnce();
});

$httpBackend.verifyNoOutstandingExpectation = noop;
});

});


Expand Down