-
Notifications
You must be signed in to change notification settings - Fork 235
/
http_server_bun.test.ts
153 lines (137 loc) · 3.98 KB
/
http_server_bun.test.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright 2018-2024 the oak authors. All rights reserved. MIT license.
// deno-lint-ignore-file no-explicit-any
import { assert } from "./deps.ts";
import { assertEquals } from "./deps_test.ts";
import { createMockApp } from "./testing.ts";
import { Server } from "./http_server_bun.ts";
interface SocketAddress {
address: string;
port: number;
family: "IPv4" | "IPv6";
}
let currentServer: MockBunServer | undefined;
let requests: Request[] = [];
class MockBunServer {
stoppedCount = 0;
fetch: (
req: Request,
server: this,
) => Response | Promise<Response>;
responses: Response[] = [];
runPromise: Promise<void>;
development: boolean;
hostname: string;
port: number;
pendingRequests = 0;
async #run() {
for (const req of requests) {
const res = await this.fetch(req, this);
this.responses.push(res);
}
}
constructor(
{ fetch, hostname, port, development }: {
fetch: (
req: Request,
server: unknown,
) => Response | Promise<Response>;
hostname?: string;
port?: number;
development?: boolean;
error?: (error: Error) => Response | Promise<Response>;
tls?: {
key?: string;
cert?: string;
};
},
) {
this.fetch = fetch;
this.development = development ?? false;
this.hostname = hostname ?? "localhost";
this.port = port ?? 567890;
currentServer = this;
this.runPromise = this.#run();
}
requestIP(_req: Request): SocketAddress | null {
return { address: "127.0.0.0", port: 567890, family: "IPv4" };
}
stop(): void {
this.stoppedCount++;
}
}
function setup(reqs?: Request[]) {
if (reqs) {
requests = reqs;
}
(globalThis as any)["Bun"] = {
serve(options: any) {
return new MockBunServer(options);
},
};
}
function teardown() {
delete (globalThis as any)["Bun"];
currentServer = undefined;
}
Deno.test({
name: "bun server can listen",
async fn() {
setup();
const server = new Server(createMockApp(), { port: 8080 });
const listener = await server.listen();
assertEquals(listener, { addr: { hostname: "localhost", port: 8080 } });
assert(currentServer);
assertEquals(currentServer.stoppedCount, 0);
await server.close();
assertEquals(currentServer.stoppedCount, 1);
teardown();
},
});
Deno.test({
name: "bun server can process requests",
// this is working but there is some sort of hanging promise somewhere I can't
// narrow down at the moment
ignore: true,
async fn() {
setup([new Request(new URL("http://localhost:8080/"))]);
const server = new Server(createMockApp(), { port: 8080 });
const listener = await server.listen();
assertEquals(listener, { addr: { hostname: "localhost", port: 8080 } });
assert(currentServer);
for await (const req of server) {
assert(!req.body);
assertEquals(req.url, "/");
await req.respond(new Response("hello world"));
}
await server.close();
await currentServer.runPromise;
assertEquals(currentServer.stoppedCount, 1);
assertEquals(currentServer.responses.length, 1);
teardown();
},
});
Deno.test({
name: "bun server closes on abort signal",
// this is working but there is some sort of hanging promise somewhere I can't
// narrow down at the moment
ignore: true,
async fn() {
setup([new Request(new URL("http://localhost:8080/"))]);
const controller = new AbortController();
const { signal } = controller;
const server = new Server(createMockApp(), { port: 8080, signal });
const listener = await server.listen();
assertEquals(listener, { addr: { hostname: "localhost", port: 8080 } });
assert(currentServer);
for await (const req of server) {
assert(!req.body);
assertEquals(req.url, "/");
await req.respond(new Response("hello world"));
}
controller.abort();
await currentServer.runPromise;
assertEquals(currentServer.stoppedCount, 1);
assertEquals(currentServer.responses.length, 1);
teardown();
},
});