tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

fetch-event.https.h2.html (4467B)


      1 <!DOCTYPE html>
      2 <meta name=timeout content=long>
      3 <script src="/resources/testharness.js"></script>
      4 <script src="/resources/testharnessreport.js"></script>
      5 <script src="/common/get-host-info.sub.js"></script>
      6 <script src="/common/utils.js"></script>
      7 <script src="resources/test-helpers.sub.js"></script>
      8 <body>
      9 <script>
     10 const worker = 'resources/fetch-event-test-worker.js';
     11 
     12 const method = 'POST';
     13 const duplex = 'half';
     14 
     15 function createBody(t) {
     16  const rs = new ReadableStream({start(c) {
     17    c.enqueue('i a');
     18    c.enqueue('m the request');
     19    step_timeout(t.step_func(() => {
     20      c.enqueue(' body');
     21      c.close();
     22    }, 10));
     23  }});
     24  return rs.pipeThrough(new TextEncoderStream());
     25 }
     26 
     27 promise_test(async t => {
     28  const scope = 'resources/';
     29  const registration =
     30    await service_worker_unregister_and_register(t, worker, scope);
     31  await wait_for_state(t, registration.installing, 'activated');
     32 
     33  // This will happen after all other tests
     34  promise_test(t => {
     35    return registration.unregister();
     36  }, 'restore global state');
     37 }, 'global setup');
     38 
     39 // Test that the service worker can read FetchEvent#body when it is made from
     40 // a ReadableStream. It responds with request body it read.
     41 promise_test(async t => {
     42  const body = createBody(t);
     43  // Set page_url to "?ignore" so the service worker falls back to network
     44  // for the main resource request, and add a suffix to avoid colliding
     45  // with other tests.
     46  const page_url = `resources/simple.html?ignore&id=${token()}`;
     47  const frame = await with_iframe(page_url);
     48  t.add_cleanup(() => { frame.remove(); });
     49  const response = await frame.contentWindow.fetch('simple.html?request-body', {
     50    method, body, duplex});
     51  assert_equals(response.status, 200, 'status');
     52  const text = await response.text();
     53  assert_equals(text, 'i am the request body', 'body');
     54 }, 'The streaming request body is readable in the service worker.');
     55 
     56 // Network fallback
     57 promise_test(async t => {
     58  const body = createBody(t);
     59  // Set page_url to "?ignore" so the service worker falls back to network
     60  // for the main resource request, and add a suffix to avoid colliding
     61  // with other tests.
     62  const page_url = `resources/simple.html?ignore&id=${token()}`;
     63  const frame = await with_iframe(page_url);
     64  t.add_cleanup(() => { frame.remove(); });
     65  // Add "?ignore" so that the service worker falls back to
     66  // echo-content.h2.py.
     67  const echo_url = '/fetch/api/resources/echo-content.h2.py?ignore';
     68  const response =
     69    await frame.contentWindow.fetch(echo_url, { method, body, duplex});
     70  assert_equals(response.status, 200, 'status');
     71  const text = await response.text();
     72  assert_equals(text, 'i am the request body', 'body');
     73 }, 'Network fallback for streaming upload.');
     74 
     75 // When the streaming body is used in the service worker, network fallback
     76 // fails.
     77 promise_test(async t => {
     78  const body = createBody(t);
     79  // Set page_url to "?ignore" so the service worker falls back to network
     80  // for the main resource request, and add a suffix to avoid colliding
     81  // with other tests.
     82  const page_url = `resources/simple.html?ignore&id=${token()}`;
     83  const frame = await with_iframe(page_url);
     84  t.add_cleanup(() => { frame.remove(); });
     85  const echo_url = '/fetch/api/resources/echo-content.h2.py?use-and-ignore';
     86  const w = frame.contentWindow;
     87  await promise_rejects_js(t, w.TypeError, w.fetch(echo_url, {
     88    method, body, duplex}));
     89 }, 'When the streaming request body is used, network fallback fails.');
     90 
     91 // When the streaming body is used by clone() in the service worker, network
     92 // fallback succeeds.
     93 promise_test(async t => {
     94  const body = createBody(t);
     95  // Set page_url to "?ignore" so the service worker falls back to network
     96  // for the main resource request, and add a suffix to avoid colliding
     97  // with other tests.
     98  const page_url = `resources/simple.html?ignore&id=${token()}`;
     99  const frame = await with_iframe(page_url);
    100  t.add_cleanup(() => { frame.remove(); });
    101  // Add "?clone-and-ignore" so that the service worker falls back to
    102  // echo-content.h2.py.
    103  const echo_url = '/fetch/api/resources/echo-content.h2.py?clone-and-ignore';
    104  const response = await frame.contentWindow.fetch(echo_url, {
    105    method, body, duplex});
    106  assert_equals(response.status, 200, 'status');
    107  const text = await response.text();
    108  assert_equals(text, 'i am the request body', 'body');
    109 }, 'Running clone() in the service worker does not prevent network fallback.');
    110 
    111 </script>
    112 </body>