tor-browser

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

browser_network_longstring.js (5538B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 // Test that the network actor uses the LongStringActor
      7 
      8 const {
      9  DevToolsServer,
     10 } = require("resource://devtools/server/devtools-server.js");
     11 const LONG_STRING_LENGTH = 400;
     12 const LONG_STRING_INITIAL_LENGTH = 400;
     13 let ORIGINAL_LONG_STRING_LENGTH, ORIGINAL_LONG_STRING_INITIAL_LENGTH;
     14 
     15 add_task(async function () {
     16  const tab = await addTab(URL_ROOT + "network_requests_iframe.html");
     17 
     18  const commands = await CommandsFactory.forTab(tab);
     19  await commands.targetCommand.startListening();
     20 
     21  // Override the default long string settings to lower values.
     22  // This is done from the parent process's DevToolsServer as the LongString
     23  // actor is being created from the parent process as network requests are
     24  // watched from the parent process.
     25  ORIGINAL_LONG_STRING_LENGTH = DevToolsServer.LONG_STRING_LENGTH;
     26  ORIGINAL_LONG_STRING_INITIAL_LENGTH =
     27    DevToolsServer.LONG_STRING_INITIAL_LENGTH;
     28 
     29  DevToolsServer.LONG_STRING_LENGTH = LONG_STRING_LENGTH;
     30  DevToolsServer.LONG_STRING_INITIAL_LENGTH = LONG_STRING_INITIAL_LENGTH;
     31 
     32  info("test network POST request");
     33  const networkResource = await new Promise(resolve => {
     34    commands.resourceCommand
     35      .watchResources([commands.resourceCommand.TYPES.NETWORK_EVENT], {
     36        onAvailable: () => {},
     37        onUpdated: resourceUpdate => {
     38          resolve(resourceUpdate[0].resource);
     39        },
     40      })
     41      .then(() => {
     42        // Spawn the network request after we started watching
     43        SpecialPowers.spawn(gBrowser.selectedBrowser, [], async function () {
     44          content.wrappedJSObject.testXhrPost();
     45        });
     46      });
     47  });
     48 
     49  const netActor = networkResource.actor;
     50  ok(netActor, "We have a netActor:" + netActor);
     51 
     52  const { client } = commands;
     53  const requestHeaders = await client.request({
     54    to: netActor,
     55    type: "getRequestHeaders",
     56  });
     57  assertRequestHeaders(requestHeaders);
     58  const requestCookies = await client.request({
     59    to: netActor,
     60    type: "getRequestCookies",
     61  });
     62  assertRequestCookies(requestCookies);
     63  const requestPostData = await client.request({
     64    to: netActor,
     65    type: "getRequestPostData",
     66  });
     67  assertRequestPostData(requestPostData);
     68  const responseHeaders = await client.request({
     69    to: netActor,
     70    type: "getResponseHeaders",
     71  });
     72  assertResponseHeaders(responseHeaders);
     73  const responseCookies = await client.request({
     74    to: netActor,
     75    type: "getResponseCookies",
     76  });
     77  assertResponseCookies(responseCookies);
     78  const responseContent = await client.request({
     79    to: netActor,
     80    type: "getResponseContent",
     81  });
     82  assertResponseContent(responseContent);
     83  const eventTimings = await client.request({
     84    to: netActor,
     85    type: "getEventTimings",
     86  });
     87  assertEventTimings(eventTimings);
     88 
     89  await commands.destroy();
     90 
     91  DevToolsServer.LONG_STRING_LENGTH = ORIGINAL_LONG_STRING_LENGTH;
     92  DevToolsServer.LONG_STRING_INITIAL_LENGTH =
     93    ORIGINAL_LONG_STRING_INITIAL_LENGTH;
     94 });
     95 
     96 function assertRequestHeaders(response) {
     97  info("checking request headers");
     98 
     99  ok(!!response.headers.length, "request headers > 0");
    100  Assert.greater(response.headersSize, 0, "request headersSize > 0");
    101 
    102  checkHeadersOrCookies(response.headers, {
    103    Referer: /network_requests_iframe\.html/,
    104    Cookie: /bug768096/,
    105  });
    106 }
    107 
    108 function assertRequestCookies(response) {
    109  info("checking request cookies");
    110 
    111  is(response.cookies.length, 3, "request cookies length");
    112 
    113  checkHeadersOrCookies(response.cookies, {
    114    foobar: "fooval",
    115    omgfoo: "bug768096",
    116    badcookie: "bug826798=st3fan",
    117  });
    118 }
    119 
    120 function assertRequestPostData(response) {
    121  info("checking request POST data");
    122 
    123  checkObject(response, {
    124    postData: {
    125      text: {
    126        type: "longString",
    127        initial: /^Hello world! foobaz barr.+foobaz barrfo$/,
    128        length: 563,
    129        actor: /[a-z]/,
    130      },
    131    },
    132    postDataDiscarded: false,
    133  });
    134 
    135  is(
    136    response.postData.text.initial.length,
    137    LONG_STRING_INITIAL_LENGTH,
    138    "postData text initial length"
    139  );
    140 }
    141 
    142 function assertResponseHeaders(response) {
    143  info("checking response headers");
    144 
    145  ok(!!response.headers.length, "response headers > 0");
    146  Assert.greater(response.headersSize, 0, "response headersSize > 0");
    147 
    148  checkHeadersOrCookies(response.headers, {
    149    "content-type": /^application\/(json|octet-stream)$/,
    150    "content-length": /^\d+$/,
    151    "x-very-short": "hello world",
    152    "x-very-long": {
    153      type: "longString",
    154      length: 521,
    155      initial: /^Lorem ipsum.+\. Donec vitae d$/,
    156      actor: /[a-z]/,
    157    },
    158  });
    159 }
    160 
    161 function assertResponseCookies(response) {
    162  info("checking response cookies");
    163 
    164  is(response.cookies.length, 0, "response cookies length");
    165 }
    166 
    167 function assertResponseContent(response) {
    168  info("checking response content");
    169 
    170  checkObject(response, {
    171    content: {
    172      text: {
    173        type: "longString",
    174        initial: /^\{ id: "test JSON data"(.|\r|\n)+ barfoo ba$/g,
    175        length: 1070,
    176        actor: /[a-z]/,
    177      },
    178    },
    179    contentDiscarded: false,
    180  });
    181 
    182  is(
    183    response.content.text.initial.length,
    184    LONG_STRING_INITIAL_LENGTH,
    185    "content initial length"
    186  );
    187 }
    188 
    189 function assertEventTimings(response) {
    190  info("checking event timings");
    191 
    192  checkObject(response, {
    193    timings: {
    194      blocked: /^-1|\d+$/,
    195      dns: /^-1|\d+$/,
    196      connect: /^-1|\d+$/,
    197      send: /^-1|\d+$/,
    198      wait: /^-1|\d+$/,
    199      receive: /^-1|\d+$/,
    200    },
    201    totalTime: /^\d+$/,
    202  });
    203 }