diff --git a/packages/elysia/src/index.test.ts b/packages/elysia/src/index.test.ts index 1d4084dc1..aeb9f2db6 100644 --- a/packages/elysia/src/index.test.ts +++ b/packages/elysia/src/index.test.ts @@ -1,3 +1,4 @@ +import { createFederation, MemoryKvStore } from "@fedify/fedify"; import { Elysia } from "elysia"; import { strict as assert } from "node:assert"; import { describe, test } from "node:test"; @@ -46,4 +47,67 @@ describe("[elysia] fedify() plugin", () => { "federation.fetch() must receive the context data returned by the factory", ); }); + + test("fedify() falls through to Elysia routes when federation reports not-found via onNotFound", async () => { + const elysia = new Elysia().get( + "/hello-world", + ({ set, status }) => { + set.headers["X-Custom-Header"] = "custom-value"; + return status(201, "Hello World"); + }, + ); + const federationWithoutDispatcher = createFederation({ + kv: new MemoryKvStore(), + }); + + elysia.use(fedify(federationWithoutDispatcher, () => undefined)); + const response = await elysia.handle( + new Request("http://localhost/hello-world"), + ); + + assert.strictEqual(response.status, 201, "status must come from Elysia"); + assert.strictEqual( + response.headers.get("X-Custom-Header"), + "custom-value", + "header must come from Elysia", + ); + assert.strictEqual( + await response.text(), + "Hello World", + "body must come from Elysia", + ); + }); + + test("fedify() falls through to Elysia routes when federation declines the request via onNotAcceptable", async () => { + const elysia = new Elysia().get( + "/users/alice", + ({ set, status }) => { + set.headers["X-Custom-Header"] = "custom-value"; + return status(200, "Hello Alice"); + }, + ); + // Register the actor route so the request matches, but ask for text/html + // so federation declines via onNotAcceptable instead of handling it. + const federation = createFederation({ kv: new MemoryKvStore() }); + federation.setActorDispatcher("/users/{identifier}", () => null); + + elysia.use(fedify(federation, () => undefined)); + const response = await elysia.handle( + new Request("http://localhost/users/alice", { + headers: { Accept: "text/html" }, + }), + ); + + assert.strictEqual(response.status, 200, "status must come from Elysia"); + assert.strictEqual( + response.headers.get("X-Custom-Header"), + "custom-value", + "header must come from Elysia", + ); + assert.strictEqual( + await response.text(), + "Hello Alice", + "body must come from Elysia", + ); + }); });