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

Commit 68ceb17

Browse files
corruptIgorMinar
corrupt
authored andcommitted
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
1 parent 5bd6596 commit 68ceb17

File tree

2 files changed

+56
-9
lines changed

2 files changed

+56
-9
lines changed

src/ng/httpBackend.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,11 @@ var XHR = window.XMLHttpRequest || function() {
2828
*/
2929
function $HttpBackendProvider() {
3030
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
31-
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks,
32-
$document[0], $window.location.protocol.replace(':', ''));
31+
return createHttpBackend($browser, XHR, $browser.defer, $window.angular.callbacks, $document[0]);
3332
}];
3433
}
3534

36-
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
35+
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument) {
3736
var ABORTED = -1;
3837

3938
// TODO(vojta): fix the signature
@@ -113,14 +112,14 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
113112
}
114113

115114
function completeRequest(callback, status, response, headersString) {
116-
var protocol = locationProtocol || urlResolve(url).protocol;
115+
var protocol = urlResolve(url).protocol;
117116

118117
// cancel timeout and subsequent timeout promise resolution
119118
timeoutId && $browserDefer.cancel(timeoutId);
120119
jsonpDone = xhr = null;
121120

122121
// fix status code for file protocol (it's always 0)
123-
status = (protocol == 'file') ? (response ? 200 : 404) : status;
122+
status = (protocol == 'file' && status === 0) ? (response ? 200 : 404) : status;
124123

125124
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
126125
status = status == 1223 ? 204 : status;

test/ng/httpBackendSpec.js

+52-4
Original file line numberDiff line numberDiff line change
@@ -436,13 +436,61 @@ describe('$httpBackend', function() {
436436

437437

438438
it('should convert 0 to 404 if no content - relative url', function() {
439-
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
439+
var originalUrlParsingNode = urlParsingNode;
440+
441+
//temporarily overriding the DOM element to pretend that the test runs origin with file:// protocol
442+
urlParsingNode = {
443+
hash : "#/C:/",
444+
host : "",
445+
hostname : "",
446+
href : "file:///C:/base#!/C:/foo",
447+
pathname : "/C:/foo",
448+
port : "",
449+
protocol : "file:",
450+
search : "",
451+
setAttribute: angular.noop
452+
};
440453

441-
$backend('GET', '/whatever/index.html', null, callback);
442-
respond(0, '');
454+
try {
455+
456+
$backend = createHttpBackend($browser, MockXhr);
457+
458+
$backend('GET', '/whatever/index.html', null, callback);
459+
respond(0, '');
460+
461+
expect(callback).toHaveBeenCalled();
462+
expect(callback.mostRecentCall.args[0]).toBe(404);
463+
464+
} finally {
465+
urlParsingNode = originalUrlParsingNode;
466+
}
467+
});
468+
469+
470+
it('should return original backend status code if different from 0', function () {
471+
$backend = createHttpBackend($browser, MockXhr);
472+
473+
// request to http://
474+
$backend('POST', 'http://rest_api/create_whatever', null, callback);
475+
respond(201, '');
443476

444477
expect(callback).toHaveBeenCalled();
445-
expect(callback.mostRecentCall.args[0]).toBe(404);
478+
expect(callback.mostRecentCall.args[0]).toBe(201);
479+
480+
481+
// request to file://
482+
$backend('POST', 'file://rest_api/create_whatever', null, callback);
483+
respond(201, '');
484+
485+
expect(callback).toHaveBeenCalled();
486+
expect(callback.mostRecentCall.args[0]).toBe(201);
487+
488+
// request to file:// with HTTP status >= 300
489+
$backend('POST', 'file://rest_api/create_whatever', null, callback);
490+
respond(503, '');
491+
492+
expect(callback).toHaveBeenCalled();
493+
expect(callback.mostRecentCall.args[0]).toBe(503);
446494
});
447495
});
448496
});

0 commit comments

Comments
 (0)