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.
This commit is contained in:
Cory Virok 2024-10-14 22:25:24 -07:00 committed by Andrew Kaster
parent 3fdb2b081f
commit 39f844f77c
Notes: github-actions[bot] 2024-11-09 20:30:53 +00:00
4 changed files with 59 additions and 0 deletions

View file

@ -408,6 +408,7 @@ __finishTest
__preventMultipleTestFunctions
animationFrame
asyncTest
httpTestServer
printElement
println
promiseTest

View file

@ -0,0 +1 @@
PASS

View file

@ -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;
}

View file

@ -0,0 +1,29 @@
<script src="./include.js"></script>
<script>
asyncTest(async (done) => {
try {
const httpServer = httpTestServer();
const url = await httpServer.createEcho("GET", "/test-http-test-server", {
status: 200,
headers: {
"Access-Control-Allow-Origin": "*", // necessary when running test using file://...
"Access-Control-Expose-Headers": "X-Custom-Header", // tells the browser which headers to expose to JS
"Content-Type": "text/plain",
"X-Custom-Header": "well hello friends",
},
body: "hello world!",
});
const result = await fetch(url);
const headers = result.headers;
if (headers.get("X-Custom-Header") === "well hello friends") {
println("PASS");
} else {
println("FAIL");
}
} catch (err) {
println("FAIL - " + err);
}
done();
});
</script>