/
test_util.ts
43 lines (37 loc) · 1.1 KB
/
test_util.ts
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
import type { ServerRequest } from "./vendor/https/deno.land/std/http/server.ts";
import { Application } from "./app.ts";
const encoder = new TextEncoder();
export function createApplication(): Application {
const app = new Application();
app.start({ port: 8081 });
return app;
}
export function createMockRequest(
options: {
url?: string;
headers?: Headers;
respond?: () => Promise<void>;
body?: Deno.Reader;
} = {},
): ServerRequest {
options.url = options.url ?? "https://example.com/";
options.headers = options.headers ?? new Headers();
options.respond = options.respond ?? async function (): Promise<void> {};
options.body = options.body ?? undefined;
return { ...options } as any;
}
export function createMockBodyReader(data: string): Deno.Reader {
const buf = encoder.encode(data);
let offset = 0;
return {
async read(p: Uint8Array): Promise<number | null> {
if (offset >= buf.length) {
return null;
}
const chunkSize = Math.min(p.length, buf.length - offset);
p.set(buf);
offset += chunkSize;
return chunkSize;
},
};
}