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

Commit

Permalink
fix($http): return responseText on IE8 for requests with responseType…
Browse files Browse the repository at this point in the history
… set

Closes #4464
Closes #4738
Closes #5636
  • Loading branch information
IgorMinar committed Jan 10, 2014
1 parent 36c9e42 commit a9cccbe
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
7 changes: 4 additions & 3 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,12 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc

if(status !== ABORTED) {
responseHeaders = xhr.getAllResponseHeaders();
response = xhr.responseType ? xhr.response : xhr.responseText;

// responseText is the old-school way of retrieving response (supported by IE8 & 9)
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
response = ('response' in xhr) ? xhr.response : xhr.responseText;
}

// responseText is the old-school way of retrieving response (supported by IE8 & 9)
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
completeRequest(callback,
status || xhr.status,
response,
Expand Down
44 changes: 34 additions & 10 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,45 @@ describe('$httpBackend', function() {
});


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

var xhrInstance = MockXhr.$$lastInstance;
expect(xhrInstance.responseType).toBe('blob');
it('should set responseType and return xhr.response', function() {
$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');

callback.andCallFake(function(status, response) {
expect(response).toBe(xhrInstance.response);
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();
});

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

expect(callback).toHaveBeenCalledOnce();
it('should read responseText if response was not defined', function() {
// old browsers like IE8, don't support responseType, so they always respond with responseText

$backend('GET', '/whatever', null, callback, {}, null, null, 'blob');

var xhrInstance = MockXhr.$$lastInstance;
var responseText = '{"some": "object"}';
expect(xhrInstance.responseType).toBe('blob');

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

xhrInstance.responseText = responseText;
xhrInstance.readyState = 4;
xhrInstance.onreadystatechange();

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


Expand Down

0 comments on commit a9cccbe

Please sign in to comment.