-
-
Notifications
You must be signed in to change notification settings - Fork 132
Add response-handling tests for @fedify/h3 #1007
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dahlia
merged 5 commits into
fedify-dev:main
from
Jae-Hyuk-Jang:test/h3-response-handling
Aug 26, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
67e6794
Add test scripts for @fedify/h3
Jae-Hyuk-Jang 6c19eab
Test handled responses in @fedify/h3 (#859)
Jae-Hyuk-Jang 9e95236
Test 404 delegation in @fedify/h3 (#860)
Jae-Hyuk-Jang c2247f7
Test 406 storage in @fedify/h3 (#861)
Jae-Hyuk-Jang 2a30b50
Test onError() in @fedify/h3 (#862)
Jae-Hyuk-Jang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| import type { Federation } from "@fedify/fedify"; | ||
| import type { H3Error, H3Event } from "h3"; | ||
| import { strict as assert } from "node:assert"; | ||
| import { describe, test } from "node:test"; | ||
| import { integrateFederation, onError } from "./index.ts"; | ||
|
|
||
| interface MockFederation { | ||
| fetch(request: Request, options: unknown): Promise<Response>; | ||
| } | ||
|
|
||
| function createMockEvent(request: Request): { | ||
| event: H3Event; | ||
| respondWithCalls: Response[]; | ||
| } { | ||
| const respondWithCalls: Response[] = []; | ||
| const event = { | ||
| web: { request }, | ||
| context: {}, | ||
| respondWith(response: Response) { | ||
| respondWithCalls.push(response); | ||
| return Promise.resolve(); | ||
| }, | ||
| }; | ||
| return { event: event as unknown as H3Event, respondWithCalls }; | ||
| } | ||
|
|
||
| describe("integrateFederation()", () => { | ||
| test("responds with the Fedify response when it is handled (e.g., 200 OK)", async () => { | ||
| const okResponse = new Response("Hello, world!", { status: 200 }); | ||
| const mockFederation: MockFederation = { | ||
| fetch() { | ||
| return Promise.resolve(okResponse); | ||
| }, | ||
| }; | ||
| const handler = integrateFederation( | ||
| mockFederation as unknown as Federation<void>, | ||
| () => undefined, | ||
| ); | ||
| const { event, respondWithCalls } = createMockEvent( | ||
| new Request("https://example.com/"), | ||
| ); | ||
|
|
||
| await handler(event); | ||
|
|
||
| assert.equal(respondWithCalls.length, 1); | ||
| assert.equal(respondWithCalls[0], okResponse); | ||
| }); | ||
|
|
||
| test("does not respond and delegates to the next handler on 404 Not Found", async () => { | ||
| const mockFederation: MockFederation = { | ||
| fetch() { | ||
| return Promise.resolve(new Response(null, { status: 404 })); | ||
| }, | ||
| }; | ||
| const handler = integrateFederation( | ||
| mockFederation as unknown as Federation<void>, | ||
| () => undefined, | ||
| ); | ||
| const { event, respondWithCalls } = createMockEvent( | ||
| new Request("https://example.com/"), | ||
| ); | ||
|
|
||
| await handler(event); | ||
|
|
||
| assert.equal(respondWithCalls.length, 0); | ||
| assert.equal("__fedify_response__" in event.context, false); | ||
| }); | ||
|
|
||
| test("stores the response in the event context on 406 Not Acceptable", async () => { | ||
| const notAcceptableResponse = new Response(null, { status: 406 }); | ||
| const mockFederation: MockFederation = { | ||
| fetch() { | ||
| return Promise.resolve(notAcceptableResponse); | ||
| }, | ||
| }; | ||
| const handler = integrateFederation( | ||
| mockFederation as unknown as Federation<void>, | ||
| () => undefined, | ||
| ); | ||
| const { event, respondWithCalls } = createMockEvent( | ||
| new Request("https://example.com/"), | ||
| ); | ||
|
|
||
| await handler(event); | ||
|
|
||
| assert.equal(respondWithCalls.length, 0); | ||
| assert.equal(event.context["__fedify_response__"], notAcceptableResponse); | ||
| }); | ||
|
|
||
| test("waits for an async contextDataFactory and passes the resolved value to federation.fetch()", async () => { | ||
| let resolveContextData!: (value: string) => void; | ||
| let fetchCalled = false; | ||
| let receivedContextData: unknown; | ||
| const mockFederation: MockFederation = { | ||
| fetch(_request, options) { | ||
| fetchCalled = true; | ||
| const { contextData } = options as { contextData: unknown }; | ||
| receivedContextData = contextData; | ||
| return Promise.resolve(new Response(null, { status: 200 })); | ||
| }, | ||
| }; | ||
| const contextDataFactory = () => | ||
| new Promise<string>((resolve) => { | ||
| resolveContextData = resolve; | ||
| }); | ||
| const handler = integrateFederation( | ||
| mockFederation as unknown as Federation<string>, | ||
| contextDataFactory, | ||
| ); | ||
| const { event } = createMockEvent(new Request("https://example.com/")); | ||
|
|
||
| const handlerPromise = handler(event); | ||
| await Promise.resolve(); | ||
| assert.equal(fetchCalled, false); | ||
|
|
||
| resolveContextData("context data"); | ||
| await handlerPromise; | ||
|
|
||
| assert.equal(fetchCalled, true); | ||
| assert.equal(receivedContextData, "context data"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("onError()", () => { | ||
| function createMockError(statusCode: number): H3Error { | ||
| return { statusCode } as unknown as H3Error; | ||
| } | ||
|
|
||
| test("responds with the stored 406 response when the handler later reports 404", async () => { | ||
| const notAcceptableResponse = new Response(null, { status: 406 }); | ||
| const { event, respondWithCalls } = createMockEvent( | ||
| new Request("https://example.com/"), | ||
| ); | ||
| event.context["__fedify_response__"] = notAcceptableResponse; | ||
|
|
||
| await onError(createMockError(404), event); | ||
|
|
||
| assert.equal(respondWithCalls.length, 1); | ||
| assert.equal(respondWithCalls[0], notAcceptableResponse); | ||
| }); | ||
|
|
||
| test("does nothing when there is no stored 406 response", async () => { | ||
| const { event, respondWithCalls } = createMockEvent( | ||
| new Request("https://example.com/"), | ||
| ); | ||
|
|
||
| await onError(createMockError(404), event); | ||
|
|
||
| assert.equal(respondWithCalls.length, 0); | ||
| }); | ||
|
|
||
| test("does nothing when the error status is not 404", async () => { | ||
| const notAcceptableResponse = new Response(null, { status: 406 }); | ||
| const { event, respondWithCalls } = createMockEvent( | ||
| new Request("https://example.com/"), | ||
| ); | ||
| event.context["__fedify_response__"] = notAcceptableResponse; | ||
|
|
||
| await onError(createMockError(500), event); | ||
|
|
||
| assert.equal(respondWithCalls.length, 0); | ||
| }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.