Repository navigation
Expand file tree
/
Copy pathhttp.ts
More file actions
92 lines (81 loc) · 2.57 KB
/
Copy pathhttp.ts
File metadata and controls
92 lines (81 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/**
* HTTP Feature-test helpers — bootstrap + Bun.serve without boilerplate.
*
* Zero runtime deps beyond bun:test / Bun APIs used by the caller.
*
* @packageDocumentation
*/
import { expect } from "bun:test";
import type { Application } from "@ninots/foundation";
import { bootstrap, createAppServeOptions } from "@/bootstrap/app";
export type TestRequestOptions = {
headers?: HeadersInit;
body?: BodyInit | null;
};
export type TestApp = {
app: Application;
baseUrl: string;
stop: () => void;
get: (path: string, options?: TestRequestOptions) => Promise<Response>;
post: (path: string, options?: TestRequestOptions) => Promise<Response>;
};
function normalizePath(path: string): string {
if (path.startsWith("http://") || path.startsWith("https://")) {
return path;
}
return path.startsWith("/") ? path : `/${path}`;
}
/**
* Boot the app and serve on an ephemeral local port.
*/
export async function createTestApp(): Promise<TestApp> {
const app = await bootstrap();
const server = Bun.serve({
...createAppServeOptions(app),
port: 0,
hostname: "127.0.0.1",
});
const baseUrl = `http://127.0.0.1:${server.port}`;
const request = async (method: string, path: string, options: TestRequestOptions = {}): Promise<Response> => {
const url =
path.startsWith("http://") || path.startsWith("https://") ? path : `${baseUrl}${normalizePath(path)}`;
return fetch(url, {
method,
headers: options.headers,
body: options.body,
redirect: "manual",
});
};
return {
app,
baseUrl,
stop: () => {
server.stop();
},
get: (path, options) => request("GET", path, options),
post: (path, options) => request("POST", path, options),
};
}
/**
* Assert HTTP status code.
*/
export function assertStatus(response: Response, status: number): void {
expect(response.status).toBe(status);
}
/**
* Assert JSON body deep-equals `expected` (and Content-Type includes json when present).
*/
export async function assertJson(response: Response, expected: unknown): Promise<void> {
const contentType = response.headers.get("Content-Type");
if (contentType !== null) {
expect(contentType).toContain("json");
}
const body: unknown = await response.json();
expect(body).toEqual(expected);
}
/**
* Read response text body (convenience for HTML assertions).
*/
export async function responseText(response: Response): Promise<string> {
return response.text();
}