Tests: Add test for credentials flag in fetch

This commit is contained in:
Julian Dominguez-Schatz 2025-08-24 20:39:42 -04:00 committed by Tim Ledbetter
parent 36bfee7f1b
commit 193163be2f
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1 @@
PASS

View File

@ -0,0 +1,49 @@
<!DOCTYPE html>
<script src="../include.js"></script>
<script>
const COOKIE_NAME = "testcookie";
async function hasCookie(resp) {
const body = await resp.json();
return body["Cookie"]?.find(
(cookie) => cookie.trim().startsWith(COOKIE_NAME + '=')
);
}
promiseTest(async () => {
try {
const httpServer = httpTestServer();
const url = await httpServer.createEcho("GET", `/fetch-cors-credentials-flag-cookie`, {
status: 200,
headers: {
"Access-Control-Allow-Origin": location.origin,
"Access-Control-Allow-Credentials": "true",
"Set-Cookie": `${COOKIE_NAME}=COOKIE`
},
reflect_headers_in_body: true,
});
let resp = await fetch(url, { credentials: 'omit' });
if (await hasCookie(resp)) {
println("FAIL - cookie was set");
return;
}
resp = await fetch(url, { credentials: 'include' });
if (await hasCookie(resp)) {
println("FAIL - cookie was set");
return;
}
resp = await fetch(url, { credentials: 'include' });
if (!await hasCookie(resp)) {
println("FAIL - cookie was not set");
return;
}
println("PASS");
} catch (err) {
println("FAIL - " + err);
}
});
</script>