Skip to content

Commit 071c7cc

Browse files
committed
Clean up doc test
1 parent a8a899d commit 071c7cc

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

preload/prefetch-document.html

+27-27
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,6 @@
1010
<script>
1111

1212
const {ORIGIN, REMOTE_ORIGIN, HTTP_NOTSAMESITE_ORIGIN} = get_host_info();
13-
const origins = {
14-
"same origin": ORIGIN,
15-
"same site": REMOTE_ORIGIN,
16-
"different site": HTTP_NOTSAMESITE_ORIGIN
17-
}
1813
const loaders = {
1914
image: {
2015
file: '../../images/green.png',
@@ -59,30 +54,36 @@
5954
}
6055
};
6156

62-
async function test_prefetch_document({origin, as}, expected) {
63-
promise_test(async t => {
64-
const {href, uid} = await prefetch({
65-
file: "prefetch-exec.html",
66-
type: "text/html",
67-
corssOrigin: "anonymous",
68-
origin: origins[origin],
69-
as});
57+
async function prefetch_document_and_count_fetches(options, t) {
58+
const {href, uid} = await prefetch({
59+
file: "prefetch-exec.html",
60+
type: "text/html",
61+
corssOrigin: "anonymous",
62+
...options});
63+
const popup = window.open(href);
64+
const remoteContext = new RemoteContext(uid);
65+
t.add_cleanup(() => popup.close());
66+
const result = await remoteContext.execute_script(() => "OK");
67+
assert_equals(result, "OK");
68+
const requests = await get_prefetch_info(href);
69+
return requests.length;
70+
}
7071

71-
const popup = window.open(href);
72-
const remoteContext = new RemoteContext(uid);
73-
t.add_cleanup(() => popup.close());
74-
const result = await remoteContext.execute_script(() => "OK");
75-
assert_equals(result, "OK");
72+
promise_test(async t => {
73+
assert_equals(await prefetch_document_and_count_fetches({origin: ORIGIN}, t), 1);
74+
}, "same origin document prefetch without 'as' should be consumed");
7675

77-
const requests = await get_prefetch_info(href);
78-
const did_consume = requests.length === 1 ? "consumed" : "not consumed";
79-
assert_equals(did_consume, expected);
80-
}, `Prefetching a document (${origin}, as="${as}") should be ${expected}`);
81-
}
76+
promise_test(async t => {
77+
assert_equals(await prefetch_document_and_count_fetches({origin: REMOTE_ORIGIN}, t), 1);
78+
}, "same-site different-origin document prefetch without 'as' should be consumed");
8279

83-
test_prefetch_document({origin: "same origin"}, "consumed");
84-
test_prefetch_document({origin: "same site"}, "consumed");
85-
test_prefetch_document({as: "document", origin: "different site"}, "not consumed");
80+
promise_test(async t => {
81+
assert_equals(await prefetch_document_and_count_fetches({origin: HTTP_NOTSAMESITE_ORIGIN}, t), 2);
82+
}, "different-site document prefetch without 'as' should not be consumed");
83+
84+
promise_test(async t => {
85+
assert_equals(await prefetch_document_and_count_fetches({origin: HTTP_NOTSAMESITE_ORIGIN, as: "document"}, t), 2);
86+
}, "different-site document prefetch without 'as' should not be consumed");
8687

8788
promise_test(async t => {
8889
const {href, uid} = await prefetch({
@@ -98,6 +99,5 @@
9899
assert_equals(results.length, 2);
99100
assert_equals(results[0].headers.accept, results[1].headers.accept);
100101
}, "Document prefetch should send the exact Accept header as navigation")
101-
102102
</script>
103103
</body>

preload/resources/prefetch-helper.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ async function get_prefetch_info(href) {
66
async function prefetch(p = {}, t) {
77
const link = document.createElement("link");
88
link.rel = "prefetch";
9-
if (p.as !== undefined)
10-
link.as = p.as;
9+
link.as = p.as;
1110
if (p.crossOrigin)
1211
link.setAttribute("crossorigin", p.crossOrigin);
1312
const uid = token();

0 commit comments

Comments
 (0)