-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhttp-server.test.ts
More file actions
477 lines (404 loc) · 16.1 KB
/
Copy pathhttp-server.test.ts
File metadata and controls
477 lines (404 loc) · 16.1 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import { describe, it, expect } from 'vitest';
import type {
IHttpRequest,
IHttpResponse,
RouteHandler,
Middleware,
IHttpServer,
HttpResponseObservation,
HttpResponseObserver,
} from './http-server';
import { UNMATCHED_ROUTE_PATTERN } 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();
});
describe('optional setFallbackHandler (#5040 E1)', () => {
/** A server with only the REQUIRED members. */
const baseServer = (): IHttpServer => ({
get: () => {},
post: () => {},
put: () => {},
delete: () => {},
patch: () => {},
use: () => {},
listen: async () => {},
});
it('is optional — an adapter without it still satisfies the contract', () => {
const server = baseServer();
expect(typeof server.setFallbackHandler).toBe('undefined');
expect(typeof server.setFallbackHandler === 'function').toBe(false);
});
it('is feature-detected with typeof === "function" when provided', () => {
const server: IHttpServer = {
...baseServer(),
setFallbackHandler: (_handler) => {},
};
expect(typeof server.setFallbackHandler).toBe('function');
});
it('accepts a RouteHandler — the same handler shape routes take', () => {
let installed: RouteHandler | undefined;
const server: IHttpServer = {
...baseServer(),
setFallbackHandler: (handler) => { installed = handler; },
};
const fallback: RouteHandler = async (_req, res) => {
res.status(404).json({ error: { code: 'ROUTE_NOT_FOUND' } });
};
server.setFallbackHandler!(fallback);
expect(installed).toBe(fallback);
});
it('runs only after every registered route misses', async () => {
const registered = new Set<string>();
let fallback: RouteHandler | undefined;
const server: IHttpServer = {
...baseServer(),
get: (path) => { registered.add(`GET ${path}`); },
setFallbackHandler: (handler) => { fallback = handler; },
};
server.get('/api/v1/data/showcase_task', async (_req, res) => res.json([]));
const answered: string[] = [];
server.setFallbackHandler!(async (req, res) => {
answered.push(`${req.method} ${req.path}`);
res.status(404).json({ error: { code: 'ROUTE_NOT_FOUND' } });
});
const dispatch = async (method: string, path: string) => {
const res: IHttpResponse = {
json: () => {}, send: () => {},
status: function () { return this; },
header: function () { return this; },
};
if (registered.has(`${method} ${path}`)) return 'route';
await fallback!(
{ params: {}, query: {}, headers: {}, method, path, body: { note: 'readable' } },
res,
);
return 'fallback';
};
// A registered route is never shadowed by the fallback.
expect(await dispatch('GET', '/api/v1/data/showcase_task')).toBe('route');
expect(answered).toEqual([]);
// Only the unmatched request reaches it.
expect(await dispatch('GET', '/api/v1/apps/showcase/tasks')).toBe('fallback');
expect(answered).toEqual(['GET /api/v1/apps/showcase/tasks']);
});
it('receives a request whose body is readable (unlike the use() middleware seam)', async () => {
let seenBody: unknown;
const fallback: RouteHandler = async (req, res) => {
seenBody = req.body;
res.status(200).json({ ok: true });
};
const res: IHttpResponse = {
json: () => {}, send: () => {},
status: function () { return this; },
header: function () { return this; },
};
await fallback(
{
params: {},
query: {},
headers: { 'content-type': 'application/json' },
method: 'POST',
path: '/api/v1/apps/showcase/inquiries/purge',
body: { olderThanDays: 30 },
},
res,
);
expect(seenBody).toEqual({ olderThanDays: 30 });
});
it('installing twice replaces the handler — there is one fallback, not a chain', () => {
let current: RouteHandler | undefined;
const server: IHttpServer = {
...baseServer(),
setFallbackHandler: (handler) => { current = handler; },
};
const first: RouteHandler = () => {};
const second: RouteHandler = () => {};
server.setFallbackHandler!(first);
expect(current).toBe(first);
server.setFallbackHandler!(second);
expect(current).toBe(second);
});
});
describe('optional afterResponse (#9835)', () => {
/** A server with only the REQUIRED members. */
const baseServer = (): IHttpServer => ({
get: () => {},
post: () => {},
put: () => {},
delete: () => {},
patch: () => {},
use: () => {},
listen: async () => {},
});
it('is optional — an adapter without it still satisfies the contract', () => {
const server = baseServer();
expect(typeof server.afterResponse).toBe('undefined');
// The documented consequence: this transport reports NO HTTP metrics.
// Zero on `http_requests_total` there means "not instrumented",
// never "no traffic" — a consumer must ask, exactly like this:
expect(typeof server.afterResponse === 'function').toBe(false);
});
it('is feature-detected with typeof === "function" when provided (runtime-real, not type-only)', () => {
const server: IHttpServer = {
...baseServer(),
afterResponse: (_observer) => {},
};
expect(typeof server.afterResponse).toBe('function');
});
it('a delegating wrapper that forwards only required members ERASES detection (#5122 shape)', () => {
const underlying: IHttpServer = {
...baseServer(),
afterResponse: (_observer) => {},
};
// The failure mode the contract warns wrappers against: forward the
// required members, drop the optional ones.
const erasingWrapper: IHttpServer = {
get: underlying.get,
post: underlying.post,
put: underlying.put,
delete: underlying.delete,
patch: underlying.patch,
use: underlying.use,
listen: underlying.listen,
};
// The underlying adapter implements the hook; the wrapper makes the
// contract's own detection idiom read "not instrumented".
expect(typeof underlying.afterResponse === 'function').toBe(true);
expect(typeof erasingWrapper.afterResponse === 'function').toBe(false);
// The compliant wrapper forwards conditionally — present iff wrapped.
const forwardingWrapper: IHttpServer = {
...erasingWrapper,
...(typeof underlying.afterResponse === 'function'
? { afterResponse: underlying.afterResponse.bind(underlying) }
: {}),
};
expect(typeof forwardingWrapper.afterResponse === 'function').toBe(true);
});
it('registration APPENDS — several observers coexist, unlike setFallbackHandler', () => {
const observers: HttpResponseObserver[] = [];
const server: IHttpServer = {
...baseServer(),
afterResponse: (observer) => { observers.push(observer); },
};
const metricsObserver: HttpResponseObserver = () => {};
const accessLogObserver: HttpResponseObserver = () => {};
server.afterResponse!(metricsObserver);
server.afterResponse!(accessLogObserver);
// Both registered, registration order kept — nothing replaced.
expect(observers).toEqual([metricsObserver, accessLogObserver]);
});
it('carries the observation shape: method, routePattern (the PATTERN), status, elapsedMs', () => {
const seen: HttpResponseObservation[] = [];
const observer: HttpResponseObserver = (observation) => { seen.push(observation); };
// What a compliant adapter reports for GET /api/v1/data/rec_42
// answered by the registered route `/api/v1/data/:id`: the PATTERN,
// never the concrete path — the hard requirement the contract states
// so no adapter re-decides cardinality.
observer({
method: 'GET',
routePattern: '/api/v1/data/:id',
status: 200,
elapsedMs: 3,
});
expect(seen).toHaveLength(1);
expect(seen[0].routePattern).toBe('/api/v1/data/:id');
expect(seen[0].routePattern).not.toContain('rec_42');
expect(seen[0].status).toBe(200);
expect(typeof seen[0].elapsedMs).toBe('number');
});
it('reports a request no route matched with the reserved UNMATCHED_ROUTE_PATTERN', () => {
// Reserved in the CONTRACT, not per adapter, so every transport
// reports the same spelling and unrouted traffic is one series.
expect(UNMATCHED_ROUTE_PATTERN).toBe('unmatched');
const seen: HttpResponseObservation[] = [];
const observer: HttpResponseObserver = (observation) => { seen.push(observation); };
observer({
method: 'GET',
routePattern: UNMATCHED_ROUTE_PATTERN,
status: 404,
elapsedMs: 1,
});
expect(seen[0].routePattern).toBe('unmatched');
});
it('a throwing observer must not affect the response or sibling observers — the delivery contract, modelled', () => {
const observers: HttpResponseObserver[] = [];
const server: IHttpServer = {
...baseServer(),
afterResponse: (observer) => { observers.push(observer); },
};
const delivered: string[] = [];
server.afterResponse!(() => { throw new Error('broken metrics backend'); });
server.afterResponse!(() => { delivered.push('access-log'); });
// How a compliant adapter delivers: each observer isolated, failures
// swallowed — a metrics backend must never break a response.
const deliver = (observation: HttpResponseObservation) => {
for (const observer of observers) {
try {
observer(observation);
} catch {
/* observer failures never propagate */
}
}
};
expect(() =>
deliver({ method: 'GET', routePattern: '/x', status: 200, elapsedMs: 0 }),
).not.toThrow();
expect(delivered).toEqual(['access-log']);
});
});
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);
});
});
});