MIRROR: javascript for 馃悳's, a tiny runtime with big ambitions
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at master 71 lines 2.4 kB view raw
1import { test, summary } from './helpers.js'; 2 3console.log('Fetch Tests\n'); 4 5async function testRejects(name, fn) { 6 try { 7 await fn(); 8 test(`${name} rejects`, false, true); 9 } catch (_err) { 10 test(`${name} rejects`, true, true); 11 } 12} 13 14test('fetch typeof', typeof fetch, 'function'); 15 16const textResponse = await fetch('data:,hello%20world'); 17test('fetch data url returns Response', textResponse instanceof Response, true); 18test('fetch data url status', textResponse.status, 200); 19test('fetch data url ok', textResponse.ok, true); 20test('fetch data url url', textResponse.url, 'data:,hello%20world'); 21test('fetch data url text', await textResponse.text(), 'hello world'); 22 23const jsonResponse = await fetch('data:application/json,%7B%22ok%22%3Atrue%7D'); 24test('fetch data url json content-type', jsonResponse.headers.get('content-type'), 'application/json'); 25test('fetch data url json body', (await jsonResponse.json()).ok, true); 26 27test( 28 'fetch response headers immutable', 29 (() => { 30 try { 31 jsonResponse.headers.append('x-test', '1'); 32 return false; 33 } catch (_err) { 34 return true; 35 } 36 })(), 37 true 38); 39 40const requestInput = new Request('data:text/plain,request-body'); 41const requestResponse = await fetch(requestInput); 42test('fetch accepts Request input', await requestResponse.text(), 'request-body'); 43 44const encoder = new TextEncoder(); 45const streamBody = new ReadableStream({ 46 start(controller) { 47 controller.enqueue(encoder.encode('{"runtime":"ant",')); 48 controller.enqueue(encoder.encode('"stream":true}')); 49 controller.close(); 50 } 51}); 52const streamedUploadResponse = await fetch('https://httpbingo.org/post', { 53 method: 'POST', 54 body: streamBody, 55 duplex: 'half', 56 headers: { 57 'Content-Type': 'application/json' 58 } 59}); 60const streamedUploadJson = await streamedUploadResponse.json(); 61test('fetch streamed body status', streamedUploadResponse.status, 200); 62test('fetch streamed body json runtime', streamedUploadJson.json.runtime, 'ant'); 63test('fetch streamed body json stream', streamedUploadJson.json.stream, true); 64 65const abortedController = new AbortController(); 66abortedController.abort(new Error('aborted')); 67await testRejects('fetch with aborted signal', () => fetch('data:,ignored', { signal: abortedController.signal })); 68 69await testRejects('fetch unsupported scheme', () => fetch('file:///tmp/ant-fetch-unsupported')); 70 71summary();