-
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathapiIntroduction.int.spec.ts
More file actions
101 lines (85 loc) · 3 KB
/
apiIntroduction.int.spec.ts
File metadata and controls
101 lines (85 loc) · 3 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
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { SwaggerUiProvider } from '@inversifyjs/http-open-api';
import { OpenApiValidationPipe } from '@inversifyjs/http-openapi-validation';
import { InversifyValidationErrorFilter } from '@inversifyjs/http-validation';
import { type OpenApi3Dot1Object } from '@inversifyjs/open-api-types/v3Dot1';
import { Container } from 'inversify';
import { buildExpressServer } from '../../../server/adapter/express/actions/buildExpressServer.js';
import { type Server } from '../../../server/models/Server.js';
import { UserController } from './apiIntroduction.js';
describe('API Introduction (OpenAPI Validation)', () => {
describe('having an OpenApiValidationPipe in an HTTP server with Quick Start user validation', () => {
let server: Server;
beforeAll(async () => {
const container: Container = new Container();
const openApiObject: OpenApi3Dot1Object = {
info: { title: 'My API', version: '1.0.0' },
openapi: '3.1.1',
};
const swaggerProvider: SwaggerUiProvider = new SwaggerUiProvider({
api: {
openApiObject,
path: '/docs',
},
});
container
.bind(InversifyValidationErrorFilter)
.toSelf()
.inSingletonScope();
container.bind(UserController).toSelf().inSingletonScope();
swaggerProvider.provide(container);
server = await buildExpressServer(
container,
[InversifyValidationErrorFilter],
[new OpenApiValidationPipe(swaggerProvider.openApiObject)],
);
});
afterAll(async () => {
await server.shutdown();
});
describe('when a valid POST /users request is made', () => {
let response: Response;
beforeAll(async () => {
response = await fetch(
`http://${server.host}:${server.port.toString()}/users`,
{
body: JSON.stringify({
email: 'jane.doe@example.com',
name: 'Jane Doe',
}),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
},
);
});
it('should return expected Response', async () => {
expect(response.status).toBe(200);
await expect(response.text()).resolves.toBe('Created user: Jane Doe');
});
});
describe('when an invalid POST /users request is made', () => {
let response: Response;
beforeAll(async () => {
response = await fetch(
`http://${server.host}:${server.port.toString()}/users`,
{
body: JSON.stringify({
email: 'jane.doe@example.com',
extra: 'not allowed',
name: 'Jane Doe',
}),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
},
);
});
it('should return expected Response', async () => {
expect(response.status).toBe(400);
});
});
});
});