Skip to content

Commit 778586c

Browse files
nhirokichromium-wpt-export-bot
authored andcommitted
Prefetch: Add WPTs for ruleset-based speculation rules tags
Bug: 404294138 Change-Id: Ice577c0efefd4f87bb0fe134ada380d410550cb6 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6365774 Reviewed-by: Huanpo Lin <[email protected]> Commit-Queue: Hiroki Nakagawa <[email protected]> Cr-Commit-Position: refs/heads/main@{#1440068}
1 parent bd675ea commit 778586c

File tree

6 files changed

+134
-3
lines changed

6 files changed

+134
-3
lines changed

speculation-rules/prefetch/resources/executor.sub.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,23 @@
22
<script src="/common/dispatcher/dispatcher.js" nonce="abc"></script>
33
<script src="utils.sub.js" nonce="abc"></script>
44
<script nonce="abc">
5+
6+
// Workaround for https://crbug.com/404573971.
7+
function reverse_html_escape(str) {
8+
str = str.replaceAll('&quot;', '"');
9+
return str;
10+
}
11+
512
window.requestHeaders = {
613
purpose: "{{header_or_default(Purpose, )}}",
714
sec_purpose: "{{header_or_default(Sec-Purpose, )}}",
815
referer: "{{header_or_default(Referer, )}}",
916
sec_fetch_dest: "{{header_or_default(Sec-Fetch-Dest, )}}",
1017
sec_fetch_mode: "{{header_or_default(Sec-Fetch-Mode, )}}",
18+
// Convert to the raw string to avoid backslashes from being processed as
19+
// escape sequences.
20+
sec_speculation_tags: reverse_html_escape(
21+
String.raw`{{header_or_default(Sec-Speculation-Tags, )}}`),
1122
};
1223

1324
const uuid = new URLSearchParams(location.search).get('uuid');
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Utilities for speculation tags tests.
2+
3+
function testRulesetTag(tag, expectedTag, description) {
4+
promise_test(async t => {
5+
const agent = await spawnWindow(t);
6+
const nextUrl = agent.getExecutorURL({ page: 2 });
7+
await agent.forceSpeculationRules({
8+
tag,
9+
prefetch: [{source: "list", urls: [nextUrl]}]
10+
});
11+
await agent.navigate(nextUrl);
12+
13+
const headers = await agent.getRequestHeaders();
14+
assert_prefetched(headers, "must be prefetched");
15+
assert_equals(headers.sec_speculation_tags, expectedTag, "Sec-Speculation-Tags");
16+
}, "Sec-Speculation-Tags: " + description);
17+
}

speculation-rules/prefetch/resources/utils.sub.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,16 @@ class PrefetchAgent extends RemoteContext {
4242
// occur despite heuristic matching, etc., and await the completion of the
4343
// prefetch.
4444
async forceSinglePrefetch(url, extra = {}, wait_for_completion = true) {
45-
await this.execute_script((url, extra) => {
46-
insertSpeculationRules({ prefetch: [{source: 'list', urls: [url], ...extra}] });
47-
}, [url, extra]);
45+
return this.forceSpeculationRules(
46+
{
47+
prefetch: [{source: 'list', urls: [url], ...extra}]
48+
}, wait_for_completion);
49+
}
50+
51+
async forceSpeculationRules(rules, wait_for_completion = true) {
52+
await this.execute_script((rules) => {
53+
insertSpeculationRules(rules);
54+
}, [rules]);
4855
if (!wait_for_completion) {
4956
return Promise.resolve();
5057
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<title>Prefetch request's Sec-Speculation-Tags request headers</title>
3+
<meta name="timeout" content="long">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="/common/dispatcher/dispatcher.js"></script>
7+
<script src="/common/utils.js"></script>
8+
<script src="../resources/utils.js"></script>
9+
<script src="resources/utils.sub.js"></script>
10+
<script src="resources/speculation-tags-utils.js"></script>
11+
<script>
12+
"use strict";
13+
14+
setup(() => assertSpeculationRulesIsSupported());
15+
16+
// https://wicg.github.io/nav-speculation/speculation-rules.html#collect-tags-for-matching-speculative-load-candidates
17+
// https://wicg.github.io/nav-speculation/prefetch.html#sec-speculation-tags-header
18+
19+
// Invalid (null) tag cases.
20+
testRulesetTag(123, 'null', "integer tag");
21+
testRulesetTag({a: "1"}, 'null', "object tag");
22+
testRulesetTag(null, 'null', "null value tag");
23+
testRulesetTag(undefined, 'null', "undefined value tag");
24+
testRulesetTag('\n', 'null', "non-printable character tag");
25+
testRulesetTag('my\nrules', 'null', "string with non-printable character tag");
26+
testRulesetTag('マイルール', 'null', "non-ascii string tag");
27+
28+
// Boundary checks: the tag allows only printable ascii characters between 0x20
29+
// and 0x7E, inclusive.
30+
testRulesetTag('\x19', 'null', "0x19 tag");
31+
testRulesetTag('\x20', '" "', "0x20 tag");
32+
testRulesetTag('\x7E', '"~"', "0x7E tag");
33+
testRulesetTag('\x7F', 'null', "0x7F tag");
34+
</script>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<title>Prefetch request's Sec-Speculation-Tags request headers</title>
3+
<meta name="timeout" content="long">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="/common/dispatcher/dispatcher.js"></script>
7+
<script src="/common/utils.js"></script>
8+
<script src="../resources/utils.js"></script>
9+
<script src="resources/utils.sub.js"></script>
10+
<script src="resources/speculation-tags-utils.js"></script>
11+
<script>
12+
"use strict";
13+
14+
setup(() => assertSpeculationRulesIsSupported());
15+
16+
// https://wicg.github.io/nav-speculation/speculation-rules.html#collect-tags-for-matching-speculative-load-candidates
17+
// https://wicg.github.io/nav-speculation/prefetch.html#sec-speculation-tags-header
18+
19+
// No tags case.
20+
promise_test(async t => {
21+
const agent = await spawnWindow(t);
22+
const nextUrl = agent.getExecutorURL({ page: 2 });
23+
await agent.forceSpeculationRules({
24+
prefetch: [{source: "list", urls: [nextUrl]}]
25+
});
26+
await agent.navigate(nextUrl);
27+
28+
const headers = await agent.getRequestHeaders();
29+
assert_prefetched(headers, "must be prefetched");
30+
assert_equals(headers.sec_speculation_tags, "null", "Sec-Speculation-Tags");
31+
}, "Sec-Speculation-Tags: no tags");
32+
</script>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<!DOCTYPE html>
2+
<title>Prefetch request's Sec-Speculation-Tags request headers</title>
3+
<meta name="timeout" content="long">
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<script src="/common/dispatcher/dispatcher.js"></script>
7+
<script src="/common/utils.js"></script>
8+
<script src="../resources/utils.js"></script>
9+
<script src="resources/utils.sub.js"></script>
10+
<script src="resources/speculation-tags-utils.js"></script>
11+
<script>
12+
"use strict";
13+
14+
setup(() => assertSpeculationRulesIsSupported());
15+
16+
// https://wicg.github.io/nav-speculation/speculation-rules.html#collect-tags-for-matching-speculative-load-candidates
17+
// https://wicg.github.io/nav-speculation/prefetch.html#sec-speculation-tags-header
18+
19+
const R = String.raw;
20+
21+
// Valid tag cases.
22+
testRulesetTag('my-rules', '"my-rules"', "string tag");
23+
testRulesetTag('null', '"null"', "'null' string tag");
24+
testRulesetTag('', '""', "empty string tag");
25+
testRulesetTag(' ', '" "', "single space tag");
26+
testRulesetTag('"', R`"\""`, "double quote tag");
27+
testRulesetTag('"""', R`"\"\"\""`, "double quotes tag");
28+
testRulesetTag('\\', R`"\\"`, "backslash tag");
29+
testRulesetTag('\\\\\\', R`"\\\\\\"`, "backslashes tag");
30+
</script>

0 commit comments

Comments
 (0)