mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
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:
parent
3fdb2b081f
commit
39f844f77c
Notes:
github-actions[bot]
2024-11-09 20:30:53 +00:00
Author: https://github.com/coryvirok Commit: https://github.com/LadybirdBrowser/ladybird/commit/39f844f77c7 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/1801 Reviewed-by: https://github.com/rmg-x
4 changed files with 59 additions and 0 deletions
|
@ -408,6 +408,7 @@ __finishTest
|
|||
__preventMultipleTestFunctions
|
||||
animationFrame
|
||||
asyncTest
|
||||
httpTestServer
|
||||
printElement
|
||||
println
|
||||
promiseTest
|
||||
|
|
1
Tests/LibWeb/Text/expected/test-http-test-server.txt
Normal file
1
Tests/LibWeb/Text/expected/test-http-test-server.txt
Normal file
|
@ -0,0 +1 @@
|
|||
PASS
|
|
@ -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;
|
||||
}
|
||||
|
|
29
Tests/LibWeb/Text/input/test-http-test-server.html
Normal file
29
Tests/LibWeb/Text/input/test-http-test-server.html
Normal 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>
|
Loading…
Reference in a new issue