Skip to content

Commit 7c394b6

Browse files
fix(platform-fastify): fix versioning (media type and header)
1 parent 2405b66 commit 7c394b6

8 files changed

Lines changed: 1026 additions & 11 deletions

File tree

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
import { INestApplication, VersioningType } from '@nestjs/common';
2+
import {
3+
FastifyAdapter,
4+
NestFastifyApplication,
5+
} from '@nestjs/platform-fastify';
6+
import { Test } from '@nestjs/testing';
7+
import * as request from 'supertest';
8+
import { AppModule } from '../src/app.module';
9+
10+
describe('Versioning (fastify)', () => {
11+
let app: INestApplication;
12+
13+
before(async () => {
14+
const moduleRef = await Test.createTestingModule({
15+
imports: [AppModule],
16+
}).compile();
17+
18+
app = moduleRef.createNestApplication<NestFastifyApplication>(
19+
new FastifyAdapter(),
20+
);
21+
app.enableVersioning({
22+
type: VersioningType.HEADER,
23+
header: 'X-API-Version',
24+
});
25+
await app.init();
26+
await app.getHttpAdapter().getInstance().ready();
27+
});
28+
29+
describe('GET /', () => {
30+
it('V1', () => {
31+
return request(app.getHttpServer())
32+
.get('/')
33+
.set({
34+
'X-API-Version': '1',
35+
})
36+
.expect(200)
37+
.expect('Hello World V1!');
38+
});
39+
40+
it('V2', () => {
41+
return request(app.getHttpServer())
42+
.get('/')
43+
.set({
44+
'X-API-Version': '2',
45+
})
46+
.expect(200)
47+
.expect('Hello World V2!');
48+
});
49+
50+
it('V3', () => {
51+
return request(app.getHttpServer())
52+
.get('/')
53+
.set({
54+
'X-API-Version': '3',
55+
})
56+
.expect(404);
57+
});
58+
59+
it('No Version', () => {
60+
return request(app.getHttpServer())
61+
.get('/')
62+
.set({
63+
'X-API-Version': '',
64+
})
65+
.expect(404);
66+
});
67+
68+
it('No Header', () => {
69+
return request(app.getHttpServer()).get('/').expect(404);
70+
});
71+
});
72+
73+
describe('GET /:param', () => {
74+
it('V1', () => {
75+
return request(app.getHttpServer())
76+
.get('/param/hello')
77+
.set({
78+
'X-API-Version': '1',
79+
})
80+
.expect(200)
81+
.expect('Parameter V1!');
82+
});
83+
84+
it('V2', () => {
85+
return request(app.getHttpServer())
86+
.get('/param/hello')
87+
.set({
88+
'X-API-Version': '2',
89+
})
90+
.expect(200)
91+
.expect('Parameter V2!');
92+
});
93+
94+
it('V3', () => {
95+
return request(app.getHttpServer())
96+
.get('/param/hello')
97+
.set({
98+
'X-API-Version': '3',
99+
})
100+
.expect(404);
101+
});
102+
103+
it('No Version', () => {
104+
return request(app.getHttpServer())
105+
.get('/param/hello')
106+
.set({
107+
'X-API-Version': '',
108+
})
109+
.expect(404);
110+
});
111+
112+
it('No Header', () => {
113+
return request(app.getHttpServer()).get('/').expect(404);
114+
});
115+
});
116+
117+
describe('GET /multiple', () => {
118+
it('V1', () => {
119+
return request(app.getHttpServer())
120+
.get('/multiple')
121+
.set({
122+
'X-API-Version': '1',
123+
})
124+
.expect(200)
125+
.expect('Multiple Versions 1 or 2');
126+
});
127+
128+
it('V2', () => {
129+
return request(app.getHttpServer())
130+
.get('/multiple')
131+
.set({
132+
'X-API-Version': '2',
133+
})
134+
.expect(200)
135+
.expect('Multiple Versions 1 or 2');
136+
});
137+
138+
it('V3', () => {
139+
return request(app.getHttpServer())
140+
.get('/multiple')
141+
.set({
142+
'X-API-Version': '3',
143+
})
144+
.expect(404);
145+
});
146+
147+
it('No Version', () => {
148+
return request(app.getHttpServer())
149+
.get('/multiple')
150+
.set({
151+
'X-API-Version': '',
152+
})
153+
.expect(404);
154+
});
155+
156+
it('No Header', () => {
157+
return request(app.getHttpServer()).get('/multiple').expect(404);
158+
});
159+
});
160+
161+
describe('GET /neutral', () => {
162+
it('V1', () => {
163+
return request(app.getHttpServer())
164+
.get('/neutral')
165+
.set({
166+
'X-API-Version': '1',
167+
})
168+
.expect(200)
169+
.expect('Neutral');
170+
});
171+
172+
it('V2', () => {
173+
return request(app.getHttpServer())
174+
.get('/neutral')
175+
.set({
176+
'X-API-Version': '2',
177+
})
178+
.expect(200)
179+
.expect('Neutral');
180+
});
181+
182+
it('No Version', () => {
183+
return request(app.getHttpServer())
184+
.get('/neutral')
185+
.set({
186+
'X-API-Version': '',
187+
})
188+
.expect(200)
189+
.expect('Neutral');
190+
});
191+
192+
it('No Header', () => {
193+
return request(app.getHttpServer())
194+
.get('/neutral')
195+
.expect(200)
196+
.expect('Neutral');
197+
});
198+
});
199+
200+
describe('GET /override', () => {
201+
it('V1', () => {
202+
return request(app.getHttpServer())
203+
.get('/override')
204+
.set({
205+
'X-API-Version': '1',
206+
})
207+
.expect(200)
208+
.expect('Override Version 1');
209+
});
210+
211+
it('V2', () => {
212+
return request(app.getHttpServer())
213+
.get('/override')
214+
.set({
215+
'X-API-Version': '2',
216+
})
217+
.expect(200)
218+
.expect('Override Version 2');
219+
});
220+
221+
it('V3', () => {
222+
return request(app.getHttpServer())
223+
.get('/override')
224+
.set({
225+
'X-API-Version': '3',
226+
})
227+
.expect(404);
228+
});
229+
230+
it('No Version', () => {
231+
return request(app.getHttpServer())
232+
.get('/override')
233+
.set({
234+
'X-API-Version': '',
235+
})
236+
.expect(404);
237+
});
238+
239+
it('No Header', () => {
240+
return request(app.getHttpServer()).get('/override').expect(404);
241+
});
242+
});
243+
244+
describe('GET /override-partial', () => {
245+
it('V1', () => {
246+
return request(app.getHttpServer())
247+
.get('/override-partial')
248+
.set({
249+
'X-API-Version': '1',
250+
})
251+
.expect(200)
252+
.expect('Override Partial Version 1');
253+
});
254+
255+
it('V2', () => {
256+
return request(app.getHttpServer())
257+
.get('/override-partial')
258+
.set({
259+
'X-API-Version': '2',
260+
})
261+
.expect(200)
262+
.expect('Override Partial Version 2');
263+
});
264+
265+
it('V3', () => {
266+
return request(app.getHttpServer())
267+
.get('/override-partial')
268+
.set({
269+
'X-API-Version': '3',
270+
})
271+
.expect(404);
272+
});
273+
274+
it('No Version', () => {
275+
return request(app.getHttpServer())
276+
.get('/override-partial')
277+
.set({
278+
'X-API-Version': '',
279+
})
280+
.expect(404);
281+
});
282+
283+
it('No Header', () => {
284+
return request(app.getHttpServer()).get('/override-partial').expect(404);
285+
});
286+
});
287+
288+
after(async () => {
289+
await app.close();
290+
});
291+
});

0 commit comments

Comments
 (0)