-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhttp-server.test.ts
More file actions
197 lines (167 loc) · 5.78 KB
/
Copy pathhttp-server.test.ts
File metadata and controls
197 lines (167 loc) · 5.78 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
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { describe, it, expect } from 'vitest';
import type {
IHttpRequest,
IHttpResponse,
RouteHandler,
Middleware,
IHttpServer,
} from './http-server';
describe('HTTP Server Contract', () => {
describe('IHttpRequest interface', () => {
it('should allow a valid request object', () => {
const req: IHttpRequest = {
params: { id: '123' },
query: { page: '1', tags: ['a', 'b'] },
body: { name: 'Test' },
headers: { 'content-type': 'application/json' },
method: 'POST',
path: '/api/users',
};
expect(req.params.id).toBe('123');
expect(req.method).toBe('POST');
expect(req.path).toBe('/api/users');
expect(req.body).toEqual({ name: 'Test' });
});
it('should allow a minimal request without body', () => {
const req: IHttpRequest = {
params: {},
query: {},
headers: {},
method: 'GET',
path: '/',
};
expect(req.body).toBeUndefined();
expect(req.method).toBe('GET');
});
});
describe('IHttpResponse interface', () => {
it('should support chaining status and header calls', () => {
const res: IHttpResponse = {
json: (_data) => {},
send: (_data) => {},
status: function (code) {
return this;
},
header: function (name, value) {
return this;
},
};
// Chaining: res.status(200).header('X-Custom', 'val').json({})
const chained = res.status(200).header('X-Custom', 'value');
expect(chained).toBeDefined();
});
it('should call json and send', () => {
const sent: any[] = [];
const res: IHttpResponse = {
json: (data) => { sent.push({ type: 'json', data }); },
send: (data) => { sent.push({ type: 'text', data }); },
status: function () { return this; },
header: function () { return this; },
};
res.json({ message: 'ok' });
res.send('<h1>Hello</h1>');
expect(sent).toHaveLength(2);
expect(sent[0]).toEqual({ type: 'json', data: { message: 'ok' } });
expect(sent[1]).toEqual({ type: 'text', data: '<h1>Hello</h1>' });
});
});
describe('RouteHandler type', () => {
it('should accept a sync route handler', () => {
const handler: RouteHandler = (_req, res) => {
res.status(200).json({ ok: true });
};
expect(typeof handler).toBe('function');
});
it('should accept an async route handler', () => {
const handler: RouteHandler = async (_req, res) => {
res.status(200).json({ ok: true });
};
expect(typeof handler).toBe('function');
});
});
describe('Middleware type', () => {
it('should accept a sync middleware', () => {
const mw: Middleware = (_req, _res, next) => {
next();
};
expect(typeof mw).toBe('function');
});
it('should accept an async middleware', () => {
const mw: Middleware = async (_req, _res, next) => {
await next();
};
expect(typeof mw).toBe('function');
});
});
describe('IHttpServer interface', () => {
it('should allow a full implementation', () => {
const routes: Array<{ method: string; path: string }> = [];
const server: IHttpServer = {
get: (path, _handler) => { routes.push({ method: 'GET', path }); },
post: (path, _handler) => { routes.push({ method: 'POST', path }); },
put: (path, _handler) => { routes.push({ method: 'PUT', path }); },
delete: (path, _handler) => { routes.push({ method: 'DELETE', path }); },
patch: (path, _handler) => { routes.push({ method: 'PATCH', path }); },
use: (_pathOrHandler, _handler?) => {},
listen: async (_port) => {},
};
server.get('/api/users', async (_req, res) => res.json([]));
server.post('/api/users', async (_req, res) => res.status(201).json({}));
server.put('/api/users/:id', async (_req, res) => res.json({}));
server.delete('/api/users/:id', async (_req, res) => res.status(204).send(''));
server.patch('/api/users/:id', async (_req, res) => res.json({}));
expect(routes).toHaveLength(5);
expect(routes.map(r => r.method)).toEqual(['GET', 'POST', 'PUT', 'DELETE', 'PATCH']);
});
it('should support middleware registration', () => {
const middlewareApplied: string[] = [];
const server: IHttpServer = {
get: () => {},
post: () => {},
put: () => {},
delete: () => {},
patch: () => {},
use: (pathOrHandler, _handler?) => {
if (typeof pathOrHandler === 'string') {
middlewareApplied.push(`path:${pathOrHandler}`);
} else {
middlewareApplied.push('global');
}
},
listen: async () => {},
};
const globalMw: Middleware = (_req, _res, next) => { next(); };
server.use(globalMw);
server.use('/api', globalMw);
expect(middlewareApplied).toEqual(['global', 'path:/api']);
});
it('should support optional close method', async () => {
const server: IHttpServer = {
get: () => {},
post: () => {},
put: () => {},
delete: () => {},
patch: () => {},
use: () => {},
listen: async () => {},
close: async () => {},
};
expect(server.close).toBeDefined();
await expect(server.close!()).resolves.toBeUndefined();
});
it('should listen on a port', async () => {
let listenedPort: number | undefined;
const server: IHttpServer = {
get: () => {},
post: () => {},
put: () => {},
delete: () => {},
patch: () => {},
use: () => {},
listen: async (port) => { listenedPort = port; },
};
await server.listen(3000);
expect(listenedPort).toBe(3000);
});
});
});