Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@
return type;
}

function assertHeaders(headers, expectedTag, preloadingType) {
if (expectedTag === undefined) {
// If `tag` is invalid, preloading should not be
// triggered, and the navigation should fall back to network. Confirm
// this behavior by checking the request headers.
assert_false(headers.has("sec-purpose"));
assert_false(headers.has("sec-speculation-tags"));
} else {
// Make sure the page is preloaded.
assert_equals(
headers.get("sec-purpose"),
preloadingType === "prefetch" ? "prefetch" : "prefetch;prerender");
assert_equals(headers.get("sec-speculation-tags"), expectedTag);
}
}

function testRulesetTag(tag, expectedTag, description) {
promise_test(async t => {
const rcHelper = new RemoteContextHelper();
Expand All @@ -35,7 +51,7 @@
const preloadingType = getPreloadingType();
const preloadedRC = await referrerRC.helper.createContext({
executorCreator(url) {
return referrerRC.executeScript((preloadingType, tag, url) => {
return referrerRC.executeScript((preloadingType, tag, url, expectedTag) => {
const script = document.createElement("script");
script.type = "speculationrules";
script.textContent = JSON.stringify({
Expand All @@ -47,20 +63,24 @@
}
]
});

if (expectedTag === undefined) {
return new Promise(resolve => {
script.addEventListener('error', resolve, { once: true });
document.head.append(script);
});
}

document.head.append(script);
}, [preloadingType, tag, url]);
}, [preloadingType, tag, url, expectedTag]);
}, extraConfig
});

// Navigate to the preloaded page.
referrerRC.navigateTo(preloadedRC.url);

const headers = await preloadedRC.getRequestHeaders();
// Make sure the page is preloaded.
assert_equals(
headers.get("sec-purpose"),
preloadingType === "prefetch" ? "prefetch" : "prefetch;prerender");
assert_equals(headers.get("sec-speculation-tags"), expectedTag);
assertHeaders(headers, expectedTag, preloadingType);
}, "Sec-Speculation-Tags [ruleset-based]: " + description);
}

Expand Down Expand Up @@ -94,21 +114,7 @@
referrerRC.navigateTo(preloadedRC.url);

const headers = await preloadedRC.getRequestHeaders();

if (expectedTag === undefined) {
// If `tag` on the rule level is invalid, preloading should not be
// triggered, and the navigation should fall back to network. Confirm
// this behavior by checking the request headers.
assert_false(headers.has("sec-purpose"));
assert_false(headers.has("sec-speculation-tags"));
} else {
// Make sure the page is preloaded.
assert_equals(
headers.get("sec-purpose"),
preloadingType === "prefetch" ? "prefetch" : "prefetch;prerender");
assert_equals(headers.get("sec-speculation-tags"), expectedTag);
}

assertHeaders(headers, expectedTag, preloadingType);
}, "Sec-Speculation-Tags [rule-based]: " + description);
}

Expand All @@ -124,7 +130,7 @@
// Runs the test function for invalid tag cases based on the tag level.
globalThis.testInvalidTag = (tag, description) => {
if (getTagLevel() === 'ruleset') {
testRulesetTag(tag, 'null', description);
testRulesetTag(tag, undefined, description);
} else {
// Pass `undefined` to indicate this preloading is expected to fail.
testRuleTag(tag, undefined, description);
Expand Down