mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-20 12:21:52 +00:00
parent
9767f7bdd3
commit
e0a54f6b20
4 changed files with 34 additions and 7 deletions
|
|
@ -381,6 +381,8 @@ function $HttpProvider() {
|
||||||
* - **withCredentials** - `{boolean}` - whether to to set the `withCredentials` flag on the
|
* - **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
|
* XHR object. See {@link https://developer.mozilla.org/en/http_access_control#section_5
|
||||||
* requests with credentials} for more information.
|
* 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
|
* @returns {HttpPromise} Returns a {@link ng.$q promise} object with the
|
||||||
* standard `then` method and two http specific methods: `success` and `error`. The `then`
|
* 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 we won't have the response in cache, send the request to the backend
|
||||||
if (!cachedResp) {
|
if (!cachedResp) {
|
||||||
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
|
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
|
||||||
config.withCredentials);
|
config.withCredentials, config.responseType);
|
||||||
}
|
}
|
||||||
|
|
||||||
return promise;
|
return promise;
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ function $HttpBackendProvider() {
|
||||||
|
|
||||||
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
|
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
|
||||||
// TODO(vojta): fix the signature
|
// 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();
|
$browser.$$incOutstandingRequestCount();
|
||||||
url = url || $browser.url();
|
url = url || $browser.url();
|
||||||
|
|
||||||
|
|
@ -65,8 +65,8 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
|
||||||
// always async
|
// always async
|
||||||
xhr.onreadystatechange = function() {
|
xhr.onreadystatechange = function() {
|
||||||
if (xhr.readyState == 4) {
|
if (xhr.readyState == 4) {
|
||||||
completeRequest(
|
completeRequest(callback, status || xhr.status, xhr.response || xhr.responseText,
|
||||||
callback, status || xhr.status, xhr.responseText, xhr.getAllResponseHeaders());
|
xhr.getAllResponseHeaders());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -74,6 +74,10 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
|
||||||
xhr.withCredentials = true;
|
xhr.withCredentials = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (responseType) {
|
||||||
|
xhr.responseType = responseType;
|
||||||
|
}
|
||||||
|
|
||||||
xhr.send(post || '');
|
xhr.send(post || '');
|
||||||
|
|
||||||
if (timeout > 0) {
|
if (timeout > 0) {
|
||||||
|
|
|
||||||
|
|
@ -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() {
|
describe('JSONP', function() {
|
||||||
|
|
||||||
var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;
|
var SCRIPT_URL = /([^\?]*)\?cb=angular\.callbacks\.(.*)/;
|
||||||
|
|
|
||||||
|
|
@ -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');
|
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(timeout).toBe(12345);
|
||||||
expect(withCredentials).toBe(true);
|
expect(withCredentials).toBe(true);
|
||||||
|
expect(responseType).toBe('json');
|
||||||
});
|
});
|
||||||
|
|
||||||
module(function($provide) {
|
module(function($provide) {
|
||||||
|
|
@ -972,7 +973,9 @@ describe('$http', function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
inject(function($http) {
|
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();
|
expect($httpBackend).toHaveBeenCalledOnce();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue