OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 // A simple, incomplete implementation of Fetch, intended to facilitate end |
| 6 // to end serviceworker testing. |
| 7 |
| 8 // See http://fetch.spec.whatwg.org/#fetch-method |
| 9 |
| 10 (function (global) { |
| 11 var _castToRequest = function (item) { |
| 12 if (typeof item === 'string') { |
| 13 item = new Request({ |
| 14 url: item |
| 15 }); |
| 16 } |
| 17 return item; |
| 18 }; |
| 19 |
| 20 // FIXME: Support init argument to fetch. |
| 21 var fetch = function(request) { |
| 22 request = _castToRequest(request); |
| 23 |
| 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 */); |
| 28 xhr.send(null); |
| 29 xhr.onreadystatechange = function() { |
| 30 if (xhr.readyState !== 4) return; |
| 31 |
| 32 var response = new Response({ |
| 33 status: xhr.status, |
| 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. |
| 39 }); |
| 40 |
| 41 if (xhr.status === 200) { |
| 42 resolve(response); |
| 43 } else { |
| 44 reject(response); |
| 45 } |
| 46 }; |
| 47 }); |
| 48 }; |
| 49 |
| 50 global.fetch = global.fetch || fetch; |
| 51 }(self)); // window or worker global scope |
OLD | NEW |