Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions packages/elysia/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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<void>({
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<void>({ 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",
);
});
});