summaryrefslogtreecommitdiffstats
path: root/netwerk/test/mochitests/test_accept_header.html
blob: b8434230f451cf3f2e25f1000fa594080f865bbe (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<!DOCTYPE HTML>
<html>
<head>
  <title>Accept header</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script>

// All the requests are sent to test_accept_header.sjs which will return
// different content based on the queryString. When the queryString is 'get',
// test_accept_header.sjs returns a JSON object with the latest request and its
// accept header value.

function test_last_request_and_continue(query, expected) {
  fetch("test_accept_header.sjs?get").then(r => r.json()).then(json => {
    is(json.type, query, "Expected: " + query);
    is(json.accept, expected, "Accept header: " + expected);
    next();
  });
}

function test_iframe() {
  let observer = new PerformanceObserver(function(list, obj) {
    obj.disconnect();

    list.getEntries().forEach(entry => {
      if (entry.name.endsWith("test_accept_header.sjs?iframe")) {
        obj.disconnect();
        test_last_request_and_continue("iframe", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
      }
    });
  });

  observer.observe({entryTypes: ["resource"]});

  let ifr = document.createElement("iframe");
  ifr.src = "test_accept_header.sjs?iframe";
  document.body.appendChild(ifr);
}

function test_image() {
  let i = new Image();
  i.src = "test_accept_header.sjs?image";
  i.onload = function() {
    // Fetch spec says we should have: "image/png,image/svg+xml,image/*;q=0.8,*/*;q=0.5"
    test_last_request_and_continue("image", "image/webp,image/png,image/*;q=0.8,*/*;q=0.5");
  }
}

function test_style() {
  let observer = new PerformanceObserver(function(list, obj) {
    obj.disconnect();

    list.getEntries().forEach(entry => {
      if (entry.name.endsWith("test_accept_header.sjs?style")) {
        obj.disconnect();
        test_last_request_and_continue("style", "text/css,*/*;q=0.1");
      }
    });
  });

  observer.observe({entryTypes: ["resource"]});

  let head = document.getElementsByTagName("head")[0];
  let link = document.createElement("link");
  link.rel = "stylesheet";
  link.type = "text/css";
  link.href = "test_accept_header.sjs?style";
  head.appendChild(link);
}

function test_worker() {
  let w = new Worker("test_accept_header.sjs?worker");
  w.onmessage = function() {
    test_last_request_and_continue("worker", "*/*");
  }
}

let tests = [
  test_iframe,
  test_image,
  test_style,
  test_worker,
];

function next() {
  if (tests.length == 0) {
    SimpleTest.finish();
    return;
  }

  let test = tests.shift();
  test();
}

SimpleTest.waitForExplicitFinish();

SpecialPowers.pushPrefEnv({ "set": [
  [ "dom.enable_performance_observer", true ]
]}, next);

</script>
</body>
</html>