Teach XHR how to handle relative urls.

Hixie says window.URL may not exist yet in the
sky specs, but I'm sure we'll need something like it.

R=ojan@chromium.org, abarth@chromium.org

Review URL: https://codereview.chromium.org/729913003
This commit is contained in:
Eric Seidel 2014-11-18 11:15:56 -08:00
parent 4d83953263
commit a511ef074a
3 changed files with 34 additions and 1 deletions

View File

@ -4,6 +4,9 @@
<import src="/mojo/services/public/interfaces/network/url_loader.mojom.sky" as="loader" />
<import src="shell.sky" as="shell" />
<script>
// XHR keeps itself alive.
var outstandingRequests = new Set();
function XMLHttpRequest() {
this.networkService_ = shell.connectToService(
"mojo:network_service", net.NetworkService);
@ -18,7 +21,7 @@ XMLHttpRequest.prototype.onerror = function(error) { };
XMLHttpRequest.prototype.open = function(method, url) {
this.request_ = new loader.URLRequest();
this.request_.url = url;
this.request_.url = new URL(url, document.URL);
this.request_.method = method;
this.request_.auto_follow_redirects = true;
this.headers_.clear();
@ -41,12 +44,15 @@ XMLHttpRequest.prototype.send = function() {
this.loader_ = shell.wrapHandle(pipe.handle0, loader.URLLoader);
var self = this;
outstandingRequests.add(this);
this.loader_.start(this.request_).then(function(result) {
return core.drainData(result.response.body).then(function(result) {
outstandingRequests.delete(self);
self.responseText = unicode.decodeUtf8String(new Uint8Array(result.buffer));
self.onload();
});
}).catch(function(error) {
outstandingRequests.delete(self);
self.onerror(error);
});
};

View File

@ -0,0 +1,5 @@
Running 1 tests
ok 1 XMLHttpRequest should be able to fetch relative urls
1 tests
1 pass
0 fail

View File

@ -0,0 +1,22 @@
<html>
<import src="../resources/chai.sky" />
<import src="../resources/mocha.sky" />
<import src="/sky/framework/xmlhttprequest.sky" as="XMLHttpRequest" />
<script>
describe('XMLHttpRequest', function() {
this.enableTimeouts(false);
it('should be able to fetch relative urls', function(done) {
var xhr = new XMLHttpRequest();
xhr.onload = function() {
// Also testing that "this" is set correctly in the onload callback.
assert.equal(this.responseText, "This is data from the network.\n");
done();
};
xhr.open("GET", "resources/pass.txt");
xhr.send();
});
});
</script>
</html>