Skip to content

Commit 8d4e2e5

Browse files
committed
feat($http): support reponseType
Closes angular#1013
1 parent 77e6d83 commit 8d4e2e5

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

src/ng/http.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,8 @@ function $HttpProvider() {
381381
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
382382
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
383383
* requests with credentials} for more information.
384+
* - **responseType** - `{string}` - see {@link
385+
* https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType requestType}.
384386
*
385387
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
386388
* standard `then` method and two http specific methods: `success` and `error`. The `then`
@@ -697,7 +699,7 @@ function $HttpProvider() {
697699
// if we won't have the response in cache, send the request to the backend
698700
if (!cachedResp) {
699701
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
700-
config.withCredentials);
702+
config.withCredentials, config.responseType);
701703
}
702704

703705
return promise;

src/ng/httpBackend.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ function $HttpBackendProvider() {
3232

3333
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
3434
// TODO(vojta): fix the signature
35-
return function(method, url, post, callback, headers, timeout, withCredentials) {
35+
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
3636
$browser.$$incOutstandingRequestCount();
3737
url = url || $browser.url();
3838

@@ -65,15 +65,19 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
6565
// always async
6666
xhr.onreadystatechange = function() {
6767
if (xhr.readyState == 4) {
68-
completeRequest(
69-
callback, status || xhr.status, xhr.responseText, xhr.getAllResponseHeaders());
68+
completeRequest(callback, status || xhr.status, xhr.response || xhr.responseText,
69+
xhr.getAllResponseHeaders());
7070
}
7171
};
7272

7373
if (withCredentials) {
7474
xhr.withCredentials = true;
7575
}
7676

77+
if (responseType) {
78+
xhr.responseType = responseType;
79+
}
80+
7781
xhr.send(post || '');
7882

7983
if (timeout > 0) {

test/ng/httpBackendSpec.js

+18
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ describe('$httpBackend', function() {
135135
});
136136

137137

138+
it('should set responseType and return xhr.response', function() {
139+
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');
140+
141+
var xhrInstance = MockXhr.$$lastInstance;
142+
expect(xhrInstance.responseType).toBe('blob');
143+
144+
callback.andCallFake(function(status, response) {
145+
expect(response).toBe(xhrInstance.response);
146+
});
147+
148+
xhrInstance.response = {some: 'object'};
149+
xhrInstance.readyState = 4;
150+
xhrInstance.onreadystatechange();
151+
152+
expect(callback).toHaveBeenCalledOnce();
153+
});
154+
155+
138156
describe('JSONP', function() {
139157

140158
var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;

test/ng/httpSpec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -959,20 +959,23 @@ describe('$http', function() {
959959
});
960960

961961

962-
it('should pass timeout and withCredentials', function() {
962+
it('should pass timeout, withCredentials and responseType', function() {
963963
var $httpBackend = jasmine.createSpy('$httpBackend');
964964

965-
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials) {
965+
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
966966
expect(timeout).toBe(12345);
967967
expect(withCredentials).toBe(true);
968+
expect(responseType).toBe('json');
968969
});
969970

970971
module(function($provide) {
971972
$provide.value('$httpBackend', $httpBackend);
972973
});
973974

974975
inject(function($http) {
975-
$http({method: 'GET', url: 'some.html', timeout: 12345, withCredentials: true});
976+
$http({
977+
method: 'GET', url: 'some.html', timeout: 12345, withCredentials: true, responseType: 'json'
978+
});
976979
expect($httpBackend).toHaveBeenCalledOnce();
977980
});
978981

0 commit comments

Comments
 (0)