mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-03-16 23:30:23 +00:00
chore($httpBackend): preserve original non-zero http status code for file:// apps
Previously if an app was running from file:// origin we would always return either http 200 or 404 depending on whether the response was present. This changes the behavior so that we do this only if the protocol of the request (not the origin) is file:// and only if the status code is 0. Closes #4436 Closes #4587 Closes #4514
This commit is contained in:
parent
5bd6596856
commit
68ceb17272
2 changed files with 56 additions and 9 deletions
|
|
@ -28,12 +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], $window.location.protocol.replace(':', ''));
|
||||
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks, $document[0]);
|
||||
}];
|
||||
}
|
||||
|
||||
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
|
||||
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument) {
|
||||
var ABORTED = -1;
|
||||
|
||||
// TODO(vojta): fix the signature
|
||||
|
|
@ -113,14 +112,14 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
|
|||
}
|
||||
|
||||
function completeRequest(callback, status, response, headersString) {
|
||||
var protocol = locationProtocol || urlResolve(url).protocol;
|
||||
var protocol = urlResolve(url).protocol;
|
||||
|
||||
// cancel timeout and subsequent timeout promise resolution
|
||||
timeoutId && $browserDefer.cancel(timeoutId);
|
||||
jsonpDone = xhr = null;
|
||||
|
||||
// fix status code for file protocol (it's always 0)
|
||||
status = (protocol == 'file') ? (response ? 200 : 404) : status;
|
||||
status = (protocol == 'file' && status === 0) ? (response ? 200 : 404) : status;
|
||||
|
||||
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
|
||||
status = status == 1223 ? 204 : status;
|
||||
|
|
|
|||
|
|
@ -436,13 +436,61 @@ describe('$httpBackend', function() {
|
|||
|
||||
|
||||
it('should convert 0 to 404 if no content - relative url', function() {
|
||||
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
|
||||
var originalUrlParsingNode = urlParsingNode;
|
||||
|
||||
$backend('GET', '/whatever/index.html', null, callback);
|
||||
respond(0, '');
|
||||
//temporarily overriding the DOM element to pretend that the test runs origin with file:// protocol
|
||||
urlParsingNode = {
|
||||
hash : "#/C:/",
|
||||
host : "",
|
||||
hostname : "",
|
||||
href : "file:///C:/base#!/C:/foo",
|
||||
pathname : "/C:/foo",
|
||||
port : "",
|
||||
protocol : "file:",
|
||||
search : "",
|
||||
setAttribute: angular.noop
|
||||
};
|
||||
|
||||
try {
|
||||
|
||||
$backend = createHttpBackend($browser, MockXhr);
|
||||
|
||||
$backend('GET', '/whatever/index.html', null, callback);
|
||||
respond(0, '');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(404);
|
||||
|
||||
} finally {
|
||||
urlParsingNode = originalUrlParsingNode;
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it('should return original backend status code if different from 0', function () {
|
||||
$backend = createHttpBackend($browser, MockXhr);
|
||||
|
||||
// request to http://
|
||||
$backend('POST', 'http://rest_api/create_whatever', null, callback);
|
||||
respond(201, '');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(404);
|
||||
expect(callback.mostRecentCall.args[0]).toBe(201);
|
||||
|
||||
|
||||
// request to file://
|
||||
$backend('POST', 'file://rest_api/create_whatever', null, callback);
|
||||
respond(201, '');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(201);
|
||||
|
||||
// request to file:// with HTTP status >= 300
|
||||
$backend('POST', 'file://rest_api/create_whatever', null, callback);
|
||||
respond(503, '');
|
||||
|
||||
expect(callback).toHaveBeenCalled();
|
||||
expect(callback.mostRecentCall.args[0]).toBe(503);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue