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(9)
|
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('Multiple methods', t => {
|
69 | t.plan(1)
|
70 | try {
|
71 | fastify.route({
|
72 | method: ['GET', 'DELETE'],
|
73 | url: '/multiple',
|
74 | handler: function (req, reply) {
|
75 | reply.send({ hello: 'world' })
|
76 | }
|
77 | })
|
78 | t.pass()
|
79 | } catch (e) {
|
80 | t.fail()
|
81 | }
|
82 | })
|
83 |
|
84 | test('Add multiple methods', t => {
|
85 | t.plan(1)
|
86 | try {
|
87 | fastify.get('/add-multiple', function (req, reply) {
|
88 | reply.send({ hello: 'Bob!' })
|
89 | })
|
90 | fastify.route({
|
91 | method: ['PUT', 'DELETE'],
|
92 | url: '/add-multiple',
|
93 | handler: function (req, reply) {
|
94 | reply.send({ hello: 'world' })
|
95 | }
|
96 | })
|
97 | t.pass()
|
98 | } catch (e) {
|
99 | t.fail()
|
100 | }
|
101 | })
|
102 |
|
103 | fastify.listen(0, function (err) {
|
104 | if (err) t.error(err)
|
105 | fastify.server.unref()
|
106 |
|
107 | test('cannot add another route after binding', t => {
|
108 | t.plan(1)
|
109 | try {
|
110 | fastify.route({
|
111 | method: 'GET',
|
112 | url: '/another-get-route',
|
113 | handler: function (req, reply) {
|
114 | reply.send({ hello: 'world' })
|
115 | }
|
116 | })
|
117 | t.fail()
|
118 | } catch (e) {
|
119 | t.pass()
|
120 | }
|
121 | })
|
122 |
|
123 | test('route - get', t => {
|
124 | t.plan(3)
|
125 | sget({
|
126 | method: 'GET',
|
127 | url: 'http://localhost:' + fastify.server.address().port
|
128 | }, (err, response, body) => {
|
129 | t.error(err)
|
130 | t.strictEqual(response.statusCode, 200)
|
131 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
132 | })
|
133 | })
|
134 |
|
135 | test('route - missing schema', t => {
|
136 | t.plan(3)
|
137 | sget({
|
138 | method: 'GET',
|
139 | url: 'http://localhost:' + fastify.server.address().port + '/missing'
|
140 | }, (err, response, body) => {
|
141 | t.error(err)
|
142 | t.strictEqual(response.statusCode, 200)
|
143 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
144 | })
|
145 | })
|
146 |
|
147 | test('route - multiple methods', t => {
|
148 | t.plan(6)
|
149 | sget({
|
150 | method: 'GET',
|
151 | url: 'http://localhost:' + fastify.server.address().port + '/multiple'
|
152 | }, (err, response, body) => {
|
153 | t.error(err)
|
154 | t.strictEqual(response.statusCode, 200)
|
155 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
156 | })
|
157 |
|
158 | sget({
|
159 | method: 'DELETE',
|
160 | url: 'http://localhost:' + fastify.server.address().port + '/multiple'
|
161 | }, (err, response, body) => {
|
162 | t.error(err)
|
163 | t.strictEqual(response.statusCode, 200)
|
164 | t.deepEqual(JSON.parse(body), { hello: 'world' })
|
165 | })
|
166 | })
|
167 | })
|
168 | })
|
169 |
|
170 | test('invalid schema - route', t => {
|
171 | t.plan(3)
|
172 |
|
173 | const fastify = Fastify()
|
174 | fastify.route({
|
175 | handler: () => {},
|
176 | method: 'GET',
|
177 | url: '/invalid',
|
178 | schema: {
|
179 | querystring: {
|
180 | id: 'string'
|
181 | }
|
182 | }
|
183 | })
|
184 | fastify.after(err => {
|
185 | t.notOk(err, 'the error is throw on preReady')
|
186 | })
|
187 | fastify.ready(err => {
|
188 | t.is(err.code, 'FST_ERR_SCH_BUILD')
|
189 | t.isLike(err.message, /Failed building the schema for GET: \/invalid/)
|
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 | })
|
293 |
|
294 | test('multiple routes with one schema', t => {
|
295 | t.plan(2)
|
296 |
|
297 | const fastify = Fastify()
|
298 |
|
299 | const schema = {
|
300 | query: {
|
301 | id: { type: 'number' }
|
302 | }
|
303 | }
|
304 |
|
305 | fastify.route({
|
306 | schema,
|
307 | method: 'GET',
|
308 | path: '/first/:id',
|
309 | handler (req, res) {
|
310 | res.send({ hello: 'world' })
|
311 | }
|
312 | })
|
313 |
|
314 | fastify.route({
|
315 | schema,
|
316 | method: 'GET',
|
317 | path: '/second/:id',
|
318 | handler (req, res) {
|
319 | res.send({ hello: 'world' })
|
320 | }
|
321 | })
|
322 |
|
323 | fastify.ready(error => {
|
324 | t.error(error)
|
325 | t.strictSame(schema, {
|
326 | query: { id: { type: 'number' } },
|
327 | querystring: { type: 'object', properties: { id: { type: 'number' } } }
|
328 | })
|
329 | })
|
330 | })
|