1 | 'use strict'
|
2 |
|
3 | const test = require('tap').test
|
4 | const sget = require('simple-get')
|
5 | const Fastify = require('../')
|
6 | const {
|
7 | codes: {
|
8 | FST_ERR_BAD_URL
|
9 | }
|
10 | } = require('../lib/errors')
|
11 |
|
12 | test('Should honor ignoreTrailingSlash option', t => {
|
13 | t.plan(4)
|
14 | const fastify = Fastify({
|
15 | ignoreTrailingSlash: true
|
16 | })
|
17 |
|
18 | fastify.get('/test', (req, res) => {
|
19 | res.send('test')
|
20 | })
|
21 |
|
22 | fastify.listen(0, (err) => {
|
23 | fastify.server.unref()
|
24 | if (err) t.threw(err)
|
25 |
|
26 | const baseUrl = 'http://127.0.0.1:' + fastify.server.address().port
|
27 |
|
28 | sget.concat(baseUrl + '/test', (err, res, data) => {
|
29 | if (err) t.threw(err)
|
30 | t.is(res.statusCode, 200)
|
31 | t.is(data.toString(), 'test')
|
32 | })
|
33 |
|
34 | sget.concat(baseUrl + '/test/', (err, res, data) => {
|
35 | if (err) t.threw(err)
|
36 | t.is(res.statusCode, 200)
|
37 | t.is(data.toString(), 'test')
|
38 | })
|
39 | })
|
40 | })
|
41 |
|
42 | test('Should honor maxParamLength option', t => {
|
43 | t.plan(4)
|
44 | const fastify = Fastify({ maxParamLength: 10 })
|
45 |
|
46 | fastify.get('/test/:id', (req, reply) => {
|
47 | reply.send({ hello: 'world' })
|
48 | })
|
49 |
|
50 | fastify.inject({
|
51 | method: 'GET',
|
52 | url: '/test/123456789'
|
53 | }, (error, res) => {
|
54 | t.error(error)
|
55 | t.strictEqual(res.statusCode, 200)
|
56 | })
|
57 |
|
58 | fastify.inject({
|
59 | method: 'GET',
|
60 | url: '/test/123456789abcd'
|
61 | }, (error, res) => {
|
62 | t.error(error)
|
63 | t.strictEqual(res.statusCode, 404)
|
64 | })
|
65 | })
|
66 |
|
67 | test('Should honor frameworkErrors option', t => {
|
68 | t.plan(3)
|
69 | const fastify = Fastify({
|
70 | frameworkErrors: function (err, req, res) {
|
71 | if (err instanceof FST_ERR_BAD_URL) {
|
72 | t.ok(true)
|
73 | } else {
|
74 | t.fail()
|
75 | }
|
76 | res.send(err.message)
|
77 | }
|
78 | })
|
79 |
|
80 | fastify.get('/test/:id', (req, res) => {
|
81 | res.send('{ hello: \'world\' }')
|
82 | })
|
83 |
|
84 | fastify.inject(
|
85 | {
|
86 | method: 'GET',
|
87 | url: '/test/%world'
|
88 | },
|
89 | (err, res) => {
|
90 | t.error(err)
|
91 | t.equals(res.body, 'FST_ERR_BAD_URL: \'%world\' is not a valid url component')
|
92 | }
|
93 | )
|
94 | })
|