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

Commit caeb1bf

Browse files
vojtajinaIgorMinar
authored andcommitted
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.
1 parent 9b4efa7 commit caeb1bf

File tree

2 files changed

+72
-8
lines changed

2 files changed

+72
-8
lines changed

src/service/httpBackend.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,14 @@ var XHR = window.XMLHttpRequest || function() {
1717
*/
1818
function $HttpBackendProvider() {
1919
this.$get = ['$browser', '$window', '$document', function($browser, $window, $document) {
20-
return createHttpBackend($browser, XHR, $browser.defer, $window, $document[0].body);
20+
return createHttpBackend($browser, XHR, $browser.defer, $window, $document[0].body,
21+
$window.location.href.replace(':', ''));
2122
}];
2223
}
2324

24-
function createHttpBackend($browser, XHR, $browserDefer, $window, body) {
25+
function createHttpBackend($browser, XHR, $browserDefer, $window, body, locationProtocol) {
2526
var idCounter = 0;
2627

27-
function completeRequest(callback, status, response) {
28-
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
29-
callback(status == 1223 ? 204 : status, response);
30-
$browser.$$completeOutstandingRequest(noop);
31-
}
32-
3328
// TODO(vojta): fix the signature
3429
return function(method, url, post, callback, headers, timeout) {
3530
$browser.$$incOutstandingRequestCount();
@@ -81,6 +76,20 @@ function createHttpBackend($browser, XHR, $browserDefer, $window, body) {
8176

8277
return xhr;
8378
}
79+
80+
function completeRequest(callback, status, response) {
81+
// URL_MATCH is defined in src/service/location.js
82+
var protocol = (url.match(URL_MATCH) || ['', locationProtocol])[1];
83+
84+
// fix status code for file protocol (it's always 0)
85+
status = protocol == 'file' ? (response ? 200 : 404) : status;
86+
87+
// normalize IE bug (http://bugs.jquery.com/ticket/1450)
88+
status = status == 1223 ? 204 : status;
89+
90+
callback(status, response);
91+
$browser.$$completeOutstandingRequest(noop);
92+
}
8493
};
8594
}
8695

test/service/httpBackendSpec.js

+55
Original file line numberDiff line numberDiff line change
@@ -175,5 +175,60 @@ describe('$httpBackend', function() {
175175
// TODO(vojta): test whether it fires "async-start"
176176
// TODO(vojta): test whether it fires "async-end" on both success and error
177177
});
178+
179+
describe('file protocol', function() {
180+
181+
function respond(status, content) {
182+
xhr = MockXhr.$$lastInstance;
183+
xhr.status = status;
184+
xhr.responseText = content;
185+
xhr.readyState = 4;
186+
xhr.onreadystatechange();
187+
}
188+
189+
190+
it('should convert 0 to 200 if content', function() {
191+
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');
192+
193+
$backend('GET', 'file:///whatever/index.html', null, callback);
194+
respond(0, 'SOME CONTENT');
195+
196+
expect(callback).toHaveBeenCalled();
197+
expect(callback.mostRecentCall.args[0]).toBe(200);
198+
});
199+
200+
201+
it('should convert 0 to 200 if content - relative url', function() {
202+
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
203+
204+
$backend('GET', '/whatever/index.html', null, callback);
205+
respond(0, 'SOME CONTENT');
206+
207+
expect(callback).toHaveBeenCalled();
208+
expect(callback.mostRecentCall.args[0]).toBe(200);
209+
});
210+
211+
212+
it('should convert 0 to 404 if no content', function() {
213+
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'http');
214+
215+
$backend('GET', 'file:///whatever/index.html', null, callback);
216+
respond(0, '');
217+
218+
expect(callback).toHaveBeenCalled();
219+
expect(callback.mostRecentCall.args[0]).toBe(404);
220+
});
221+
222+
223+
it('should convert 0 to 200 if content - relative url', function() {
224+
$backend = createHttpBackend($browser, MockXhr, null, null, null, 'file');
225+
226+
$backend('GET', '/whatever/index.html', null, callback);
227+
respond(0, '');
228+
229+
expect(callback).toHaveBeenCalled();
230+
expect(callback.mostRecentCall.args[0]).toBe(404);
231+
});
232+
});
178233
});
179234

0 commit comments

Comments
 (0)