mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-17 07:40:22 +00:00
feat($httpBackend): fix 0 status code when "file" protocol
Browsers return always 0 status code for "file" protocol, so we convert them into 200/404.
This commit is contained in:
parent
9b4efa73f9
commit
caeb1bf899
2 changed files with 72 additions and 8 deletions
|
|
@ -17,19 +17,14 @@ var XHR = window.XMLHttpRequest || function() {
|
|||
*/
|
||||
function $HttpBackendProvider() {
|
||||
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
|
||||
return createHttpBackend($browser, XHR, $browser.defer, $window, $document[0].body);
|
||||
return createHttpBackend($browser, XHR, $browser.defer, $window, $document[0].body,
|
||||
$window.location.href.replace(':', ''));
|
||||
}];
|
||||
}
|
||||
|
||||
function createHttpBackend($browser, XHR, $browserDefer, $window, body) {
|
||||
function createHttpBackend($browser, XHR, $browserDefer, $window, body, locationProtocol) {
|
||||
var idCounter = 0;
|
||||
|
||||
function completeRequest(callback, status, response) {
|
||||
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
|
||||
callback(status == 1223 ? 204 : status, response);
|
||||
$browser.$$completeOutstandingRequest(noop);
|
||||
}
|
||||
|
||||
// TODO(vojta): fix the signature
|
||||
return function(method, url, post, callback, headers, timeout) {
|
||||
$browser.$$incOutstandingRequestCount();
|
||||
|
|
@ -81,6 +76,20 @@ function createHttpBackend($browser, XHR, $browserDefer, $window, body) {
|
|||
|
||||
return xhr;
|
||||
}
|
||||
|
||||
function completeRequest(callback, status, response) {
|
||||
// URL_MATCH is defined in src/service/location.js
|
||||
var protocol = (url.match(URL_MATCH) || ['', locationProtocol])[1];
|
||||
|
||||
// fix status code for file protocol (it's always 0)
|
||||
status = protocol == 'file' ? (response ? 200 : 404) : status;
|
||||
|
||||
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
|
||||
status = status == 1223 ? 204 : status;
|
||||
|
||||
callback(status, response);
|
||||
$browser.$$completeOutstandingRequest(noop);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -175,5 +175,60 @@ describe('$httpBackend', function() {
|
|||
// TODO(vojta): test whether it fires "async-start"
|
||||
// TODO(vojta): test whether it fires "async-end" on both success and error
|
||||
});
|
||||
|
||||
describe('file protocol', function() {
|
||||
|
||||
function respond(status, content) {
|
||||
xhr = MockXhr.$$lastInstance;
|
||||
xhr.status = status;
|
||||
xhr.responseText = content;
|
||||
xhr.readyState = 4;
|
||||
xhr.onreadystatechange();
|
||||
}
|
||||
|
||||
|
||||
it('should convert 0 to 200 if content', function() {
|
||||
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');
|
||||
|
||||
$backend('GET', 'file:///whatever/index.html', null, callback);
|
||||
respond(0, 'SOME CONTENT');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(200);
|
||||
});
|
||||
|
||||
|
||||
it('should convert 0 to 200 if content - relative url', function() {
|
||||
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
|
||||
|
||||
$backend('GET', '/whatever/index.html', null, callback);
|
||||
respond(0, 'SOME CONTENT');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(200);
|
||||
});
|
||||
|
||||
|
||||
it('should convert 0 to 404 if no content', function() {
|
||||
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');
|
||||
|
||||
$backend('GET', 'file:///whatever/index.html', null, callback);
|
||||
respond(0, '');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(404);
|
||||
});
|
||||
|
||||
|
||||
it('should convert 0 to 200 if content - relative url', function() {
|
||||
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
|
||||
|
||||
$backend('GET', '/whatever/index.html', null, callback);
|
||||
respond(0, '');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(404);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue