summaryrefslogtreecommitdiffstats
path: root/netwerk/test/browser/browser_103_referrer_policy.js
blob: 70879eb835a53fc62b0df957d312019815777484 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

Services.prefs.setBoolPref("network.early-hints.enabled", true);

async function test_referrer_policy(input, expected_results) {
  // reset the count
  let headers = new Headers();
  headers.append("X-Early-Hint-Count-Start", "");
  await fetch(
    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
    { headers }
  );

  await fetch(
    "https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?action=reset_referrer_results"
  );

  let requestUrl = `https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?as=${
    input.resource_type
  }&hinted=${input.hinted ? "1" : "0"}${
    input.header_referrer_policy
      ? "&header_referrer_policy=" + input.header_referrer_policy
      : ""
  }
    ${
      input.link_referrer_policy
        ? "&link_referrer_policy=" + input.link_referrer_policy
        : ""
    }`;

  await BrowserTestUtils.withNewTab(
    {
      gBrowser,
      url: requestUrl,
      waitForLoad: true,
    },
    async function() {}
  );

  let gotRequestCount = await fetch(
    "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
  ).then(response => response.json());

  // Retrieve the request referrer from the server
  let referrer_response = await fetch(
    "https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?action=get_request_referrer_results"
  ).then(response => response.text());

  Assert.ok(
    referrer_response === expected_results.referrer,
    "Request referrer matches expected - " + input.test_name
  );

  await Assert.deepEqual(
    gotRequestCount,
    { hinted: expected_results.hinted, normal: expected_results.normal },
    `${input.testName} (${input.resource_type}): Unexpected amount of requests made`
  );
}

add_task(async function test_103_referrer_policies() {
  let tests = [
    {
      input: {
        test_name: "image - no policies",
        resource_type: "image",
        header_referrer_policy: "",
        link_referrer_policy: "",
        hinted: true,
      },
      expected: {
        hinted: 1,
        normal: 0,
        referrer:
          "https://example.com/browser/netwerk/test/browser/early_hint_referrer_policy_html.sjs?as=image&hinted=1",
      },
    },
    {
      input: {
        test_name: "image - origin on header",
        resource_type: "image",
        header_referrer_policy: "origin",
        link_referrer_policy: "",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
    },
    {
      input: {
        test_name: "image - origin on link",
        resource_type: "image",
        header_referrer_policy: "",
        link_referrer_policy: "origin",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
    },
    {
      input: {
        test_name: "image - origin on both",
        resource_type: "image",
        header_referrer_policy: "origin",
        link_referrer_policy: "origin",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
    },
    {
      input: {
        test_name: "image - no-referrer on header",
        resource_type: "image",
        header_referrer_policy: "no-referrer",
        link_referrer_policy: "",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "" },
    },
    {
      input: {
        test_name: "image - no-referrer on link",
        resource_type: "image",
        header_referrer_policy: "",
        link_referrer_policy: "no-referrer",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "" },
    },
    {
      input: {
        test_name: "image - no-referrer on both",
        resource_type: "image",
        header_referrer_policy: "no-referrer",
        link_referrer_policy: "no-referrer",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "" },
    },
    {
      // link referrer policy takes precedence
      input: {
        test_name: "image - origin on header, no-referrer on link",
        resource_type: "image",
        header_referrer_policy: "origin",
        link_referrer_policy: "no-referrer",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "" },
    },
    {
      // link referrer policy takes precedence
      input: {
        test_name: "image - no-referrer on header, origin on link",
        resource_type: "image",
        header_referrer_policy: "no-referrer",
        link_referrer_policy: "origin",
        hinted: true,
      },
      expected: { hinted: 1, normal: 0, referrer: "https://example.com/" },
    },
  ];

  for (let test of tests) {
    await test_referrer_policy(test.input, test.expected);
  }
});