From 39f844f77c719b5c7789d644aac6ea44c87b9625 Mon Sep 17 00:00:00 2001 From: Cory Virok Date: Mon, 14 Oct 2024 22:25:24 -0700 Subject: [PATCH] LibWeb/Tests: Added example test for how to use http-test-server.py The example shows how to write a test that depends on custom HTTP headers in the response. This will be useful for testing browser JS that depends on how Ladybird processes response headers, eg CORS headers like Access-Control-Allow-Origin and others. --- .../Text/expected/all-window-properties.txt | 1 + .../Text/expected/test-http-test-server.txt | 1 + Tests/LibWeb/Text/input/include.js | 28 ++++++++++++++++++ .../Text/input/test-http-test-server.html | 29 +++++++++++++++++++ 4 files changed, 59 insertions(+) create mode 100644 Tests/LibWeb/Text/expected/test-http-test-server.txt create mode 100644 Tests/LibWeb/Text/input/test-http-test-server.html diff --git a/Tests/LibWeb/Text/expected/all-window-properties.txt b/Tests/LibWeb/Text/expected/all-window-properties.txt index dfe342a7a54..92fc025af76 100644 --- a/Tests/LibWeb/Text/expected/all-window-properties.txt +++ b/Tests/LibWeb/Text/expected/all-window-properties.txt @@ -408,6 +408,7 @@ __finishTest __preventMultipleTestFunctions animationFrame asyncTest +httpTestServer printElement println promiseTest diff --git a/Tests/LibWeb/Text/expected/test-http-test-server.txt b/Tests/LibWeb/Text/expected/test-http-test-server.txt new file mode 100644 index 00000000000..7ef22e9a431 --- /dev/null +++ b/Tests/LibWeb/Text/expected/test-http-test-server.txt @@ -0,0 +1 @@ +PASS diff --git a/Tests/LibWeb/Text/input/include.js b/Tests/LibWeb/Text/input/include.js index 7ac4f1d0923..2b643c7649d 100644 --- a/Tests/LibWeb/Text/input/include.js +++ b/Tests/LibWeb/Text/input/include.js @@ -98,3 +98,31 @@ function promiseTest(f) { }); }); } + +class HTTPTestServer { + constructor(baseURL) { + this.baseURL = baseURL; + } + async createEcho(method, path, options) { + const result = await fetch(`${this.baseURL}/create`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ ...options, method, path }), + }); + if (!result.ok) { + throw new Error("Error creating echo: " + result.statusText); + } + return `${this.baseURL}${path}`; + } + getStaticURL(path) { + return `${this.baseURL}/static/${path}`; + } +} + +// FIXME: Get the port from internals +const __httpTestServer = new HTTPTestServer("http://localhost:8123"); +function httpTestServer() { + return __httpTestServer; +} diff --git a/Tests/LibWeb/Text/input/test-http-test-server.html b/Tests/LibWeb/Text/input/test-http-test-server.html new file mode 100644 index 00000000000..46a213b0d7d --- /dev/null +++ b/Tests/LibWeb/Text/input/test-http-test-server.html @@ -0,0 +1,29 @@ + +