46 lines
2.2 KiB
TypeScript
46 lines
2.2 KiB
TypeScript
import { expect, test } from "bun:test";
|
|
import { TransportError } from "../src/core/transport";
|
|
import { pageTransport } from "../src/web/transport";
|
|
|
|
const ORIGIN = "https://mbseed.example.org";
|
|
function raw(respond: (url: string) => Response = () => new Response("ok")) {
|
|
const seen: { url: string; init?: RequestInit }[] = [];
|
|
const fn = async (url: string, init?: RequestInit) => {
|
|
seen.push({ url, init });
|
|
return respond(url);
|
|
};
|
|
return { fn, seen };
|
|
}
|
|
|
|
test("MangaBaka, litterbox and our own server directly; everything else through /api/fetch", async () => {
|
|
const r = raw();
|
|
const t = pageTransport(ORIGIN, r.fn);
|
|
await t.fetch("https://api.mangabaka.org/v1/series/1", { headers: { "x-api-key": "mb-x" } });
|
|
await t.fetch("https://litterbox.catbox.moe/resources/internals/api.php", { method: "POST", body: "x" });
|
|
await t.fetch(`${ORIGIN}/api/config`);
|
|
await t.fetch("https://www.webtoons.com/-/-/-/list?title_no=95");
|
|
expect(r.seen.map((s) => s.url)).toEqual([
|
|
"https://api.mangabaka.org/v1/series/1",
|
|
"https://litterbox.catbox.moe/resources/internals/api.php",
|
|
`${ORIGIN}/api/config`,
|
|
`${ORIGIN}/api/fetch?url=${encodeURIComponent("https://www.webtoons.com/-/-/-/list?title_no=95")}`,
|
|
]);
|
|
expect(new Headers(r.seen[3]!.init?.headers).get("x-api-key")).toBeNull();
|
|
});
|
|
|
|
test("a POST to a host without CORS is refused in the page", async () => {
|
|
const t = pageTransport(ORIGIN, raw().fn);
|
|
await expect(t.fetch("https://catbox.moe/user/api.php", { method: "POST", body: "x" })).rejects.toBeInstanceOf(TransportError);
|
|
});
|
|
|
|
test("a proxy error becomes a TransportError with the server's message", async () => {
|
|
const t = pageTransport(ORIGIN, raw(() =>
|
|
Response.json({ error: "Host not allowed: evil.example" }, { status: 403, headers: { "x-mbseed-error": "1" } })).fn);
|
|
await expect(t.fetch("https://www.webtoons.com/x")).rejects.toThrow(/Host not allowed/);
|
|
});
|
|
|
|
test("an upstream status (e.g. Wayback 429) passes through as a response", async () => {
|
|
const t = pageTransport(ORIGIN, raw(() => new Response("slow down", { status: 429 })).fn);
|
|
expect((await t.fetch("https://web.archive.org/web/1id_/x")).status).toBe(429);
|
|
});
|