Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ Version 2.0.4

To be released.

### @fedify/fastify

- Fixed the default `onNotAcceptable` handler in `@fedify/fastify` to
create a fresh `Response` for each request instead of reusing a shared
singleton instance. [[#612] by Lee Dogeon]

[#612]: https://github.com/fedify-dev/fedify/pull/612


Version 2.0.3
-------------
Expand Down
37 changes: 37 additions & 0 deletions packages/fastify/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,43 @@ test("Fedify should handle notAcceptable and return 406", async () => {
await fastify.close();
});

test("Fedify should create a fresh 406 response for each request", async () => {
const fastify = Fastify({ logger: false });
const federation = createFederation<void>({ kv: new MemoryKvStore() });

federation.setActorDispatcher(
"/users/{identifier}",
(_ctx: RequestContext<void>, identifier: string) => {
return new Person({
id: new URL(`https://example.com/users/${identifier}`),
preferredUsername: identifier,
name: `User ${identifier}`,
});
},
);

await fastify.register(fedifyPlugin, { federation });
await fastify.ready();

const firstResponse = await fastify.inject({
method: "GET",
url: "/users/alice",
headers: { "Accept": "text/html" },
});
const secondResponse = await fastify.inject({
method: "GET",
url: "/users/alice",
headers: { "Accept": "text/html" },
});

assert.equal(firstResponse.statusCode, 406);
assert.equal(firstResponse.body, "Not Acceptable");
assert.equal(secondResponse.statusCode, 406);
assert.equal(secondResponse.body, "Not Acceptable");

await fastify.close();
});

test("Fedify should handle notAcceptable with custom error handler", async () => {
const fastify = Fastify({ logger: false });
const federation = createFederation<void>({ kv: new MemoryKvStore() });
Expand Down
11 changes: 6 additions & 5 deletions packages/fastify/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ const fedifyPluginCore: FastifyPluginAsync<FedifyPluginOptions<unknown>> = (

const response = await federation.fetch(webRequest, {
contextData,
onNotAcceptable: () => defaultNotAcceptableResponse,
onNotAcceptable: createDefaultNotAcceptableResponse,
onNotFound: () => dummyNotFoundResponse,
...errorHandlers,
});
Expand All @@ -101,10 +101,11 @@ const fedifyPlugin: FastifyPluginAsync<FedifyPluginOptions<unknown>> = fp(
);

const dummyNotFoundResponse = new Response("", { status: 404 });
const defaultNotAcceptableResponse = new Response("Not Acceptable", {
status: 406,
headers: { "Content-Type": "text/plain", Vary: "Accept" },
});
const createDefaultNotAcceptableResponse = () =>
new Response("Not Acceptable", {
status: 406,
headers: { "Content-Type": "text/plain", Vary: "Accept" },
});

/**
* Convert Fastify request to Web API Request.
Expand Down
Loading