Commit ec2acc8e authored by r-4bb1t's avatar r-4bb1t
Browse files

test: add cancellation tests for `getAuthenticatedDocumentLoader` and `doubleKnock` functions

parent a5c53d37
Loading
Loading
Loading
Loading
+66 −0
Original line number Diff line number Diff line
@@ -57,3 +57,69 @@ test("getAuthenticatedDocumentLoader()", async (t) => {
    assertRejects(() => loader("http://localhost"), UrlError);
  });
});

test("getAuthenticatedDocumentLoader() cancellation", {
  sanitizeResources: false,
  sanitizeOps: false,
}, async (t) => {
  fetchMock.spyGlobal();

  await t.step("document loader cancellation", async () => {
    fetchMock.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",
              },
            });
          }, 1000);
        }),
    );

    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,
    );
  });

  fetchMock.hardReset();
});
+70 −0
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ import {
  assertEquals,
  assertExists,
  assertFalse,
  assertRejects,
  assertStringIncludes,
  assertThrows,
} from "@std/assert";
@@ -1975,3 +1976,72 @@ test("doubleKnock() regression test for redirect handling bug", async () => {

  fetchMock.hardReset();
});

test("signRequest() and verifyRequest() cancellation", {
  sanitizeResources: false,
  sanitizeOps: false,
}, async (t) => {
  fetchMock.spyGlobal();

  await t.step("doubleKnock cancellation", async () => {
    fetchMock.post(
      "https://example.com/slow-endpoint",
      () =>
        new Promise((resolve) => {
          setTimeout(() => {
            resolve(new Response("", { status: 202 }));
          }, 1000);
        }),
    );

    const request = new Request("https://example.com/slow-endpoint", {
      method: "POST",
      body: "Test message",
      headers: {
        "Content-Type": "text/plain",
      },
    });

    const controller = new AbortController();
    const promise = doubleKnock(
      request,
      {
        keyId: rsaPublicKey2.id!,
        privateKey: rsaPrivateKey2,
      },
      { signal: controller.signal },
    );

    controller.abort();

    await assertRejects(
      () => promise,
      Error,
    );
  });

  await t.step("doubleKnock immediate cancellation", async () => {
    const request = new Request("https://example.com/", {
      method: "POST",
      body: "Hello, world!",
      headers: {
        "Content-Type": "text/plain; charset=utf-8",
        Accept: "text/plain",
      },
    });

    const controller = new AbortController();
    controller.abort();

    await assertRejects(
      () =>
        doubleKnock(request, {
          keyId: rsaPublicKey2.id!,
          privateKey: rsaPrivateKey2,
        }, { signal: controller.signal }),
      Error,
    );
  });

  fetchMock.hardReset();
});