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

Commit

Permalink
feat($http): support reponseType
Browse files Browse the repository at this point in the history
Closes #1013
  • Loading branch information
vojtajina authored and IgorMinar committed Aug 10, 2012
1 parent 9767f7b commit e0a54f6
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,8 @@ function $HttpProvider() {
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
* requests with credentials} for more information.
* - **responseType** - `{string}` - see {@link
* https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#responseType requestType}.
*
* @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 @@ -697,7 +699,7 @@ function $HttpProvider() {
// if we won't have the response in cache, send the request to the backend
if (!cachedResp) {
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials);
config.withCredentials, config.responseType);
}

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

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

Expand Down Expand Up @@ -65,15 +65,19 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
// always async
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
completeRequest(
callback, status || xhr.status, xhr.responseText, xhr.getAllResponseHeaders());
completeRequest(callback, status || xhr.status, xhr.response || xhr.responseText,
xhr.getAllResponseHeaders());
}
};

if (withCredentials) {
xhr.withCredentials = true;
}

if (responseType) {
xhr.responseType = responseType;
}

xhr.send(post || '');

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


it('should set responseType and return xhr.response', function() {
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');

var xhrInstance = MockXhr.$$lastInstance;
expect(xhrInstance.responseType).toBe('blob');

callback.andCallFake(function(status, response) {
expect(response).toBe(xhrInstance.response);
});

xhrInstance.response = {some: 'object'};
xhrInstance.readyState = 4;
xhrInstance.onreadystatechange();

expect(callback).toHaveBeenCalledOnce();
});


describe('JSONP', function() {

var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;
Expand Down
9 changes: 6 additions & 3 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -959,20 +959,23 @@ describe('$http', function() {
});


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

$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials) {
$httpBackend.andCallFake(function(m, u, d, c, h, timeout, withCredentials, responseType) {
expect(timeout).toBe(12345);
expect(withCredentials).toBe(true);
expect(responseType).toBe('json');
});

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

inject(function($http) {
$http({method: 'GET', url: 'some.html', timeout: 12345, withCredentials: true});
$http({
method: 'GET', url: 'some.html', timeout: 12345, withCredentials: true, responseType: 'json'
});
expect($httpBackend).toHaveBeenCalledOnce();
});

Expand Down

0 comments on commit e0a54f6

Please sign in to comment.