-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathserver.ts
More file actions
123 lines (106 loc) · 2.76 KB
/
server.ts
File metadata and controls
123 lines (106 loc) · 2.76 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
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
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import http, {
type IncomingMessage,
type Server,
type ServerResponse,
} from 'node:http';
import {before, after, afterEach} from 'node:test';
import {html} from './utils.js';
export class TestServer {
#port: number;
#server: Server;
static randomPort() {
/**
* Some ports are restricted by Chromium and will fail to connect
* to prevent we start after the
*
* https://source.chromium.org/chromium/chromium/src/+/main:net/base/port_util.cc;l=107?q=kRestrictedPorts&ss=chromium
*/
const min = 10101;
const max = 20202;
return Math.floor(Math.random() * (max - min + 1) + min);
}
#routes: Record<string, (req: IncomingMessage, res: ServerResponse) => void> =
{};
constructor(port: number) {
this.#port = port;
this.#server = http.createServer((req, res) => this.#handle(req, res));
}
get baseUrl(): string {
return `http://localhost:${this.#port}`;
}
getRoute(path: string) {
if (!this.#routes[path]) {
throw new Error(`Route ${path} was not setup.`);
}
return `${this.baseUrl}${path}`;
}
addHtmlRoute(path: string, htmlContent: string) {
if (this.#routes[path]) {
throw new Error(`Route ${path} was already setup.`);
}
this.#routes[path] = (_req: IncomingMessage, res: ServerResponse) => {
res.setHeader('Content-Type', 'text/html; charset=utf-8');
res.statusCode = 200;
res.end(htmlContent);
};
}
addRoute(
path: string,
handler: (req: IncomingMessage, res: ServerResponse) => void,
) {
if (this.#routes[path]) {
throw new Error(`Route ${path} was already setup.`);
}
this.#routes[path] = handler;
}
#handle(req: IncomingMessage, res: ServerResponse) {
const url = req.url ?? '';
const routeHandler = this.#routes[url];
if (routeHandler) {
routeHandler(req, res);
} else {
res.writeHead(404, {'Content-Type': 'text/html'});
res.end(
html`<h1>404 - Not Found</h1><p>The requested page does not exist.</p>`,
);
}
}
restore() {
this.#routes = {};
}
start(): Promise<void> {
return new Promise(res => {
this.#server.listen(this.#port, res);
});
}
stop(): Promise<void> {
return new Promise((res, rej) => {
this.#server.closeAllConnections();
this.#server.close(err => {
if (err) {
rej(err);
} else {
res();
}
});
});
}
}
export function serverHooks() {
const server = new TestServer(TestServer.randomPort());
before(async () => {
await server.start();
});
after(async () => {
await server.stop();
});
afterEach(() => {
server.restore();
});
return server;
}