@fedify/fedify
Version:
An ActivityPub server framework
108 lines (107 loc) • 4.11 kB
JavaScript
import { Temporal } from "@js-temporal/polyfill";
import "urlpattern-polyfill";
globalThis.addEventListener = () => {};
import { t as esm_default } from "../esm-DVILvP5e.mjs";
import { t as assertEquals } from "../assert_equals-Ew3jOFa3.mjs";
import "../std__assert-CRDpx_HF.mjs";
import { t as assertRejects } from "../assert_rejects-B-qJtC9Z.mjs";
import { l as verifyRequest } from "../http-C_edJspG.mjs";
import { i as rsaPrivateKey2 } from "../keys-DGu1NFwu.mjs";
import { t as getAuthenticatedDocumentLoader } from "../docloader-Da15YRxG.mjs";
import { mockDocumentLoader, test } from "@fedify/fixture";
import { UrlError } from "@fedify/vocab-runtime";
//#region src/utils/docloader.test.ts
test("getAuthenticatedDocumentLoader()", async (t) => {
esm_default.spyGlobal();
esm_default.get("begin:https://example.com/object", async (cl) => {
const v = await verifyRequest(cl.request, {
documentLoader: mockDocumentLoader,
contextLoader: mockDocumentLoader,
currentTime: Temporal.Now.instant()
});
return new Response(JSON.stringify(v != null), { headers: { "Content-Type": "application/json" } });
});
await t.step("test", async () => {
assertEquals(await (await getAuthenticatedDocumentLoader({
keyId: new URL("https://example.com/key2"),
privateKey: rsaPrivateKey2
}))("https://example.com/object"), {
contextUrl: null,
documentUrl: "https://example.com/object",
document: true
});
});
esm_default.hardReset();
await t.step("deny non-HTTP/HTTPS", async () => {
const loader = await getAuthenticatedDocumentLoader({
keyId: new URL("https://example.com/key2"),
privateKey: rsaPrivateKey2
});
assertRejects(() => loader("ftp://localhost"), UrlError);
});
await t.step("deny private network", async () => {
const loader = await getAuthenticatedDocumentLoader({
keyId: new URL("https://example.com/key2"),
privateKey: rsaPrivateKey2
});
assertRejects(() => loader("http://localhost"), UrlError);
});
await t.step("custom max redirection", async () => {
esm_default.spyGlobal();
let requestCount = 0;
esm_default.get("begin:https://example.com/custom-too-many-redirects/", (cl) => {
requestCount++;
const index = Number(cl.url.split("/").at(-1));
return Response.redirect(`https://example.com/custom-too-many-redirects/${index + 1}`, 302);
});
const loader = getAuthenticatedDocumentLoader({
keyId: new URL("https://example.com/key2"),
privateKey: rsaPrivateKey2
}, { maxRedirection: 1 });
await assertRejects(() => loader("https://example.com/custom-too-many-redirects/0"), Error, "Too many redirections");
assertEquals(requestCount, 2);
esm_default.hardReset();
});
});
test("getAuthenticatedDocumentLoader() cancellation", {
sanitizeResources: false,
sanitizeOps: false
}, async (t) => {
esm_default.spyGlobal();
await t.step("document loader cancellation", async () => {
esm_default.get("https://example.com/slow-object", () => new Promise((resolve) => {
setTimeout(() => {
resolve({
status: 200,
headers: { "Content-Type": "application/activity+json" },
body: {
"@context": "https://www.w3.org/ns/activitystreams",
type: "Note",
content: "Slow response"
}
});
}, 1e3);
}));
const loader = getAuthenticatedDocumentLoader({
keyId: new URL("https://example.com/key2"),
privateKey: rsaPrivateKey2
});
const controller = new AbortController();
const promise = loader("https://example.com/slow-object", { signal: controller.signal });
controller.abort();
await assertRejects(() => promise, Error);
await assertRejects(() => loader("https://example.com/object", { signal: controller.signal }), Error);
});
await t.step("immediate cancellation", async () => {
const loader = getAuthenticatedDocumentLoader({
keyId: new URL("https://example.com/key2"),
privateKey: rsaPrivateKey2
});
const controller = new AbortController();
controller.abort();
await assertRejects(() => loader("https://example.com/object", { signal: controller.signal }), Error);
});
esm_default.hardReset();
});
//#endregion
export {};