tor-browser

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

browser_resources_stylesheets_header.js (2552B)


      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 we do get the appropriate stylesheet content when the stylesheet is only
      7 // served based on the Accept: text/css header
      8 
      9 add_task(async function () {
     10  const httpServer = createTestHTTPServer();
     11 
     12  httpServer.registerContentType("html", "text/html");
     13 
     14  httpServer.registerPathHandler("/index.html", function (request, response) {
     15    response.setStatusLine(request.httpVersion, 200, "OK");
     16    response.write(`
     17 <!DOCTYPE html>
     18 <meta charset="utf-8">
     19 <title>Test stylesheet</title>
     20 <link href="/test/" rel="stylesheet" type="text/css"/>
     21 <script src="/test/"></script>
     22 <h1>Hello</h1>
     23  `);
     24  });
     25 
     26  let resourceUrlCalls = 0;
     27  // The /test/ URL should be called:
     28  // - once by the content page to load the <link>
     29  // - once by the content page to load the <script>
     30  // - once by DevTools to fetch the stylesheet text
     31  // (we could probably optimize this so we only call once)
     32  const expectedResourceUrlCalls = 3;
     33 
     34  const styleSheetText = `body { background-color: tomato; }`;
     35  httpServer.registerPathHandler("/test/", function (request, response) {
     36    resourceUrlCalls++;
     37    response.setStatusLine(request.httpVersion, 200, "OK");
     38 
     39    if (request.getHeader("Accept").startsWith("text/css")) {
     40      response.setHeader("Content-Type", "text/css", false);
     41      response.write(styleSheetText);
     42      return;
     43    }
     44    response.setHeader("Content-Type", "application/javascript", false);
     45    response.write(`/* NOT A STYLESHEET */`);
     46  });
     47  const port = httpServer.identity.primaryPort;
     48  const TEST_URL = `http://localhost:${port}/index.html`;
     49 
     50  info("Check resource available feature of the ResourceCommand");
     51  const tab = await addTab(TEST_URL);
     52 
     53  const { client, resourceCommand, targetCommand } =
     54    await initResourceCommand(tab);
     55 
     56  info("Check whether ResourceCommand gets existing stylesheet");
     57  const availableResources = [];
     58  await resourceCommand.watchResources([resourceCommand.TYPES.STYLESHEET], {
     59    onAvailable: resources => availableResources.push(...resources),
     60  });
     61  is(
     62    availableResources.length,
     63    1,
     64    "We have the expected number of stylesheets"
     65  );
     66 
     67  is(
     68    await getStyleSheetResourceText(availableResources[0]),
     69    styleSheetText,
     70    "Got expected text for the stylesheet"
     71  );
     72 
     73  is(
     74    resourceUrlCalls,
     75    expectedResourceUrlCalls,
     76    "The /test URL was called the number of time we expected"
     77  );
     78 
     79  targetCommand.destroy();
     80  await client.close();
     81 });