1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const Fastify = require('..')
|
6 |
|
7 | test('Prefix options should add a prefix for all the routes inside a register / 1', t => {
|
8 | t.plan(6)
|
9 | const fastify = Fastify()
|
10 |
|
11 | fastify.get('/first', (req, reply) => {
|
12 | reply.send({ route: '/first' })
|
13 | })
|
14 |
|
15 | fastify.register(function (fastify, opts, next) {
|
16 | fastify.get('/first', (req, reply) => {
|
17 | reply.send({ route: '/v1/first' })
|
18 | })
|
19 |
|
20 | fastify.register(function (fastify, opts, next) {
|
21 | fastify.get('/first', (req, reply) => {
|
22 | reply.send({ route: '/v1/v2/first' })
|
23 | })
|
24 | next()
|
25 | }, { prefix: '/v2' })
|
26 |
|
27 | next()
|
28 | }, { prefix: '/v1' })
|
29 |
|
30 | fastify.inject({
|
31 | method: 'GET',
|
32 | url: '/first'
|
33 | }, (err, res) => {
|
34 | t.error(err)
|
35 | t.same(JSON.parse(res.payload), { route: '/first' })
|
36 | })
|
37 |
|
38 | fastify.inject({
|
39 | method: 'GET',
|
40 | url: '/v1/first'
|
41 | }, (err, res) => {
|
42 | t.error(err)
|
43 | t.same(JSON.parse(res.payload), { route: '/v1/first' })
|
44 | })
|
45 |
|
46 | fastify.inject({
|
47 | method: 'GET',
|
48 | url: '/v1/v2/first'
|
49 | }, (err, res) => {
|
50 | t.error(err)
|
51 | t.same(JSON.parse(res.payload), { route: '/v1/v2/first' })
|
52 | })
|
53 | })
|
54 |
|
55 | test('Prefix options should add a prefix for all the routes inside a register / 2', t => {
|
56 | t.plan(4)
|
57 | const fastify = Fastify()
|
58 |
|
59 | fastify.register(function (fastify, opts, next) {
|
60 | fastify.get('/first', (req, reply) => {
|
61 | reply.send({ route: '/v1/first' })
|
62 | })
|
63 |
|
64 | fastify.get('/second', (req, reply) => {
|
65 | reply.send({ route: '/v1/second' })
|
66 | })
|
67 | next()
|
68 | }, { prefix: '/v1' })
|
69 |
|
70 | fastify.inject({
|
71 | method: 'GET',
|
72 | url: '/v1/first'
|
73 | }, (err, res) => {
|
74 | t.error(err)
|
75 | t.same(JSON.parse(res.payload), { route: '/v1/first' })
|
76 | })
|
77 |
|
78 | fastify.inject({
|
79 | method: 'GET',
|
80 | url: '/v1/second'
|
81 | }, (err, res) => {
|
82 | t.error(err)
|
83 | t.same(JSON.parse(res.payload), { route: '/v1/second' })
|
84 | })
|
85 | })
|
86 |
|
87 | test('Prefix options should add a prefix for all the chained routes inside a register / 3', t => {
|
88 | t.plan(4)
|
89 |
|
90 | const fastify = Fastify()
|
91 |
|
92 | fastify.register(function (fastify, opts, next) {
|
93 | fastify
|
94 | .get('/first', (req, reply) => {
|
95 | reply.send({ route: '/v1/first' })
|
96 | })
|
97 | .get('/second', (req, reply) => {
|
98 | reply.send({ route: '/v1/second' })
|
99 | })
|
100 | next()
|
101 | }, { prefix: '/v1' })
|
102 |
|
103 | fastify.inject({
|
104 | method: 'GET',
|
105 | url: '/v1/first'
|
106 | }, (err, res) => {
|
107 | t.error(err)
|
108 | t.same(JSON.parse(res.payload), { route: '/v1/first' })
|
109 | })
|
110 |
|
111 | fastify.inject({
|
112 | method: 'GET',
|
113 | url: '/v1/second'
|
114 | }, (err, res) => {
|
115 | t.error(err)
|
116 | t.same(JSON.parse(res.payload), { route: '/v1/second' })
|
117 | })
|
118 | })
|
119 |
|
120 | test('Prefix should support parameters as well', t => {
|
121 | t.plan(2)
|
122 | const fastify = Fastify()
|
123 |
|
124 | fastify.register(function (fastify, opts, next) {
|
125 | fastify.get('/hello', (req, reply) => {
|
126 | reply.send({ id: req.params.id })
|
127 | })
|
128 | next()
|
129 | }, { prefix: '/v1/:id' })
|
130 |
|
131 | fastify.inject({
|
132 | method: 'GET',
|
133 | url: '/v1/param/hello'
|
134 | }, (err, res) => {
|
135 | t.error(err)
|
136 | t.same(JSON.parse(res.payload), { id: 'param' })
|
137 | })
|
138 | })
|
139 |
|
140 | test('Prefix should support /', t => {
|
141 | t.plan(2)
|
142 | const fastify = Fastify()
|
143 |
|
144 | fastify.register(function (fastify, opts, next) {
|
145 | fastify.get('/', (req, reply) => {
|
146 | reply.send({ hello: 'world' })
|
147 | })
|
148 | next()
|
149 | }, { prefix: '/v1' })
|
150 |
|
151 | fastify.inject({
|
152 | method: 'GET',
|
153 | url: '/v1'
|
154 | }, (err, res) => {
|
155 | t.error(err)
|
156 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
157 | })
|
158 | })
|
159 |
|
160 | test('Prefix without /', t => {
|
161 | t.plan(2)
|
162 | const fastify = Fastify()
|
163 |
|
164 | fastify.register(function (fastify, opts, next) {
|
165 | fastify.get('/', (req, reply) => {
|
166 | reply.send({ hello: 'world' })
|
167 | })
|
168 | next()
|
169 | }, { prefix: 'v1' })
|
170 |
|
171 | fastify.inject({
|
172 | method: 'GET',
|
173 | url: '/v1'
|
174 | }, (err, res) => {
|
175 | t.error(err)
|
176 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
177 | })
|
178 | })
|
179 |
|
180 | test('Prefix with trailing /', t => {
|
181 | t.plan(6)
|
182 | const fastify = Fastify()
|
183 |
|
184 | fastify.register(function (fastify, opts, next) {
|
185 | fastify.get('/route1', (req, reply) => {
|
186 | reply.send({ hello: 'world1' })
|
187 | })
|
188 | fastify.get('route2', (req, reply) => {
|
189 | reply.send({ hello: 'world2' })
|
190 | })
|
191 |
|
192 | fastify.register(function (fastify, opts, next) {
|
193 | fastify.get('/route3', (req, reply) => {
|
194 | reply.send({ hello: 'world3' })
|
195 | })
|
196 | next()
|
197 | }, { prefix: '/inner/' })
|
198 |
|
199 | next()
|
200 | }, { prefix: '/v1/' })
|
201 |
|
202 | fastify.inject({
|
203 | method: 'GET',
|
204 | url: '/v1/route1'
|
205 | }, (err, res) => {
|
206 | t.error(err)
|
207 | t.same(JSON.parse(res.payload), { hello: 'world1' })
|
208 | })
|
209 |
|
210 | fastify.inject({
|
211 | method: 'GET',
|
212 | url: '/v1/route2'
|
213 | }, (err, res) => {
|
214 | t.error(err)
|
215 | t.same(JSON.parse(res.payload), { hello: 'world2' })
|
216 | })
|
217 |
|
218 | fastify.inject({
|
219 | method: 'GET',
|
220 | url: '/v1/inner/route3'
|
221 | }, (err, res) => {
|
222 | t.error(err)
|
223 | t.same(JSON.parse(res.payload), { hello: 'world3' })
|
224 | })
|
225 | })
|
226 |
|
227 | test('Prefix works multiple levels deep', t => {
|
228 | t.plan(2)
|
229 | const fastify = Fastify()
|
230 |
|
231 | fastify.register(function (fastify, opts, next) {
|
232 | fastify.register(function (fastify, opts, next) {
|
233 | fastify.register(function (fastify, opts, next) {
|
234 | fastify.register(function (fastify, opts, next) {
|
235 | fastify.get('/', (req, reply) => {
|
236 | reply.send({ hello: 'world' })
|
237 | })
|
238 | next()
|
239 | }, { prefix: '/v3' })
|
240 | next()
|
241 | })
|
242 | next()
|
243 | }, { prefix: 'v2' })
|
244 | next()
|
245 | }, { prefix: '/v1' })
|
246 |
|
247 | fastify.inject({
|
248 | method: 'GET',
|
249 | url: '/v1/v2/v3'
|
250 | }, (err, res) => {
|
251 | t.error(err)
|
252 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
253 | })
|
254 | })
|
255 |
|
256 | test('Different register - encapsulation check', t => {
|
257 | t.plan(4)
|
258 | const fastify = Fastify()
|
259 |
|
260 | fastify.get('/first', (req, reply) => {
|
261 | reply.send({ route: '/first' })
|
262 | })
|
263 |
|
264 | fastify.register(function (instance, opts, next) {
|
265 | instance.register(function (f, opts, next) {
|
266 | f.get('/', (req, reply) => {
|
267 | reply.send({ route: '/v1/v2' })
|
268 | })
|
269 | next()
|
270 | }, { prefix: '/v2' })
|
271 | next()
|
272 | }, { prefix: '/v1' })
|
273 |
|
274 | fastify.register(function (instance, opts, next) {
|
275 | instance.register(function (f, opts, next) {
|
276 | f.get('/', (req, reply) => {
|
277 | reply.send({ route: '/v3/v4' })
|
278 | })
|
279 | next()
|
280 | }, { prefix: '/v4' })
|
281 | next()
|
282 | }, { prefix: '/v3' })
|
283 |
|
284 | fastify.inject({
|
285 | method: 'GET',
|
286 | url: '/v1/v2'
|
287 | }, (err, res) => {
|
288 | t.error(err)
|
289 | t.same(JSON.parse(res.payload), { route: '/v1/v2' })
|
290 | })
|
291 |
|
292 | fastify.inject({
|
293 | method: 'GET',
|
294 | url: '/v3/v4'
|
295 | }, (err, res) => {
|
296 | t.error(err)
|
297 | t.same(JSON.parse(res.payload), { route: '/v3/v4' })
|
298 | })
|
299 | })
|
300 |
|
301 | test('Can retrieve prefix within encapsulated instances', t => {
|
302 | t.plan(4)
|
303 | const fastify = Fastify()
|
304 |
|
305 | fastify.register(function (instance, opts, next) {
|
306 | instance.get('/one', function (req, reply) {
|
307 | reply.send(instance.prefix)
|
308 | })
|
309 |
|
310 | instance.register(function (instance, opts, next) {
|
311 | instance.get('/two', function (req, reply) {
|
312 | reply.send(instance.prefix)
|
313 | })
|
314 | next()
|
315 | }, { prefix: '/v2' })
|
316 |
|
317 | next()
|
318 | }, { prefix: '/v1' })
|
319 |
|
320 | fastify.inject({
|
321 | method: 'GET',
|
322 | url: '/v1/one'
|
323 | }, (err, res) => {
|
324 | t.error(err)
|
325 | t.is(res.payload, '/v1')
|
326 | })
|
327 |
|
328 | fastify.inject({
|
329 | method: 'GET',
|
330 | url: '/v1/v2/two'
|
331 | }, (err, res) => {
|
332 | t.error(err)
|
333 | t.is(res.payload, '/v1/v2')
|
334 | })
|
335 | })
|
336 |
|
337 | test('matches both /prefix and /prefix/ with a / route', t => {
|
338 | t.plan(4)
|
339 | const fastify = Fastify()
|
340 |
|
341 | fastify.register(function (fastify, opts, next) {
|
342 | fastify.get('/', (req, reply) => {
|
343 | reply.send({ hello: 'world' })
|
344 | })
|
345 |
|
346 | next()
|
347 | }, { prefix: '/prefix' })
|
348 |
|
349 | fastify.inject({
|
350 | method: 'GET',
|
351 | url: '/prefix'
|
352 | }, (err, res) => {
|
353 | t.error(err)
|
354 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
355 | })
|
356 |
|
357 | fastify.inject({
|
358 | method: 'GET',
|
359 | url: '/prefix/'
|
360 | }, (err, res) => {
|
361 | t.error(err)
|
362 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
363 | })
|
364 | })
|
365 |
|
366 | test('prefix "/prefix/" does not match "/prefix" with a / route', t => {
|
367 | t.plan(4)
|
368 | const fastify = Fastify()
|
369 |
|
370 | fastify.register(function (fastify, opts, next) {
|
371 | fastify.get('/', (req, reply) => {
|
372 | reply.send({ hello: 'world' })
|
373 | })
|
374 |
|
375 | next()
|
376 | }, { prefix: '/prefix/' })
|
377 |
|
378 | fastify.inject({
|
379 | method: 'GET',
|
380 | url: '/prefix'
|
381 | }, (err, res) => {
|
382 | t.error(err)
|
383 | t.equal(res.statusCode, 404)
|
384 | })
|
385 |
|
386 | fastify.inject({
|
387 | method: 'GET',
|
388 | url: '/prefix/'
|
389 | }, (err, res) => {
|
390 | t.error(err)
|
391 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
392 | })
|
393 | })
|
394 |
|
395 | test('matches both /prefix and /prefix/ with a / route - ignoreTrailingSlash: true', t => {
|
396 | t.plan(4)
|
397 | const fastify = Fastify({
|
398 | ignoreTrailingSlash: true
|
399 | })
|
400 |
|
401 | fastify.register(function (fastify, opts, next) {
|
402 | fastify.get('/', (req, reply) => {
|
403 | reply.send({ hello: 'world' })
|
404 | })
|
405 |
|
406 | next()
|
407 | }, { prefix: '/prefix' })
|
408 |
|
409 | fastify.inject({
|
410 | method: 'GET',
|
411 | url: '/prefix'
|
412 | }, (err, res) => {
|
413 | t.error(err)
|
414 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
415 | })
|
416 |
|
417 | fastify.inject({
|
418 | method: 'GET',
|
419 | url: '/prefix/'
|
420 | }, (err, res) => {
|
421 | t.error(err)
|
422 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
423 | })
|
424 | })
|
425 |
|
426 | test('matches both /prefix and /prefix/ with a / route - prefixTrailingSlash: "both", ignoreTrailingSlash: false', t => {
|
427 | t.plan(4)
|
428 | const fastify = Fastify({
|
429 | ignoreTrailingSlash: false
|
430 | })
|
431 |
|
432 | fastify.register(function (fastify, opts, next) {
|
433 | fastify.route({
|
434 | method: 'GET',
|
435 | url: '/',
|
436 | prefixTrailingSlash: 'both',
|
437 | handler: (req, reply) => {
|
438 | reply.send({ hello: 'world' })
|
439 | }
|
440 | })
|
441 |
|
442 | next()
|
443 | }, { prefix: '/prefix' })
|
444 |
|
445 | fastify.inject({
|
446 | method: 'GET',
|
447 | url: '/prefix'
|
448 | }, (err, res) => {
|
449 | t.error(err)
|
450 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
451 | })
|
452 |
|
453 | fastify.inject({
|
454 | method: 'GET',
|
455 | url: '/prefix/'
|
456 | }, (err, res) => {
|
457 | t.error(err)
|
458 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
459 | })
|
460 | })
|
461 |
|
462 | test('returns 404 status code with /prefix/ and / route - prefixTrailingSlash: "both" (default), ignoreTrailingSlash: true', t => {
|
463 | t.plan(2)
|
464 | const fastify = Fastify({
|
465 | ignoreTrailingSlash: true
|
466 | })
|
467 |
|
468 | fastify.register(function (fastify, opts, next) {
|
469 | fastify.route({
|
470 | method: 'GET',
|
471 | url: '/',
|
472 | handler: (req, reply) => {
|
473 | reply.send({ hello: 'world' })
|
474 | }
|
475 | })
|
476 |
|
477 | next()
|
478 | }, { prefix: '/prefix/' })
|
479 |
|
480 | fastify.inject({
|
481 | method: 'GET',
|
482 | url: '/prefix//'
|
483 | }, (err, res) => {
|
484 | t.error(err)
|
485 | t.same(JSON.parse(res.payload), {
|
486 | error: 'Not Found',
|
487 | message: 'Route GET:/prefix// not found',
|
488 | statusCode: 404
|
489 | })
|
490 | })
|
491 | })
|
492 |
|
493 | test('matches only /prefix with a / route - prefixTrailingSlash: "no-slash", ignoreTrailingSlash: false', t => {
|
494 | t.plan(4)
|
495 | const fastify = Fastify({
|
496 | ignoreTrailingSlash: false
|
497 | })
|
498 |
|
499 | fastify.register(function (fastify, opts, next) {
|
500 | fastify.route({
|
501 | method: 'GET',
|
502 | url: '/',
|
503 | prefixTrailingSlash: 'no-slash',
|
504 | handler: (req, reply) => {
|
505 | reply.send({ hello: 'world' })
|
506 | }
|
507 | })
|
508 |
|
509 | next()
|
510 | }, { prefix: '/prefix' })
|
511 |
|
512 | fastify.inject({
|
513 | method: 'GET',
|
514 | url: '/prefix'
|
515 | }, (err, res) => {
|
516 | t.error(err)
|
517 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
518 | })
|
519 |
|
520 | fastify.inject({
|
521 | method: 'GET',
|
522 | url: '/prefix/'
|
523 | }, (err, res) => {
|
524 | t.error(err)
|
525 | t.equal(JSON.parse(res.payload).statusCode, 404)
|
526 | })
|
527 | })
|
528 |
|
529 | test('matches only /prefix/ with a / route - prefixTrailingSlash: "slash", ignoreTrailingSlash: false', t => {
|
530 | t.plan(4)
|
531 | const fastify = Fastify({
|
532 | ignoreTrailingSlash: false
|
533 | })
|
534 |
|
535 | fastify.register(function (fastify, opts, next) {
|
536 | fastify.route({
|
537 | method: 'GET',
|
538 | url: '/',
|
539 | prefixTrailingSlash: 'slash',
|
540 | handler: (req, reply) => {
|
541 | reply.send({ hello: 'world' })
|
542 | }
|
543 | })
|
544 |
|
545 | next()
|
546 | }, { prefix: '/prefix' })
|
547 |
|
548 | fastify.inject({
|
549 | method: 'GET',
|
550 | url: '/prefix/'
|
551 | }, (err, res) => {
|
552 | t.error(err)
|
553 | t.same(JSON.parse(res.payload), { hello: 'world' })
|
554 | })
|
555 |
|
556 | fastify.inject({
|
557 | method: 'GET',
|
558 | url: '/prefix'
|
559 | }, (err, res) => {
|
560 | t.error(err)
|
561 | t.equal(JSON.parse(res.payload).statusCode, 404)
|
562 | })
|
563 | })
|