1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 Services.prefs.setBoolPref("network.early-hints.enabled", true);
11 test_hint_preload_internal,
12 request_count_checking,
13 } = ChromeUtils.importESModule(
14 "resource://testing-common/early_hint_preload_test_helper.sys.mjs"
17 // two early hint responses
18 add_task(async function test_103_two_preload_responses() {
19 await test_hint_preload_internal(
20 "103_two_preload_responses",
21 "https://example.com",
24 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
25 Services.uuid.generateUUID().toString(),
27 ["", "new_response"], // indicate new early hint response
29 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
30 Services.uuid.generateUUID().toString(),
33 { hinted: 1, normal: 1 }
37 // two link header in one early hint response
38 add_task(async function test_103_two_link_header() {
39 await test_hint_preload_internal(
40 "103_two_link_header",
41 "https://example.com",
44 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
45 Services.uuid.generateUUID().toString(),
47 ["", ""], // indicate new link header in same reponse
49 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
50 Services.uuid.generateUUID().toString(),
53 { hinted: 2, normal: 0 }
57 // two links in one early hint link header
58 add_task(async function test_103_two_links() {
59 await test_hint_preload_internal(
61 "https://example.com",
64 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
65 Services.uuid.generateUUID().toString(),
68 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
69 Services.uuid.generateUUID().toString(),
72 { hinted: 2, normal: 0 }
76 // two early hint responses, only second one has a link header
77 add_task(async function test_103_two_links() {
78 await test_hint_preload_internal(
80 "https://example.com",
82 ["", "non_link_header"], // indicate non-link related header
83 ["", "new_response"], // indicate new early hint response
85 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
86 Services.uuid.generateUUID().toString(),
89 { hinted: 1, normal: 0 }
93 // Preload twice same origin in secure context
94 add_task(async function test_103_preload_twice() {
95 // pass two times the same uuid so that on the second request, the response is
96 // already in the cache
97 let uuid = Services.uuid.generateUUID();
98 await test_hint_preload(
99 "test_103_preload_twice_1",
100 "https://example.com",
101 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
102 { hinted: 1, normal: 0 },
105 await test_hint_preload(
106 "test_103_preload_twice_2",
107 "https://example.com",
108 "https://example.com/browser/netwerk/test/browser/early_hint_pixel.sjs",
109 { hinted: 0, normal: 0 },
114 // Test that preloads in iframes don't get triggered
115 add_task(async function test_103_iframe() {
117 let headers = new Headers();
118 headers.append("X-Early-Hint-Count-Start", "");
120 "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
125 "https://example.com/browser/netwerk/test/browser/103_preload_iframe.html";
127 await BrowserTestUtils.withNewTab(
136 let gotRequestCount = await fetch(
137 "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
138 ).then(response => response.json());
139 let expectedRequestCount = { hinted: 0, normal: 1 };
141 await request_count_checking(
147 Services.cache2.clear();
150 // Test that anchors are parsed
151 add_task(async function test_103_anchor() {
153 let headers = new Headers();
154 headers.append("X-Early-Hint-Count-Start", "");
156 "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs",
161 "https://example.com/browser/netwerk/test/browser/103_preload_anchor.html";
163 await BrowserTestUtils.withNewTab(
172 let gotRequestCount = await fetch(
173 "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs"
174 ).then(response => response.json());
176 await request_count_checking("test_103_anchor", gotRequestCount, {