tor-browser

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

browser_networkobserver_decode_text.js (3072B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const TEST_URL = URL_ROOT + "doc_network-observer.html";
      7 const JSON_URL = URL_ROOT + "slow_json.sjs";
      8 const JSON_URL2 = URL_ROOT + "slow_json.sjs?complete";
      9 const JSON_URL3 = URL_ROOT + "slow_json.sjs?partial";
     10 
     11 add_task(async function () {
     12  // Test content decoding handled automatically by the network observer.
     13  await testDecodingResponseContent({ decodeResponseBodies: true });
     14  // Test content decoding lazily from the consumer of the event.
     15  await testDecodingResponseContent({ decodeResponseBodies: false });
     16 });
     17 
     18 async function testDecodingResponseContent({ decodeResponseBodies }) {
     19  info(
     20    "Test response content decoding with decodeResponseBodies=" +
     21      decodeResponseBodies
     22  );
     23  const tab = await addTab(TEST_URL);
     24 
     25  const events = [];
     26  const networkObserver = new NetworkObserver({
     27    decodeResponseBodies,
     28    ignoreChannelFunction: channel =>
     29      channel.URI.spec !== JSON_URL && channel.URI.spec !== JSON_URL3,
     30    onNetworkEvent: () => {
     31      const owner = new ResponseContentOwner();
     32      events.push(owner);
     33      return owner;
     34    },
     35  });
     36  registerCleanupFunction(() => networkObserver.destroy());
     37 
     38  await SpecialPowers.spawn(
     39    gBrowser.selectedBrowser,
     40    [JSON_URL, JSON_URL2],
     41    (_url, _url2) => {
     42      const xhr = new content.wrappedJSObject.XMLHttpRequest();
     43      xhr.addEventListener("progress", e => {
     44        if (e.loaded > 0) {
     45          const xhr2 = new content.wrappedJSObject.XMLHttpRequest();
     46          xhr2.open("GET", _url2);
     47          xhr2.send();
     48        }
     49      });
     50      xhr.open("GET", _url);
     51      xhr.send();
     52    }
     53  );
     54 
     55  info("Wait for all network events to be received");
     56  await BrowserTestUtils.waitForCondition(() => events.length >= 1);
     57  is(events.length, 1, "Received the expected number of network events");
     58  await BrowserTestUtils.waitForCondition(
     59    () => events[0].hasResponseContentComplete
     60  );
     61 
     62  is(
     63    events[0].isContentEncoded,
     64    !decodeResponseBodies,
     65    "The isContentEncoded flag has the expected value"
     66  );
     67  is(
     68    await events[0].getDecodedContent(),
     69    '"\u3042"',
     70    "expected response content"
     71  );
     72  is(events[0].truncated, false, "response content should not be truncated");
     73 
     74  await SpecialPowers.spawn(gBrowser.selectedBrowser, [JSON_URL3], _url => {
     75    content.wrappedJSObject.fetch(_url);
     76  });
     77 
     78  info("Wait for all network events to be received");
     79  await BrowserTestUtils.waitForCondition(() => events.length >= 2);
     80  is(events.length, 2, "Received the expected number of network events");
     81 
     82  await BrowserTestUtils.waitForCondition(
     83    () => events[1].hasResponseContentComplete
     84  );
     85 
     86  is(
     87    events[1].isContentEncoded,
     88    !decodeResponseBodies,
     89    "The isContentEncoded flag has the expected value"
     90  );
     91  is(await events[1].getDecodedContent(), '"', "expected response content");
     92  todo_is(events[1].truncated, true, "response content would be truncated");
     93 
     94  networkObserver.destroy();
     95  gBrowser.removeTab(tab);
     96 }