Skip to content

Commit 6fd191d

Browse files
bashimoz-wptsync-bot
authored andcommitted
Bug 1761400 [wpt PR 33365] - Add WPTs for different Early Hints preload finish timings, a=testonly
Automatic update from web-platform-tests Add WPTs for different Early Hints preload finish timings The test scenarios are: * 103 -> start preload -> preload finished -> 200 -> response body. * 103 -> start preload -> (preload inflight) -> 200 -> preload finished -> response body. The purpose of these tests is to make sure the resource is added to the preload map of the document [1]. Also refer to [2] for the motivation. [1] whatwg/html#7675 [2] web-platform-tests/wpt#33076 (comment) Bug: 1305896 Change-Id: Id0ba4748f6493bc2c78be6de5769b4d16ce9092f Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3552176 Reviewed-by: Yutaka Hirano <[email protected]> Commit-Queue: Kenichi Ishibashi <[email protected]> Cr-Commit-Position: refs/heads/main@{#985847} -- wpt-commits: f88ac14ebaf9206cc08af481c2332b8dbef11a59 wpt-pr: 33365
1 parent 02ada5e commit 6fd191d

9 files changed

+137
-10
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// META: script=/common/utils.js
2+
// META: script=resources/early-hints-helpers.sub.js
3+
4+
test(() => {
5+
const params = new URLSearchParams();
6+
const id = token();
7+
params.set("resource-url", SAME_ORIGIN_RESOURCES_URL + "/fetch-and-record-js.h2.py?id=" + id);
8+
params.set("resource-id", id);
9+
const test_url = "resources/preload-finished-before-final-response.h2.py?" + params.toString();
10+
window.location.replace(new URL(test_url, window.location));
11+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// META: script=/common/utils.js
2+
// META: script=resources/early-hints-helpers.sub.js
3+
4+
test(() => {
5+
const params = new URLSearchParams();
6+
const id = token();
7+
params.set("resource-url", SAME_ORIGIN_RESOURCES_URL + "/fetch-and-record-js.h2.py?id=" + id);
8+
params.set("resource-id", id);
9+
const test_url = "resources/preload-finished-while-receiving-final-response-body.h2.py?" + params.toString();
10+
window.location.replace(new URL(test_url, window.location));
11+
});

testing/web-platform/tests/loading/early-hints/resources/delayed-js.h2.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
import time
1+
import importlib
2+
3+
utils = importlib.import_module("loading.early-hints.resources.utils")
24

35

46
def main(request, response):
57
id = request.GET.first(b"id")
6-
url_dir = u'/'.join(request.url_parts.path.split(u'/')[:-1]) + u'/'
78
# Wait until the id is set via resume-delayed-js.h2.py.
8-
while True:
9-
if request.server.stash.take(id, url_dir):
10-
break
11-
time.sleep(0.1)
9+
utils.wait_for_preload_to_finish(request, id)
1210

1311
headers = [
1412
("Content-Type", "text/javascript"),

testing/web-platform/tests/loading/early-hints/resources/fetch-and-record-js.h2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import importlib
2+
import time
23

34
utils = importlib.import_module("loading.early-hints.resources.utils")
45

@@ -10,4 +11,6 @@ def main(request, response):
1011
("Cache-Control", "max-age=600"),
1112
]
1213
body = "/*empty script*/"
14+
# Sleep to simulate loading time.
15+
time.sleep(0.05)
1316
return (200, "OK"), headers, body
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import importlib
2+
import os
3+
4+
utils = importlib.import_module("loading.early-hints.resources.utils")
5+
6+
7+
def handle_headers(frame, request, response):
8+
resource_url = request.GET.first(b"resource-url").decode()
9+
link_header_value = "<{}>; rel=preload; as=script".format(resource_url)
10+
early_hints = [
11+
(b":status", b"103"),
12+
(b"link", link_header_value),
13+
]
14+
response.writer.write_raw_header_frame(headers=early_hints,
15+
end_headers=True)
16+
17+
# Wait for preload to finish before sending the final response headers.
18+
resource_id = request.GET.first(b"resource-id").decode()
19+
utils.wait_for_preload_to_finish(request, resource_id)
20+
21+
response.status = 200
22+
response.headers[b"content-type"] = "text/html"
23+
response.write_status_headers()
24+
25+
26+
def main(request, response):
27+
current_dir = os.path.dirname(os.path.realpath(__file__))
28+
file_path = os.path.join(current_dir, "preload-finished-before-final-response.html")
29+
with open(file_path, "r") as f:
30+
test_content = f.read()
31+
response.writer.write_data(item=test_content, last=True)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<meta charset=utf-8>
3+
<script src="/resources/testharness.js"></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<script src="early-hints-helpers.sub.js"></script>
6+
<body>
7+
<script>
8+
promise_test(async (t) => {
9+
const params = new URLSearchParams(window.location.search);
10+
const resource_url = params.get("resource-url");
11+
await fetchScript(resource_url);
12+
assert_true(isPreloadedByEarlyHints(resource_url));
13+
}, "Early hints preload finished before the final response.");
14+
</script>
15+
</body>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import importlib
2+
import os
3+
4+
utils = importlib.import_module("loading.early-hints.resources.utils")
5+
6+
7+
def handle_headers(frame, request, response):
8+
resource_url = request.GET.first(b"resource-url").decode()
9+
link_header_value = "<{}>; rel=preload; as=script".format(resource_url)
10+
early_hints = [
11+
(b":status", b"103"),
12+
(b"link", link_header_value),
13+
]
14+
response.writer.write_raw_header_frame(headers=early_hints,
15+
end_headers=True)
16+
17+
response.status = 200
18+
response.headers[b"content-type"] = "text/html"
19+
response.write_status_headers()
20+
21+
22+
def main(request, response):
23+
# Wait for preload to finish before sending the response body.
24+
resource_id = request.GET.first(b"resource-id").decode()
25+
utils.wait_for_preload_to_finish(request, resource_id)
26+
27+
current_dir = os.path.dirname(os.path.realpath(__file__))
28+
file_path = os.path.join(current_dir, "preload-finished-while-receiving-final-response-body.html")
29+
with open(file_path, "r") as f:
30+
test_content = f.read()
31+
response.writer.write_data(item=test_content, last=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<meta charset=utf-8>
3+
<script src="/resources/testharness.js"></script>
4+
<script src="/resources/testharnessreport.js"></script>
5+
<script src="early-hints-helpers.sub.js"></script>
6+
<body>
7+
<script>
8+
promise_test(async (t) => {
9+
const params = new URLSearchParams(window.location.search);
10+
const resource_url = params.get("resource-url");
11+
await fetchScript(resource_url);
12+
assert_true(isPreloadedByEarlyHints(resource_url));
13+
}, "Early hints preload finished while loading the final response body.");
14+
</script>
15+
</body>

testing/web-platform/tests/loading/early-hints/resources/utils.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import datetime
22
import json
3+
import time
34

45

56
def _url_dir(request):
@@ -22,12 +23,23 @@ def store_request_timing_and_headers(request):
2223
request.server.stash.put(id, value, url_dir)
2324

2425

25-
def get_request_timing_and_headers(request):
26+
def get_request_timing_and_headers(request, id=None):
2627
"""Get previously stored timestamp and request headers associated with the
27-
given request. The request must be a GET request and must have the "id"
28-
parameter.
28+
given "id". When "id" is not given the id is retrieved from "request".
2929
"""
30-
id = request.GET.first(b"id")
30+
if id is None:
31+
id = request.GET.first(b"id")
3132
url_dir = _url_dir(request)
3233
item = request.server.stash.take(id, url_dir)
34+
if not item:
35+
return None
3336
return json.dumps(item)
37+
38+
39+
def wait_for_preload_to_finish(request, id):
40+
"""Wait until a preload associated with "id" is sent."""
41+
while True:
42+
if get_request_timing_and_headers(request, id):
43+
break
44+
time.sleep(0.1)
45+
time.sleep(0.1)

0 commit comments

Comments
 (0)