Skip to content

Truly independent web browser

Tests/LibWeb: Count external SVG fetches made by use elements

Andreas Kling28237c0f3d63
diff --git a/Tests/LibWeb/Fixtures/http-test-server.py b/Tests/LibWeb/Fixtures/http-test-server.py
index a63bedb9973..f0ad4715603 100755
--- a/Tests/LibWeb/Fixtures/http-test-server.py
+++ b/Tests/LibWeb/Fixtures/http-test-server.py
@@ -40,6 +40,8 @@ Endpoints:
server first sends a binary frame of that many bytes: a 1, zeros, and then a 2.
- GET /reflect-websocket-cookie with an "Upgrade: websocket" header: As above, but first sends the
handshake's Cookie header (or an empty string) as a text frame.
+ - GET /recorded-request-count/<echo path>: Responds with the number of requests the server has seen at <echo path>,
+ with any query ignored.
- GET /downloads/<name>: Reports the browser's download of <name> into the --downloads-directory: 404 while
there is none, 202 while it is in progress, and the downloaded file once it has finished. A <name> longer
than 188 bytes in UTF-8 gets a 400.
@@ -84,6 +86,10 @@ echo_store: Dict[str, Echo] = {}
# Headers from the most recent request at each echo path, queryable via GET /recorded-request-headers<echo-path>.
recorded_request_headers: Dict[str, Dict[str, list]] = {}
+# Number of requests seen at each echo path, query excluded, queryable via GET /recorded-request-count<echo-path>.
+recorded_request_counts: Dict[str, int] = {}
+recorded_request_counts_lock = threading.Lock()
+
# Named events used by tests that need deterministic delayed responses.
unblock_events: Dict[str, threading.Event] = {}
@@ -388,6 +394,8 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
self.handle_echo()
elif request_path.startswith("/recorded-request-headers/"):
self._serve_recorded_request_headers()
+ elif request_path.startswith("/recorded-request-count/"):
+ self._serve_recorded_request_count()
elif request_path.startswith("/downloads/"):
self._serve_download()
elif request_path.endswith(".py"):
@@ -534,6 +542,14 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
self.end_headers()
self.wfile.write(json.dumps(headers).encode("utf-8"))
+ def _serve_recorded_request_count(self):
+ echo_path = self.path.partition("?")[0][len("/recorded-request-count") :]
+ self.send_response(200)
+ self.send_header("Access-Control-Allow-Origin", "*")
+ self.send_header("Content-Type", "application/json")
+ self.end_headers()
+ self.wfile.write(json.dumps(recorded_request_counts.get(echo_path, 0)).encode("utf-8"))
+
def _serve_download(self):
name = urllib.parse.unquote(self.path.partition("?")[0][len("/downloads/") :])
directory = TestHTTPRequestHandler.downloads_directory
@@ -569,6 +585,8 @@ class TestHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
for header, value in self.headers.items():
headers_for_path.setdefault(header, []).append(value)
recorded_request_headers[self.path] = headers_for_path
+ with recorded_request_counts_lock:
+ recorded_request_counts[parsed_url.path] = recorded_request_counts.get(parsed_url.path, 0) + 1
if parsed_url.path != self.path:
recorded_request_headers[parsed_url.path] = recorded_request_headers[self.path]
diff --git a/Tests/LibWeb/Text/expected/SVG/use-element-external-fragments-share-one-fetch.txt b/Tests/LibWeb/Text/expected/SVG/use-element-external-fragments-share-one-fetch.txt
new file mode 100644
index 00000000000..7b302d3a972
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/SVG/use-element-external-fragments-share-one-fetch.txt
@@ -0,0 +1,5 @@
+Requests for the sprite: 3
+useFirst: first-rect
+useSecond: second-circle
+useThird: third-path
+useFirstAgain: first-rect
diff --git a/Tests/LibWeb/Text/input/SVG/use-element-external-fragments-share-one-fetch.html b/Tests/LibWeb/Text/input/SVG/use-element-external-fragments-share-one-fetch.html
new file mode 100644
index 00000000000..b3ffa9e481e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/SVG/use-element-external-fragments-share-one-fetch.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<body>
+<script>
+ const server = httpTestServer();
+ const spriteURL = server.echoURL("/use-element-external-fragments-sprite.svg");
+ const xhr = new XMLHttpRequest();
+ xhr.open("POST", `${server.baseURL}/echo`, false);
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.send(JSON.stringify({
+ method: "GET",
+ path: new URL(spriteURL).pathname,
+ status: 200,
+ headers: { "Content-Type": "image/svg+xml", "Cache-Control": "no-store" },
+ body: `<svg xmlns="http://www.w3.org/2000/svg">
+ <symbol id="first" viewBox="0 0 10 10"><rect id="first-rect" width="10" height="10"/></symbol>
+ <symbol id="second" viewBox="0 0 10 10"><circle id="second-circle" r="5"/></symbol>
+ <symbol id="third" viewBox="0 0 10 10"><path id="third-path" d="M0 0h10v10z"/></symbol>
+ </svg>`,
+ }));
+
+ document.write(`
+ <svg width="10" height="10"><use id="useFirst" href="${spriteURL}#first"/></svg>
+ <svg width="10" height="10"><use id="useSecond" href="${spriteURL}#second"/></svg>
+ <svg width="10" height="10"><use id="useThird" href="${spriteURL}#third"/></svg>
+ <svg width="10" height="10"><use id="useFirstAgain" href="${spriteURL}#first"/></svg>
+ `);
+
+ asyncTest(done => {
+ window.addEventListener("load", async () => {
+ const requestCount = await (await fetch(server.recordedRequestCountURL(spriteURL))).json();
+ println(`Requests for the sprite: ${requestCount}`);
+ for (const id of ["useFirst", "useSecond", "useThird", "useFirstAgain"]) {
+ const instance = internals.getShadowRoot(document.getElementById(id)).firstElementChild;
+ println(`${id}: ${instance?.firstElementChild?.id ?? "nothing"}`);
+ }
+ done();
+ });
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/include.js b/Tests/LibWeb/Text/input/include.js
index 79d95756819..be11acd6762 100644
--- a/Tests/LibWeb/Text/input/include.js
+++ b/Tests/LibWeb/Text/input/include.js
@@ -267,6 +267,9 @@ class HTTPTestServer {
recordedRequestHeadersURL(echoURL) {
return `${this.baseURL}/recorded-request-headers${new URL(echoURL).pathname}`;
}
+ recordedRequestCountURL(echoURL) {
+ return `${this.baseURL}/recorded-request-count${new URL(echoURL).pathname}`;
+ }
// Resolves once the echo server has seen a request for echoURL, query included.
async waitForRequest(echoURL) {
const url = new URL(echoURL);