-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add multiple tests for prefetch behavior #35707
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a686862
Add multiple tests for prefetch behavior
noamr 776db0a
Add tests for load/error events
noamr a82d865
Add cross-origin event tests
noamr 3fd2122
Test for proprietary headers
noamr 0b2ca17
Add test cases for cross-origin error events
noamr b5d5398
Update preload/prefetch-cache.html
noamr 15acb49
Fix CR comments
noamr d9c6b07
Clean up doc test
noamr bae2c62
Retries in cache test
noamr 68192f7
Use script instead of fetch to de-flake firefox
noamr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<!DOCTYPE html> | ||
<title>Ensures that prefetch respects HTTP cache semantics</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="resources/prefetch-helper.js"></script> | ||
<body> | ||
<script> | ||
|
||
async function prefetch_and_count(cacheControl, t) { | ||
const {href} = await prefetch({ | ||
"cache-control": cacheControl, | ||
"type": "application/javascript", | ||
content: "/**/"}, t); | ||
const script = document.createElement("script"); | ||
script.src = href; | ||
t.add_cleanup(() => script.remove()); | ||
const loaded = new Promise(resolve => script.addEventListener("load", resolve)); | ||
document.body.appendChild(script); | ||
await loaded; | ||
const info = await get_prefetch_info(href); | ||
return info.length; | ||
} | ||
|
||
promise_test(async t => { | ||
const result = await prefetch_and_count("max-age=604800", t); | ||
assert_equals(result, 1); | ||
}, "Prefetch should populate the HTTP cache"); | ||
|
||
for (const cacheControl of ["no-cache", "no-store", "max-age=0"]) { | ||
promise_test(async t => { | ||
const result = await prefetch_and_count(cacheControl, t); | ||
assert_equals(result, 2); | ||
}, `Prefetch should respect cache-control: ${cacheControl}`); | ||
} | ||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<!DOCTYPE html> | ||
<title>Ensures that prefetch works with documents</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="/common/get-host-info.sub.js"></script> | ||
<script src="/common/dispatcher/dispatcher.js"></script> | ||
<script src="resources/prefetch-helper.js"></script> | ||
<body> | ||
<script> | ||
|
||
const {ORIGIN, REMOTE_ORIGIN, HTTP_NOTSAMESITE_ORIGIN} = get_host_info(); | ||
const loaders = { | ||
image: { | ||
file: 'square.png', | ||
type: 'image/png', | ||
load: href => { | ||
const image = document.createElement('img'); | ||
image.src = href; | ||
document.body.appendChild(image); | ||
return new Promise(resolve => image.addEventListener('load', resolve)); | ||
} | ||
}, | ||
script: { | ||
file: 'dummy.js', | ||
type: 'application/javascript', | ||
load: href => { | ||
const script = document.createElement('script'); | ||
script.src = href; | ||
document.body.appendChild(script); | ||
return new Promise(resolve => script.addEventListener('load', resolve)); | ||
} | ||
}, | ||
style: { | ||
file: 'dummy.css', | ||
type: 'text/css', | ||
load: href => { | ||
const link = document.createElement('link'); | ||
link.href = href; | ||
link.rel = "stylesheet"; | ||
document.body.appendChild(link); | ||
return new Promise(resolve => link.addEventListener('load', resolve)); | ||
} | ||
}, | ||
document: { | ||
file: 'empty.html', | ||
type: 'text/html', | ||
load: href => { | ||
const iframe = document.createElement("iframe"); | ||
iframe.src = href; | ||
document.body.appendChild(iframe); | ||
return new Promise(resolve => iframe.addEventListener("load", resolve)); | ||
} | ||
} | ||
}; | ||
|
||
async function prefetch_document_and_count_fetches(options, t) { | ||
const {href, uid} = await prefetch({ | ||
file: "prefetch-exec.html", | ||
type: "text/html", | ||
corssOrigin: "anonymous", | ||
...options}); | ||
const popup = window.open(href); | ||
const remoteContext = new RemoteContext(uid); | ||
t.add_cleanup(() => popup.close()); | ||
const result = await remoteContext.execute_script(() => "OK"); | ||
assert_equals(result, "OK"); | ||
const requests = await get_prefetch_info(href); | ||
return requests.length; | ||
} | ||
|
||
promise_test(async t => { | ||
assert_equals(await prefetch_document_and_count_fetches({origin: ORIGIN}, t), 1); | ||
}, "same origin document prefetch without 'as' should be consumed"); | ||
|
||
promise_test(async t => { | ||
assert_equals(await prefetch_document_and_count_fetches({origin: REMOTE_ORIGIN}, t), 1); | ||
}, "same-site different-origin document prefetch without 'as' should be consumed"); | ||
|
||
promise_test(async t => { | ||
assert_equals(await prefetch_document_and_count_fetches({origin: HTTP_NOTSAMESITE_ORIGIN}, t), 2); | ||
}, "different-site document prefetch without 'as' should not be consumed"); | ||
|
||
promise_test(async t => { | ||
assert_equals(await prefetch_document_and_count_fetches({origin: HTTP_NOTSAMESITE_ORIGIN, as: "document"}, t), 2); | ||
}, "different-site document prefetch with 'as=document' should not be consumed"); | ||
|
||
promise_test(async t => { | ||
const {href, uid} = await prefetch({ | ||
file: "prefetch-exec.html", | ||
type: "text/html", | ||
corssOrigin: "anonymous", | ||
origin: ORIGIN}); | ||
const popup = window.open(href + "&cache_bust=" + token()); | ||
const remoteContext = new RemoteContext(uid); | ||
t.add_cleanup(() => popup.close()); | ||
await remoteContext.execute_script(() => "OK"); | ||
const results = await get_prefetch_info(href); | ||
assert_equals(results.length, 2); | ||
assert_equals(results[0].headers.accept, results[1].headers.accept); | ||
}, "Document prefetch should send the exact Accept header as navigation") | ||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
<!DOCTYPE html> | ||
<title>Ensures that prefetch respects HTTP cache semantics</title> | ||
<meta name="timeout" content="long"> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="resources/prefetch-helper.js"></script> | ||
<script src="/common/get-host-info.sub.js"></script> | ||
<body> | ||
<script> | ||
const {REMOTE_ORIGIN} = get_host_info(); | ||
async function prefetch(link, uid, t) { | ||
link.rel = "prefetch"; | ||
document.head.appendChild(link); | ||
const event = new Promise(resolve => { | ||
link.addEventListener("error", () => resolve("error")); | ||
link.addEventListener("load", () => resolve("load")); | ||
t.step_timeout(() => resolve("timeout"), 1000); | ||
}); | ||
return await event; | ||
} | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.href = `/preload/resources/prefetch-info.py?key=${uid}`; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "load"); | ||
}, "Prefetch should fire the load event"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}`; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "load"); | ||
}, "Cross-origin prefetch should fire the load event"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.href = `/preload/resources/prefetch-info.py?key=${uid}&status=404`; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "load"); | ||
}, "Prefetch should fire the load event for 404"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}&status=404`; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "load"); | ||
}, "Prefetch should fire the load event for 404 (cross-origin)"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.href = `/preload/resources/prefetch-info.py?key=${uid}&status=500`; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "load"); | ||
}, "Prefetch should fire the load event for 500"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}&status=500`; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "load"); | ||
}, "Cross-origin prefetch should fire the load event for 500"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.crossOrigin = "anonymous"; | ||
link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}&cors=false`; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "error"); | ||
}, "Prefetch should fire the error event for network errors"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.crossOrigin = "anonymous"; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "timeout"); | ||
}, "Prefetch should do nothing with an empty href"); | ||
|
||
promise_test(async t => { | ||
const uid = token(); | ||
const link = document.createElement("link"); | ||
link.href = "https://example.com\u0000mozilla.org"; | ||
link.crossOrigin = "anonymous"; | ||
const event = await prefetch(link, uid, t); | ||
assert_equals(event, "timeout"); | ||
}, "Prefetch should do nothing with an invalid href"); | ||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<!DOCTYPE html> | ||
<title>Ensures that prefetch sends headers as per-spec</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="resources/prefetch-helper.js"></script> | ||
<body> | ||
<script> | ||
|
||
promise_test(async t => { | ||
const {href} = await prefetch({"type": "image/png", file: "../../images/green.png"}, t); | ||
const [info] = await get_prefetch_info(href); | ||
const {headers} = info; | ||
assert_equals(headers["sec-fetch-dest"], "empty"); | ||
domenic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_equals(headers["sec-purpose"], "prefetch"); | ||
assert_false("origin" in headers); | ||
}, "Prefetch should include Sec-Purpose=prefetch and Sec-Fetch-Dest=empty headers"); | ||
domenic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
promise_test(async t => { | ||
const {href} = await prefetch({"type": "image/png", file: "../../images/green.png"}, t); | ||
const [info] = await get_prefetch_info(href); | ||
const {headers} = info; | ||
assert_false("purpose" in headers); | ||
assert_false("x-moz" in headers); | ||
}, "Prefetch should not include proprietary headers (X-moz/Purpose)"); | ||
|
||
promise_test(async t => { | ||
const {href} = await prefetch({"type": "image/png", file: "../../images/green.png", crossOrigin: "anonymous"}, t); | ||
const [info] = await get_prefetch_info(href); | ||
const {headers} = info; | ||
assert_equals(headers["origin"], document.origin); | ||
}, "Prefetch should respect CORS mode"); | ||
|
||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<link rel="prefetch" href="/xhr/resources/delay.py?ms=100000"> | ||
<body> | ||
<script> | ||
|
||
promise_test(() => new Promise(resolve => window.addEventListener("load", resolve)), | ||
"Prefetch should not block the load event"); | ||
|
||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<!DOCTYPE html> | ||
<title>Ensures that prefetch is not specific to resource types</title> | ||
<script src="/resources/testharness.js"></script> | ||
<script src="/resources/testharnessreport.js"></script> | ||
<script src="/common/utils.js"></script> | ||
<script src="/common/get-host-info.sub.js"></script> | ||
<script src="resources/prefetch-helper.js"></script> | ||
<body> | ||
<script> | ||
const host_info = get_host_info(); | ||
const loaders = { | ||
"": { | ||
file: "../../common/dummy.xml", | ||
type: "text/xml", | ||
load: fetch | ||
}, | ||
image: { | ||
file: '../../images/green.png', | ||
type: 'image/png', | ||
load: href => { | ||
const image = document.createElement('img'); | ||
image.src = href; | ||
document.body.appendChild(image); | ||
return new Promise(resolve => image.addEventListener('load', resolve)); | ||
} | ||
}, | ||
script: { | ||
file: 'dummy.js', | ||
type: 'application/javascript', | ||
load: href => { | ||
const script = document.createElement('script'); | ||
script.src = href; | ||
document.body.appendChild(script); | ||
return new Promise(resolve => script.addEventListener('load', resolve)); | ||
} | ||
}, | ||
style: { | ||
file: 'dummy.css', | ||
type: 'text/css', | ||
load: href => { | ||
const link = document.createElement('link'); | ||
link.href = href; | ||
link.rel = "stylesheet"; | ||
document.body.appendChild(link); | ||
return new Promise(resolve => link.addEventListener('load', resolve)); | ||
} | ||
}, | ||
document: { | ||
file: 'empty.html', | ||
type: 'text/html', | ||
load: href => { | ||
const iframe = document.createElement("iframe"); | ||
iframe.src = href; | ||
document.body.appendChild(iframe); | ||
return new Promise(resolve => iframe.addEventListener("load", resolve)); | ||
} | ||
} | ||
}; | ||
|
||
for (const as in loaders) { | ||
for (const consumer in loaders) { | ||
const {file, type, load} = loaders[as] | ||
promise_test(async t => { | ||
const {href} = await prefetch({file, type, as, origin: host_info[origin]}); | ||
const requests = await get_prefetch_info(href); | ||
assert_equals(requests.length, 1); | ||
domenic marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_equals(requests[0].headers["sec-fetch-dest"], "empty"); | ||
}, `Prefetch as=${as} should work when consumed as ${consumer} (${origin})`); | ||
} | ||
} | ||
|
||
</script> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if all these files should go in https://github.com/web-platform-tests/wpt/tree/master/html/semantics/links/linktypes instead? Not sure, and I guess it's OK either way...