1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const sget = require('simple-get').concat
|
6 | const joi = require('joi')
|
7 | const Fastify = require('..')
|
8 |
|
9 | test('route', t => {
|
10 | t.plan(10)
|
11 | const test = t.test
|
12 | const fastify = Fastify()
|
13 |
|
14 | test('route - get', t => {
|
15 | t.plan(1)
|
16 | try {
|
17 | fastify.route({
|
18 | method: 'GET',
|
19 | url: '/',
|
20 | schema: {
|
21 | response: {
|
22 | '2xx': {
|
23 | type: 'object',
|
24 | properties: {
|
25 | hello: {
|
26 | type: 'string'
|
27 | }
|
28 | }
|
29 | }
|
30 | }
|
31 | },
|
32 | handler: function (req, reply) {
|
33 | reply.send({ hello: 'world' })
|
34 | }
|
35 | })
|
36 | t.pass()
|
37 | } catch (e) {
|
38 | t.fail()
|
39 | }
|
40 | })
|
41 |
|
42 | test('missing schema - route', t => {
|
43 | t.plan(1)
|
44 | try {
|
45 | fastify.route({
|
46 | method: 'GET',
|
47 | url: '/missing',
|
48 | handler: function (req, reply) {
|
49 | reply.send({ hello: 'world' })
|
50 | }
|
51 | })
|
52 | t.pass()
|
53 | } catch (e) {
|
54 | t.fail()
|
55 | }
|
56 | })
|
57 |
|
58 | test('invalid handler attribute - route', t => {
|
59 | t.plan(1)
|
60 | try {
|
61 | fastify.get('/', { handler: 'not a function' }, () => {})
|
62 | t.fail()
|
63 | } catch (e) {
|
64 | t.pass()
|
65 | }
|
66 | })
|
67 |
|
68 | test('invalid schema - route', t => {
|
69 | t.plan(1)
|
70 | try {
|
71 | fastify.route({
|
72 | method: 'GET',
|
73 | url: '/invalid',
|
74 | schema: {
|
75 | querystring: {
|
76 | id: 'string'
|
77 | }
|
78 | },
|
79 | handler: function (req, reply) {
|
80 | reply.send({ hello: 'world' })
|
81 | }
|
82 | })
|
83 | fastify.after(err => {
|
84 | t.ok(err instanceof Error)
|
85 | })
|
86 | } catch (e) {
|
87 | t.fail()
|
88 | }
|
89 | })
|
90 |
|
91 | test('Multiple methods', t => {
|
92 | t.plan(1)
|
93 | try {
|
94 | fastify.route({
|
95 | method: ['GET', 'DELETE'],
|
96 | url: '/multiple',
|
97 | handler: function (req, reply) {
|
98 | reply.send({ hello: 'world' })
|
99 | }
|
100 | })
|
101 | t.pass()
|
102 | } catch (e) {
|
103 | t.fail()
|
104 | }
|
105 | })
|
106 |
|
107 | test('Add multiple methods', t => {
|
108 | t.plan(1)
|
109 | try {
|
110 | fastify.get('/add-multiple', function (req, reply) {
|
111 | reply.send({ hello: 'Bob!' })
|
112 | })
|
113 | fastify.route({
|
114 | method: ['PUT', 'DELETE'],
|
115 | url: '/add-multiple',
|
116 | handler: function (req, reply) {
|
117 | reply.send({ hello: 'world' })
|
118 | }
|
119 | })
|
120 | t.pass()
|
121 | } catch (e) {
|
122 | t.fail()
|
123 | }
|
124 | })
|
125 |
|
126 | fastify.listen(0, function (err) {
|
127 | if (err) t.error(err)
|
128 | fastify.server.unref()
|
129 |
|
130 | test('cannot add another route after binding', t => {
|
131 | t.plan(1)
|
132 | try {
|
133 | fastify.route({
|
134 | method: 'GET',
|
135 | url: '/another-get-route',
|
136 | handler: function (req, reply) {
|
137 | reply.send({ hello: 'world' })
|
138 | }
|
139 | })
|
140 | t.fail()
|
141 | } catch (e) {
|
142 | t.pass()
|
143 | }
|
144 | })
|
145 |
|
146 | test('route - get', t => {
|
147 | t.plan(3)
|
148 | sget({
|
149 | method: 'GET',
|
150 | url: 'http://localhost:' + fastify.server.address().port
|
151 | }, (err, response, body) => {
|
152 | t.error(err)
|
153 | t.strictEqual(response.statusCode, 200)
|
154 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
155 | })
|
156 | })
|
157 |
|
158 | test('route - missing schema', t => {
|
159 | t.plan(3)
|
160 | sget({
|
161 | method: 'GET',
|
162 | url: 'http://localhost:' + fastify.server.address().port + '/missing'
|
163 | }, (err, response, body) => {
|
164 | t.error(err)
|
165 | t.strictEqual(response.statusCode, 200)
|
166 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
167 | })
|
168 | })
|
169 |
|
170 | test('route - multiple methods', t => {
|
171 | t.plan(6)
|
172 | sget({
|
173 | method: 'GET',
|
174 | url: 'http://localhost:' + fastify.server.address().port + '/multiple'
|
175 | }, (err, response, body) => {
|
176 | t.error(err)
|
177 | t.strictEqual(response.statusCode, 200)
|
178 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
179 | })
|
180 |
|
181 | sget({
|
182 | method: 'DELETE',
|
183 | url: 'http://localhost:' + fastify.server.address().port + '/multiple'
|
184 | }, (err, response, body) => {
|
185 | t.error(err)
|
186 | t.strictEqual(response.statusCode, 200)
|
187 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
188 | })
|
189 | })
|
190 | })
|
191 | })
|
192 |
|
193 | test('path can be specified in place of uri', t => {
|
194 | t.plan(3)
|
195 | const fastify = Fastify()
|
196 |
|
197 | fastify.route({
|
198 | method: 'GET',
|
199 | path: '/path',
|
200 | handler: function (req, reply) {
|
201 | reply.send({ hello: 'world' })
|
202 | }
|
203 | })
|
204 |
|
205 | const reqOpts = {
|
206 | method: 'GET',
|
207 | url: '/path'
|
208 | }
|
209 |
|
210 | fastify.inject(reqOpts, (err, res) => {
|
211 | t.error(err)
|
212 | t.strictEqual(res.statusCode, 200)
|
213 | t.deepEqual(JSON.parse(res.payload), { hello: 'world' })
|
214 | })
|
215 | })
|
216 |
|
217 | test('invalid bodyLimit option - route', t => {
|
218 | t.plan(2)
|
219 | const fastify = Fastify()
|
220 |
|
221 | try {
|
222 | fastify.route({
|
223 | bodyLimit: false,
|
224 | method: 'PUT',
|
225 | handler: () => null
|
226 | })
|
227 | t.fail('bodyLimit must be an integer')
|
228 | } catch (err) {
|
229 | t.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got 'false'")
|
230 | }
|
231 |
|
232 | try {
|
233 | fastify.post('/url', { bodyLimit: 10000.1 }, () => null)
|
234 | t.fail('bodyLimit must be an integer')
|
235 | } catch (err) {
|
236 | t.strictEqual(err.message, "'bodyLimit' option must be an integer > 0. Got '10000.1'")
|
237 | }
|
238 | })
|
239 |
|
240 | test('handler function in options of shorthand route should works correctly', t => {
|
241 | t.plan(3)
|
242 |
|
243 | const fastify = Fastify()
|
244 | fastify.get('/foo', {
|
245 | handler: (req, reply) => {
|
246 | reply.send({ hello: 'world' })
|
247 | }
|
248 | })
|
249 |
|
250 | fastify.inject({
|
251 | method: 'GET',
|
252 | url: '/foo'
|
253 | }, (err, res) => {
|
254 | t.error(err)
|
255 | t.strictEqual(res.statusCode, 200)
|
256 | t.deepEqual(JSON.parse(res.payload), { hello: 'world' })
|
257 | })
|
258 | })
|
259 |
|
260 | test('does not mutate joi schemas', t => {
|
261 | t.plan(4)
|
262 |
|
263 | const fastify = Fastify()
|
264 | function schemaCompiler (schema) {
|
265 | return function (data, opts) {
|
266 | return joi.validate(data, schema)
|
267 | }
|
268 | }
|
269 |
|
270 | fastify.setSchemaCompiler(schemaCompiler)
|
271 |
|
272 | fastify.route({
|
273 | path: '/foo/:an_id',
|
274 | method: 'GET',
|
275 | schema: {
|
276 | params: { an_id: joi.number() }
|
277 | },
|
278 | handler (req, res) {
|
279 | t.deepEqual(req.params, { an_id: 42 })
|
280 | res.send({ hello: 'world' })
|
281 | }
|
282 | })
|
283 |
|
284 | fastify.inject({
|
285 | method: 'GET',
|
286 | url: '/foo/42'
|
287 | }, (err, result) => {
|
288 | t.error(err)
|
289 | t.strictEqual(result.statusCode, 200)
|
290 | t.deepEqual(JSON.parse(result.payload), { hello: 'world' })
|
291 | })
|
292 | })
|