Skip to content

Commit

Permalink
fix($httpBackend): use ActiveX XHR when making PATCH requests on IE8
Browse files Browse the repository at this point in the history
IE8's native XHR doesn't support PATCH requests, but the ActiveX one does.

Closes angular#2518
Closes angular#5043

Fixed createXhr function to throw minErr when an exception occurs
  • Loading branch information
IgorMinar authored and wesleycho committed Dec 31, 2013
1 parent d158dd1 commit 0549e04
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
5 changes: 4 additions & 1 deletion src/.jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@
"DIRTY_CLASS": false,

/* ng/directive/form.js */
"nullFormCtrl": false
"nullFormCtrl": false,

/* ng/httpBackend.js */
"ActiveXObject": false

}
}
22 changes: 12 additions & 10 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
'use strict';

var XHR = window.XMLHttpRequest || function() {
/* global ActiveXObject */
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) {}
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e2) {}
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) {}
throw minErr('$httpBackend')('noxhr', "This browser does not support XMLHttpRequest.");
};
function createXhr(method) {
// IE8 doesn't support PATCH method, but the ActiveX object does
try { return (msie <= 8 && lowercase(method) === 'patch')
? new ActiveXObject('Microsoft.XMLHTTP')
: new window.XMLHttpRequest();
} catch (e) { throw minErr('$httpBackend')('noxhr', "This browser does not support XMLHttpRequest."); }
}


/**
Expand All @@ -28,11 +28,11 @@ var XHR = window.XMLHttpRequest || function() {
*/
function $HttpBackendProvider() {
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks, $document[0]);
return createHttpBackend($browser, createXhr, $browser.defer, $window.angular.callbacks, $document[0]);
}];
}

function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument) {
function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDocument) {
var ABORTED = -1;

// TODO(vojta): fix the signature
Expand All @@ -57,7 +57,9 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument)
delete callbacks[callbackId];
});
} else {
var xhr = new XHR();

var xhr = createXhr(method);

xhr.open(method, url, true);
forEach(headers, function(value, key) {
if (isDefined(value)) {
Expand Down
4 changes: 4 additions & 0 deletions src/ngMock/angular-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -1572,6 +1572,10 @@ function MockHttpExpectation(method, url, data, headers) {
};
}

function createMockXhr() {
return new MockXhr();
}

function MockXhr() {

// hack for testing $http, $httpBackend
Expand Down
12 changes: 6 additions & 6 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('$httpBackend', function() {
})
}
};
$backend = createHttpBackend($browser, MockXhr, fakeTimeout, callbacks, fakeDocument);
$backend = createHttpBackend($browser, createMockXhr, fakeTimeout, callbacks, fakeDocument);
callback = jasmine.createSpy('done');
}));

Expand Down Expand Up @@ -238,7 +238,7 @@ describe('$httpBackend', function() {
expect(response).toBe('response');
});

$backend = createHttpBackend($browser, SyncXhr);
$backend = createHttpBackend($browser, function() { return new SyncXhr() });
$backend('GET', '/url', null, callback);
expect(callback).toHaveBeenCalledOnce();
});
Expand Down Expand Up @@ -414,7 +414,7 @@ describe('$httpBackend', function() {


it('should convert 0 to 200 if content', function() {
$backend = createHttpBackend($browser, MockXhr);
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', 'file:///whatever/index.html', null, callback);
respond(0, 'SOME CONTENT');
Expand All @@ -425,7 +425,7 @@ describe('$httpBackend', function() {


it('should convert 0 to 404 if no content', function() {
$backend = createHttpBackend($browser, MockXhr);
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', 'file:///whatever/index.html', null, callback);
respond(0, '');
Expand Down Expand Up @@ -453,7 +453,7 @@ describe('$httpBackend', function() {

try {

$backend = createHttpBackend($browser, MockXhr);
$backend = createHttpBackend($browser, createMockXhr);

$backend('GET', '/whatever/index.html', null, callback);
respond(0, '');
Expand All @@ -468,7 +468,7 @@ describe('$httpBackend', function() {


it('should return original backend status code if different from 0', function () {
$backend = createHttpBackend($browser, MockXhr);
$backend = createHttpBackend($browser, createMockXhr);

// request to http://
$backend('POST', 'http://rest_api/create_whatever', null, callback);
Expand Down

0 comments on commit 0549e04

Please sign in to comment.