-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
http.spec.ts
121 lines (117 loc) · 4.08 KB
/
http.spec.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
import { INestApplication } from '@nestjs/common';
import { ExpressAdapter } from '@nestjs/platform-express';
import { FastifyAdapter } from '@nestjs/platform-fastify';
import { OgmaFilterService, OgmaInterceptor, OgmaService } from '@ogma/nestjs-module';
import { ExpressParser } from '@ogma/platform-express';
import { FastifyParser } from '@ogma/platform-fastify';
import { style } from '@ogma/styler';
import { Stub, stubMethod } from 'hanbi';
import { request, spec } from 'pactum';
import { suite } from 'uvu';
import { is } from 'uvu/assert';
import { HttpServerModule } from '../src/http/http-server.module';
import {
createTestModule,
hello,
reportValues,
serviceOptionsFactory,
toBeALogObject,
} from './utils';
const expectRequestId = (spy: Stub<OgmaInterceptor['log']> | Stub<OgmaFilterService['doLog']>) => {
is(typeof spy.firstCall.args[2], 'string');
is(spy.firstCall.args[2].length, 16);
};
const expectLogObject = (
spy: Stub<OgmaInterceptor['log']> | Stub<OgmaFilterService['doLog']>,
method: string,
endpoint: string,
status: string,
) => {
toBeALogObject(spy.firstCall.args[0], method, endpoint, 'HTTP/1.1', status);
is(spy.callCount, 1);
};
for (const { adapter, server, parser } of [
{
adapter: new ExpressAdapter(),
server: 'Express',
parser: ExpressParser,
},
{
adapter: new FastifyAdapter(),
server: 'Fastify',
parser: FastifyParser,
},
]) {
const HttpSuite = suite<{
app: INestApplication;
logSpy: Stub<OgmaInterceptor['log']>;
logs: Parameters<OgmaInterceptor['log'] | OgmaFilterService['doLog']>[];
filterSpy: Stub<OgmaFilterService['doLog']>;
}>(`${server} HTTP Log Suite`, {
app: undefined,
logSpy: undefined,
logs: [],
filterSpy: undefined,
});
HttpSuite.before(async (context) => {
const modRef = await createTestModule(HttpServerModule, serviceOptionsFactory(server), [
parser,
]);
context.app = modRef.createNestApplication(adapter);
const interceptor = context.app.get(OgmaInterceptor);
const filterService = context.app.get(OgmaFilterService);
await context.app.listen(0);
request.setBaseUrl((await context.app.getUrl()).replace('[::1]', 'localhost'));
context.logSpy = stubMethod(interceptor, 'log');
context.filterSpy = stubMethod(filterService as any, 'doLog');
});
HttpSuite.after(async ({ app, logs }) => {
const ogma = app.get(OgmaService);
await app.close();
reportValues(ogma, logs);
});
HttpSuite.after.each(({ logSpy, logs, filterSpy }) => {
logSpy.firstCall && logs.push(logSpy.firstCall.args);
logSpy.reset();
filterSpy.firstCall && logs.push(filterSpy.firstCall.args);
filterSpy.reset();
});
for (const { method, status } of [
{
method: 'GET',
status: 200,
},
{
method: 'POST',
status: 201,
},
{ method: 'PATCH', status: 200 },
{ method: 'PUT', status: 200 },
{ method: 'DELETE', status: 200 },
]) {
HttpSuite(`should log ${status} from ${method} request`, async ({ logSpy }) => {
await spec()[method.toLowerCase()]('/').expectStatus(status).expectBody(hello);
is(logSpy.callCount, 1);
expectLogObject(logSpy, method, '/', style.green.apply(status));
});
}
HttpSuite('it should log a 202 instead of 200', async ({ logSpy }) => {
await spec().get('/status').expectBody(hello).expectStatus(202);
expectLogObject(logSpy, 'GET', '/status', style.green.apply(202));
expectRequestId(logSpy);
});
HttpSuite('It should log a 400', async ({ logSpy }) => {
await spec().get('/error').expectStatus(400);
expectLogObject(logSpy, 'GET', '/error', style.yellow.apply(400));
expectRequestId(logSpy);
});
HttpSuite('It should skip the log but return data', async ({ logSpy }) => {
await spec().get('/skip').expectBody(hello).expectStatus(200);
is(logSpy.callCount, 0);
});
HttpSuite('It should get caught by a guard but still be able to log', async ({ filterSpy }) => {
await spec().get('/fail-guard').expectStatus(403);
expectLogObject(filterSpy, 'GET', '/fail-guard', style.yellow.apply(403));
});
HttpSuite.run();
}