|
| 1 | +import { createApp } from '../../app' |
| 2 | + |
| 3 | +/** |
| 4 | + * Test client for making requests to the Hono app. |
| 5 | + */ |
| 6 | +export interface TestAppClient { |
| 7 | + /** The Hono app instance */ |
| 8 | + app: ReturnType<typeof createApp> |
| 9 | + |
| 10 | + /** Make a GET request */ |
| 11 | + get: (path: string, headers?: Record<string, string>) => Promise<Response> |
| 12 | + |
| 13 | + /** Make a POST request with JSON body */ |
| 14 | + post: (path: string, body?: unknown, headers?: Record<string, string>) => Promise<Response> |
| 15 | + |
| 16 | + /** Make a PATCH request with JSON body */ |
| 17 | + patch: (path: string, body?: unknown, headers?: Record<string, string>) => Promise<Response> |
| 18 | + |
| 19 | + /** Make a PUT request with JSON body */ |
| 20 | + put: (path: string, body?: unknown, headers?: Record<string, string>) => Promise<Response> |
| 21 | + |
| 22 | + /** Make a DELETE request */ |
| 23 | + delete: (path: string, headers?: Record<string, string>) => Promise<Response> |
| 24 | + |
| 25 | + /** Make a request with full control */ |
| 26 | + request: (path: string, init?: RequestInit) => Promise<Response> |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Creates a test client for the Hono app. |
| 31 | + * Uses Hono's native request method for testing without a real server. |
| 32 | + */ |
| 33 | +export function createTestApp(): TestAppClient { |
| 34 | + const app = createApp() |
| 35 | + |
| 36 | + const request = async (path: string, init?: RequestInit): Promise<Response> => { |
| 37 | + const url = `http://localhost${path}` |
| 38 | + return app.request(url, init) |
| 39 | + } |
| 40 | + |
| 41 | + return { |
| 42 | + app, |
| 43 | + request, |
| 44 | + |
| 45 | + get: (path: string, headers?: Record<string, string>) => { |
| 46 | + return request(path, { |
| 47 | + method: 'GET', |
| 48 | + headers, |
| 49 | + }) |
| 50 | + }, |
| 51 | + |
| 52 | + post: (path: string, body?: unknown, headers?: Record<string, string>) => { |
| 53 | + return request(path, { |
| 54 | + method: 'POST', |
| 55 | + headers: { |
| 56 | + 'Content-Type': 'application/json', |
| 57 | + ...headers, |
| 58 | + }, |
| 59 | + body: body !== undefined ? JSON.stringify(body) : undefined, |
| 60 | + }) |
| 61 | + }, |
| 62 | + |
| 63 | + patch: (path: string, body?: unknown, headers?: Record<string, string>) => { |
| 64 | + return request(path, { |
| 65 | + method: 'PATCH', |
| 66 | + headers: { |
| 67 | + 'Content-Type': 'application/json', |
| 68 | + ...headers, |
| 69 | + }, |
| 70 | + body: body !== undefined ? JSON.stringify(body) : undefined, |
| 71 | + }) |
| 72 | + }, |
| 73 | + |
| 74 | + put: (path: string, body?: unknown, headers?: Record<string, string>) => { |
| 75 | + return request(path, { |
| 76 | + method: 'PUT', |
| 77 | + headers: { |
| 78 | + 'Content-Type': 'application/json', |
| 79 | + ...headers, |
| 80 | + }, |
| 81 | + body: body !== undefined ? JSON.stringify(body) : undefined, |
| 82 | + }) |
| 83 | + }, |
| 84 | + |
| 85 | + delete: (path: string, headers?: Record<string, string>) => { |
| 86 | + return request(path, { |
| 87 | + method: 'DELETE', |
| 88 | + headers, |
| 89 | + }) |
| 90 | + }, |
| 91 | + } |
| 92 | +} |
0 commit comments