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

feat($http): add overrideMimeType passthrough #7689

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 9 additions & 2 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,10 @@ function $HttpProvider() {
* XHR object. See [requests with credentials]https://developer.mozilla.org/en/http_access_control#section_5
* for more information.
* - **responseType** - `{string}` - see
* [requestType](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType).
* [requestType](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#responseType).
* - **overrideMimeType** - `{string}` - force the response to be treated as the given MIME
* type by calling [overrideMimeType()](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest#overrideMimeType%28%29)
* on the XHR object.
*
* @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 @@ -627,6 +630,10 @@ function $HttpProvider() {
config.withCredentials = defaults.withCredentials;
}

if (isUndefined(config.overrideMimeType) && !isUndefined(defaults.overrideMimeType)) {
config.overrideMimeType = defaults.overrideMimeType;
}

// send request
return sendReq(config, reqData, headers).then(transformResponse, transformResponse);
};
Expand Down Expand Up @@ -888,7 +895,7 @@ function $HttpProvider() {
// if we won't have the response in cache, send the request to the backend
if (isUndefined(cachedResp)) {
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials, config.responseType);
config.withCredentials, config.responseType, config.overrideMimeType);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not big on adding a new parameter to $httpBackend, but I guess there's no other way

Copy link
Author

Choose a reason for hiding this comment

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

It seems odd to me that $httpBackend doesn't take a config alike object, but it doesn't (for whatever reason).

}

return promise;
Expand Down
6 changes: 5 additions & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
var ABORTED = -1;

// 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, overrideMimeType) {
var status;
$browser.$$incOutstandingRequestCount();
url = url || $browser.url();
Expand Down Expand Up @@ -120,6 +120,10 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc
}
}

if (overrideMimeType) {
xhr.overrideMimeType(overrideMimeType);
}

xhr.send(post || null);
}

Expand Down
4 changes: 4 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,10 @@ function MockXhr() {
};

this.abort = angular.noop;

this.overrideMimeType = function(type) {
this.$$overrideMimeType = type;
};
}


Expand Down
6 changes: 6 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,12 @@ describe('$httpBackend', function() {
});


it('should call overrideMimeType', function() {
$backend('GET', '/some.url', null, callback, {}, null, null, null, 'text/plain;charset=x-user-defined');
expect(MockXhr.$$lastInstance.$$overrideMimeType).toBe('text/plain;charset=x-user-defined');
});


describe('responseType', function() {

it('should set responseType and return xhr.response', function() {
Expand Down
14 changes: 9 additions & 5 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1383,13 +1383,14 @@ describe('$http', function() {
});


it('should pass timeout, withCredentials and responseType', function() {
it('should pass timeout, withCredentials, responseType, and overrideMimeType', function() {
var $httpBackend = jasmine.createSpy('$httpBackend');

$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType, overrideMimeType) {
expect(timeout).toBe(12345);
expect(withCredentials).toBe(true);
expect(responseType).toBe('json');
expect(overrideMimeType).toBe('text/plain;charset=x-user-defined');
});

module(function($provide) {
Expand All @@ -1402,7 +1403,8 @@ describe('$http', function() {
url: 'some.html',
timeout: 12345,
withCredentials: true,
responseType: 'json'
responseType: 'json',
overrideMimeType: 'text/plain;charset=x-user-defined',
});
$rootScope.$digest();
expect($httpBackend).toHaveBeenCalledOnce();
Expand All @@ -1412,11 +1414,12 @@ describe('$http', function() {
});


it('should use withCredentials from default', function() {
it('should use withCredentials and overrideMimeType from default', function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we should be testing overrideMimeType in this spec, it should get its own spec, so that we can keep the scope of each spec very narrow

Copy link
Author

Choose a reason for hiding this comment

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

Why make this different than the immediately preceding spec for 'it should pass timeout, withCredentials, responseType, and overrideMimeType'?

var $httpBackend = jasmine.createSpy('$httpBackend');

$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType, overrideMimeType) {
expect(withCredentials).toBe(true);
expect(overrideMimeType).toBe('text/plain;charset=x-user-defined');
});

module(function($provide) {
Expand All @@ -1425,6 +1428,7 @@ describe('$http', function() {

inject(function($http, $rootScope) {
$http.defaults.withCredentials = true;
$http.defaults.overrideMimeType = 'text/plain;charset=x-user-defined';
$http({
method: 'GET',
url: 'some.html',
Expand Down