Skip to content

Commit 7dcf4b2

Browse files
committed
Negotiate after determining if the resource exists
Fix #34
1 parent d62ee37 commit 7dcf4b2

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ Version 0.5.1
1313

1414
To be released.
1515

16+
- Fixed a bug of `Federation` that its actor/collection dispatchers had done
17+
content negotiation before determining if the resource exists or not.
18+
It also fixed a bug that `integrateHandler()` from `@fedify/fedify/x/fresh`
19+
had responded with `406 Not Acceptable` instead of `404 Not Found` when
20+
the resource does not exist in the web browser. [[#34]]
21+
22+
[#34]: https://github.com/dahlia/fedify/issues/34
23+
1624

1725
Version 0.5.0
1826
-------------

federation/handler.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,21 @@ Deno.test("handleActor()", async () => {
100100
assertEquals(onNotAcceptableCalled, context.request);
101101

102102
onNotAcceptableCalled = null;
103+
response = await handleActor(
104+
context.request,
105+
{
106+
context,
107+
handle: "no-one",
108+
actorDispatcher,
109+
onNotFound,
110+
onNotAcceptable,
111+
},
112+
);
113+
assertEquals(response.status, 404);
114+
assertEquals(onNotFoundCalled, context.request);
115+
assertEquals(onNotAcceptableCalled, null);
116+
117+
onNotFoundCalled = null;
103118
context = createRequestContext<void>({
104119
...context,
105120
request: new Request(context.url, {
@@ -235,6 +250,21 @@ Deno.test("handleCollection()", async () => {
235250
assertEquals(onNotAcceptableCalled, context.request);
236251

237252
onNotAcceptableCalled = null;
253+
response = await handleCollection(
254+
context.request,
255+
{
256+
context,
257+
handle: "no-one",
258+
collectionCallbacks: { dispatcher },
259+
onNotFound,
260+
onNotAcceptable,
261+
},
262+
);
263+
assertEquals(response.status, 404);
264+
assertEquals(onNotFoundCalled, context.request);
265+
assertEquals(onNotAcceptableCalled, null);
266+
267+
onNotFoundCalled = null;
238268
context = createRequestContext<void>({
239269
...context,
240270
request: new Request(context.url, {

federation/handler.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ export async function handleActor<TContextData>(
5353
const response = onNotFound(request);
5454
return response instanceof Promise ? await response : response;
5555
}
56-
if (!acceptsJsonLd(request)) {
57-
const response = onNotAcceptable(request);
58-
return response instanceof Promise ? await response : response;
59-
}
6056
const key = await context.getActorKey(handle);
6157
const actor = await actorDispatcher(context, handle, key);
6258
if (actor == null) {
6359
const response = onNotFound(request);
6460
return response instanceof Promise ? await response : response;
6561
}
62+
if (!acceptsJsonLd(request)) {
63+
const response = onNotAcceptable(request);
64+
return response instanceof Promise ? await response : response;
65+
}
6666
const jsonLd = await actor.toJsonLd(context);
6767
return new Response(JSON.stringify(jsonLd), {
6868
headers: {
@@ -122,10 +122,6 @@ export async function handleCollection<
122122
const response = onNotFound(request);
123123
return response instanceof Promise ? await response : response;
124124
}
125-
if (!acceptsJsonLd(request)) {
126-
const response = onNotAcceptable(request);
127-
return response instanceof Promise ? await response : response;
128-
}
129125
const url = new URL(request.url);
130126
const cursor = url.searchParams.get("cursor");
131127
let collection: OrderedCollection | OrderedCollectionPage;
@@ -200,6 +196,10 @@ export async function handleCollection<
200196
partOf.searchParams.delete("cursor");
201197
collection = new OrderedCollectionPage({ prev, next, items, partOf });
202198
}
199+
if (!acceptsJsonLd(request)) {
200+
const response = onNotAcceptable(request);
201+
return response instanceof Promise ? await response : response;
202+
}
203203
const jsonLd = await collection.toJsonLd(context);
204204
return new Response(JSON.stringify(jsonLd), {
205205
headers: {

0 commit comments

Comments
 (0)