Make fetch API more standards compliant (#3667)

This commit is contained in:
serverhiccups 2020-02-04 03:54:47 +13:00 committed by GitHub
parent fba40d86c4
commit 2b0cf74a8f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 194 additions and 9 deletions

View file

@ -5,7 +5,8 @@ import {
assert,
assertEquals,
assertStrContains,
assertThrows
assertThrows,
fail
} from "./test_util.ts";
testPerm({ net: true }, async function fetchConnectionError(): Promise<void> {
@ -360,3 +361,75 @@ testPerm({ net: true }, async function fetchPostBodyTypedArray():Promise<void> {
assertEquals(actual, expected);
});
*/
testPerm({ net: true }, async function fetchWithManualRedirection(): Promise<
void
> {
const response = await fetch("http://localhost:4546/", {
redirect: "manual"
}); // will redirect to http://localhost:4545/
assertEquals(response.status, 0);
assertEquals(response.statusText, "");
assertEquals(response.url, "");
assertEquals(response.type, "opaqueredirect");
try {
await response.text();
fail(
"Reponse.text() didn't throw on a filtered response without a body (type opaqueredirect)"
);
} catch (e) {
return;
}
});
testPerm({ net: true }, async function fetchWithErrorRedirection(): Promise<
void
> {
const response = await fetch("http://localhost:4546/", {
redirect: "error"
}); // will redirect to http://localhost:4545/
assertEquals(response.status, 0);
assertEquals(response.statusText, "");
assertEquals(response.url, "");
assertEquals(response.type, "error");
try {
await response.text();
fail(
"Reponse.text() didn't throw on a filtered response without a body (type error)"
);
} catch (e) {
return;
}
});
test(function responseRedirect(): void {
const response = new Response(
"example.com/beforeredirect",
200,
"OK",
[["This-Should", "Disappear"]],
-1,
false,
null
);
const redir = response.redirect("example.com/newLocation", 301);
assertEquals(redir.status, 301);
assertEquals(redir.statusText, "");
assertEquals(redir.url, "");
assertEquals(redir.headers.get("Location"), "example.com/newLocation");
assertEquals(redir.type, "default");
});
test(function responseConstructionHeaderRemoval(): void {
const res = new Response(
"example.com",
200,
"OK",
[["Set-Cookie", "mysessionid"]],
-1,
false,
"basic",
null
);
assert(res.headers.get("Set-Cookie") != "mysessionid");
});