1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // A simple, incomplete implementation of Fetch, intended to facilitate end
6 // to end serviceworker testing.
8 // See http://fetch.spec.whatwg.org/#fetch-method
11 var _castToRequest = function (item) {
12 if (typeof item === 'string') {
20 // FIXME: Support init argument to fetch.
21 var fetch = function(request) {
22 request = _castToRequest(request);
24 return new Promise(function(resolve, reject) {
25 // FIXME: Use extra headers from |request|.
26 var xhr = new XMLHttpRequest();
27 xhr.open(request.method, request.url, true /* async */);
29 xhr.onreadystatechange = function() {
30 if (xhr.readyState !== 4) return;
32 var response = new Response({
34 statusText: xhr.statusText,
35 // FIXME: Set response.headers.
36 // FIXME: Set response.method when available.
37 // FIXME: Set response.url when available.
38 // FIXME: Set response.body when available.
41 if (xhr.status === 200) {
50 global.fetch = global.fetch || fetch;
51 }(self)); // window or worker global scope