summaryrefslogtreecommitdiffstats
path: root/devtools/shared/commands/resource/tests/browser_resources_stylesheets_header.js
blob: 29263d887bbd97788525865b5832faa7ee510bc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that we do get the appropriate stylesheet content when the stylesheet is only
// served based on the Accept: text/css header

add_task(async function () {
  const httpServer = createTestHTTPServer();

  httpServer.registerContentType("html", "text/html");

  httpServer.registerPathHandler("/index.html", function (request, response) {
    response.setStatusLine(request.httpVersion, 200, "OK");
    response.write(`
<!DOCTYPE html>
<meta charset="utf-8">
<title>Test stylesheet</title>
<link href="/test/" rel="stylesheet" type="text/css"/>
<script src="/test/"></script>
<h1>Hello</h1>
  `);
  });

  let resourceUrlCalls = 0;
  // The /test/ URL should be called:
  // - once by the content page to load the <link>
  // - once by the content page to load the <script>
  // - once by DevTools to fetch the stylesheet text
  // (we could probably optimize this so we only call once)
  const expectedResourceUrlCalls = 3;

  const styleSheetText = `body { background-color: tomato; }`;
  httpServer.registerPathHandler("/test/", function (request, response) {
    resourceUrlCalls++;
    response.setStatusLine(request.httpVersion, 200, "OK");

    if (request.getHeader("Accept").startsWith("text/css")) {
      response.setHeader("Content-Type", "text/css", false);
      response.write(styleSheetText);
      return;
    }
    response.setHeader("Content-Type", "application/javascript", false);
    response.write(`/* NOT A STYLESHEET */`);
  });
  const port = httpServer.identity.primaryPort;
  const TEST_URL = `http://localhost:${port}/index.html`;

  info("Check resource available feature of the ResourceCommand");
  const tab = await addTab(TEST_URL);

  const { client, resourceCommand, targetCommand } = await initResourceCommand(
    tab
  );

  info("Check whether ResourceCommand gets existing stylesheet");
  const availableResources = [];
  await resourceCommand.watchResources([resourceCommand.TYPES.STYLESHEET], {
    onAvailable: resources => availableResources.push(...resources),
  });
  is(
    availableResources.length,
    1,
    "We have the expected number of stylesheets"
  );

  is(
    await getStyleSheetResourceText(availableResources[0]),
    styleSheetText,
    "Got expected text for the stylesheet"
  );

  is(
    resourceUrlCalls,
    expectedResourceUrlCalls,
    "The /test URL was called the number of time we expected"
  );

  targetCommand.destroy();
  await client.close();
});