tor-browser

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

fetch-uploads.https.window.js (2609B)


      1 // META: script=/service-workers/service-worker/resources/test-helpers.sub.js
      2 // META: script=resources/utils.js
      3 'use strict';
      4 
      5 // Covers basic functionality provided by BackgroundFetchManager.fetch().
      6 // Specifically, when `fetch` contains request uploads.
      7 // https://wicg.github.io/background-fetch/#background-fetch-manager-fetch
      8 
      9 backgroundFetchTest(async (test, backgroundFetch) => {
     10  const uploadData = 'Background Fetch!';
     11  const request =
     12      new Request('resources/upload.py', {method: 'POST', body: uploadData});
     13 
     14  const registration = await backgroundFetch.fetch(uniqueId(), request);
     15  assert_equals(registration.uploadTotal, uploadData.length);
     16 
     17  const {type, eventRegistration, results} = await getMessageFromServiceWorker();
     18  assert_equals(type, 'backgroundfetchsuccess');
     19  assert_equals(results.length, 1);
     20  assert_equals(eventRegistration.result, 'success');
     21  assert_equals(eventRegistration.failureReason, '');
     22  assert_equals(eventRegistration.uploaded, uploadData.length);
     23  assert_equals(results[0].text, uploadData);
     24 }, 'Fetch with an upload should work');
     25 
     26 backgroundFetchTest(async (test, backgroundFetch) => {
     27  const uploadData = 'Background Fetch!';
     28  const uploadRequest =
     29      new Request('resources/upload.py', {method: 'POST', body: uploadData});
     30 
     31  const registration = await backgroundFetch.fetch(
     32      uniqueId(),
     33      [uploadRequest, '/common/slow.py']);
     34 
     35    const uploaded = await new Promise(resolve => {
     36      registration.onprogress = event => {
     37        if (event.target.downloaded === 0)
     38          return;
     39        // If a progress event with downloaded bytes was received, then
     40        // everything was uploaded.
     41        resolve(event.target.uploaded);
     42      };
     43    });
     44 
     45  assert_equals(uploaded, uploadData.length);
     46 }, 'Progress event includes uploaded bytes');
     47 
     48 backgroundFetchTest(async (test, backgroundFetch) => {
     49  const uploadRequest1 =
     50      new Request('resources/upload.py', {method: 'POST', body: 'upload1'});
     51  const uploadRequest2 =
     52      new Request('resources/upload.py', {method: 'POST', body: 'upload2'});
     53 
     54  await backgroundFetch.fetch(uniqueId(), [uploadRequest1, uploadRequest2]);
     55 
     56  const {type, eventRegistration, results} = await getMessageFromServiceWorker();
     57  assert_equals(type, 'backgroundfetchsuccess');
     58  assert_equals(results.length, 2);
     59  assert_equals(eventRegistration.result, 'success');
     60  assert_equals(eventRegistration.failureReason, '');
     61 
     62  assert_array_equals([results[0].text, results[1].text].sort(),
     63                      ['upload1', 'upload2']);
     64 }, 'Duplicate upload requests work and can be distinguished.');