aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVojta Jina2012-08-04 12:11:00 -0700
committerIgor Minar2012-08-10 16:17:59 -0700
commite0a54f6b206dc2b6595f2bc3a17c5932e7477545 (patch)
tree4db991fbffe6258f52e59a47039f0aea8fab3341
parent9767f7bdd3e1ce6f65bdea992d67369ead13d813 (diff)
downloadangular.js-e0a54f6b206dc2b6595f2bc3a17c5932e7477545.tar.bz2
feat($http): support reponseType
Closes #1013
-rw-r--r--src/ng/http.js4
-rw-r--r--src/ng/httpBackend.js10
-rw-r--r--test/ng/httpBackendSpec.js18
-rw-r--r--test/ng/httpSpec.js9
4 files changed, 34 insertions, 7 deletions
diff --git a/src/ng/http.js b/src/ng/http.js
index deeb6cbb..ef55f878 100644
--- a/src/ng/http.js
+++ b/src/ng/http.js
@@ -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`
@@ -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;
diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js
index 0a12aa23..bca46ee1 100644
--- a/src/ng/httpBackend.js
+++ b/src/ng/httpBackend.js
@@ -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();
@@ -65,8 +65,8 @@ 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());
}
};
@@ -74,6 +74,10 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
xhr.withCredentials = true;
}
+ if (responseType) {
+ xhr.responseType = responseType;
+ }
+
xhr.send(post || '');
if (timeout > 0) {
diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js
index 06b63c3c..a491ae26 100644
--- a/test/ng/httpBackendSpec.js
+++ b/test/ng/httpBackendSpec.js
@@ -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\.(.*)/;
diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js
index bb4de3c1..f7df3d4b 100644
--- a/test/ng/httpSpec.js
+++ b/test/ng/httpSpec.js
@@ -959,12 +959,13 @@ 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) {
@@ -972,7 +973,9 @@ describe('$http', function() {
});
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();
});