1 | 'use strict'
|
2 |
|
3 | const { test } = require('tap')
|
4 | const Fastify = require('..')
|
5 |
|
6 | test('Fastify should throw on wrong options', t => {
|
7 | t.plan(2)
|
8 | try {
|
9 | Fastify('lol')
|
10 | t.fail()
|
11 | } catch (e) {
|
12 | t.is(e.message, 'Options must be an object')
|
13 | t.pass()
|
14 | }
|
15 | })
|
16 |
|
17 | test('Fastify should throw on multiple assignment to the same route', t => {
|
18 | t.plan(1)
|
19 | const fastify = Fastify()
|
20 | fastify.get('/', () => {})
|
21 | fastify.get('/', () => {})
|
22 |
|
23 | fastify.ready(err => {
|
24 | t.is(err.message, "Method 'GET' already declared for route '/'")
|
25 | })
|
26 | })
|
27 |
|
28 | test('Fastify should throw for an invalid schema, printing the error route - headers', t => {
|
29 | t.plan(2)
|
30 |
|
31 | const badSchema = {
|
32 | type: 'object',
|
33 | properties: {
|
34 | bad: {
|
35 | type: 'bad-type'
|
36 | }
|
37 | }
|
38 | }
|
39 |
|
40 | const fastify = Fastify()
|
41 | fastify.get('/', { schema: { headers: badSchema } }, () => {})
|
42 | fastify.get('/not-loaded', { schema: { headers: badSchema } }, () => {})
|
43 |
|
44 | fastify.ready(err => {
|
45 | t.is(err.code, 'FST_ERR_SCH_BUILD')
|
46 | t.isLike(err.message, /Failed building the schema for GET: \
|
47 | })
|
48 | })
|
49 |
|
50 | test('Fastify should throw for an invalid schema, printing the error route - body', t => {
|
51 | t.plan(2)
|
52 |
|
53 | const badSchema = {
|
54 | type: 'object',
|
55 | properties: {
|
56 | bad: {
|
57 | type: 'bad-type'
|
58 | }
|
59 | }
|
60 | }
|
61 |
|
62 | const fastify = Fastify()
|
63 | fastify.register((instance, opts, next) => {
|
64 | instance.post('/form', { schema: { body: badSchema } }, () => {})
|
65 | next()
|
66 | }, { prefix: 'hello' })
|
67 |
|
68 | fastify.ready(err => {
|
69 | t.is(err.code, 'FST_ERR_SCH_BUILD')
|
70 | t.isLike(err.message, /Failed building the schema for POST: \/hello\/form/)
|
71 | })
|
72 | })
|
73 |
|
74 | test('Should throw on unsupported method', t => {
|
75 | t.plan(1)
|
76 | const fastify = Fastify()
|
77 | try {
|
78 | fastify.route({
|
79 | method: 'TROLL',
|
80 | url: '/',
|
81 | schema: {},
|
82 | handler: function (req, reply) {}
|
83 | })
|
84 | t.fail()
|
85 | } catch (e) {
|
86 | t.pass()
|
87 | }
|
88 | })
|
89 |
|
90 | test('Should throw on missing handler', t => {
|
91 | t.plan(1)
|
92 | const fastify = Fastify()
|
93 | try {
|
94 | fastify.route({
|
95 | method: 'GET',
|
96 | url: '/'
|
97 | })
|
98 | t.fail()
|
99 | } catch (e) {
|
100 | t.pass()
|
101 | }
|
102 | })
|
103 |
|
104 | test('Should throw if one method is unsupported', t => {
|
105 | const fastify = Fastify()
|
106 | t.plan(1)
|
107 | try {
|
108 | fastify.route({
|
109 | method: ['GET', 'TROLL'],
|
110 | url: '/',
|
111 | schema: {},
|
112 | handler: function (req, reply) {}
|
113 | })
|
114 | t.fail()
|
115 | } catch (e) {
|
116 | t.pass()
|
117 | }
|
118 | })
|
119 |
|
120 | test('Should throw on duplicate content type parser', t => {
|
121 | t.plan(1)
|
122 |
|
123 | const fastify = Fastify()
|
124 | function customParser (req, done) { done(null, '') }
|
125 |
|
126 | fastify.addContentTypeParser('application/qq', customParser)
|
127 | try {
|
128 | fastify.addContentTypeParser('application/qq', customParser)
|
129 | t.fail()
|
130 | } catch (e) {
|
131 | t.pass()
|
132 | }
|
133 | })
|
134 |
|
135 | test('Should throw on duplicate decorator', t => {
|
136 | t.plan(1)
|
137 |
|
138 | const fastify = Fastify()
|
139 | const fooObj = {}
|
140 |
|
141 | fastify.decorate('foo', fooObj)
|
142 | try {
|
143 | fastify.decorate('foo', fooObj)
|
144 | t.fail()
|
145 | } catch (e) {
|
146 | t.pass()
|
147 | }
|
148 | })
|
149 |
|
150 | test('Should not throw on duplicate decorator encapsulation', t => {
|
151 | t.plan(1)
|
152 |
|
153 | const fastify = Fastify()
|
154 | const foo2Obj = {}
|
155 |
|
156 | fastify.decorate('foo2', foo2Obj)
|
157 |
|
158 | fastify.register(function (fastify, opts, next) {
|
159 | t.notThrow(() => {
|
160 | fastify.decorate('foo2', foo2Obj)
|
161 | })
|
162 | next()
|
163 | })
|
164 |
|
165 | fastify.ready()
|
166 | })
|
167 |
|
168 | test('Should throw on duplicate request decorator', t => {
|
169 | t.plan(2)
|
170 |
|
171 | const fooObj = {}
|
172 | const fastify = Fastify()
|
173 |
|
174 | fastify.decorateRequest('foo', fooObj)
|
175 | try {
|
176 | fastify.decorateRequest('foo', fooObj)
|
177 | t.fail()
|
178 | } catch (e) {
|
179 | t.is(e.code, 'FST_ERR_DEC_ALREADY_PRESENT')
|
180 | t.is(e.message, "FST_ERR_DEC_ALREADY_PRESENT: The decorator 'foo' has already been added!")
|
181 | }
|
182 | })
|
183 |
|
184 | test('Should throw if request decorator dependencies are not met', t => {
|
185 | t.plan(2)
|
186 |
|
187 | const fastify = Fastify()
|
188 | const fooObj = {}
|
189 |
|
190 | try {
|
191 | fastify.decorateRequest('bar', fooObj, ['world'])
|
192 | t.fail()
|
193 | } catch (e) {
|
194 | t.is(e.code, 'FST_ERR_DEC_MISSING_DEPENDENCY')
|
195 | t.is(e.message, "FST_ERR_DEC_MISSING_DEPENDENCY: The decorator is missing dependency 'world'.")
|
196 | }
|
197 | })
|
198 |
|
199 | test('Should throw on duplicate reply decorator', t => {
|
200 | t.plan(1)
|
201 |
|
202 | const fastify = Fastify()
|
203 | const fooObj = {}
|
204 |
|
205 | fastify.decorateReply('foo', fooObj)
|
206 | try {
|
207 | fastify.decorateReply('foo', fooObj)
|
208 | t.fail()
|
209 | } catch (e) {
|
210 | t.ok(/has already been added/.test(e.message))
|
211 | }
|
212 | })
|
213 |
|
214 | test('Should throw if reply decorator dependencies are not met', t => {
|
215 | t.plan(1)
|
216 |
|
217 | const fastify = Fastify()
|
218 | const fooObj = {}
|
219 |
|
220 | try {
|
221 | fastify.decorateReply('bar', fooObj, ['world'])
|
222 | t.fail()
|
223 | } catch (e) {
|
224 | t.ok(/missing dependency/.test(e.message))
|
225 | }
|
226 | })
|
227 |
|
228 | test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {
|
229 | t.plan(5)
|
230 |
|
231 | const fastify = Fastify()
|
232 |
|
233 | try {
|
234 | fastify.get('/foo/1', '')
|
235 | t.fail()
|
236 | } catch (e) {
|
237 | t.pass()
|
238 | }
|
239 |
|
240 | try {
|
241 | fastify.get('/foo/2', 1)
|
242 | t.fail()
|
243 | } catch (e) {
|
244 | t.pass()
|
245 | }
|
246 |
|
247 | try {
|
248 | fastify.get('/foo/3', [])
|
249 | t.fail()
|
250 | } catch (e) {
|
251 | t.pass()
|
252 | }
|
253 |
|
254 | try {
|
255 | fastify.get('/foo/4', undefined)
|
256 | t.fail()
|
257 | } catch (e) {
|
258 | t.pass()
|
259 | }
|
260 |
|
261 | try {
|
262 | fastify.get('/foo/5', null)
|
263 | t.fail()
|
264 | } catch (e) {
|
265 | t.pass()
|
266 | }
|
267 | })
|
268 |
|
269 | test('Should throw if handler as the third parameter to the shortcut method is missing and the second parameter is not a function and also not an object', t => {
|
270 | t.plan(5)
|
271 |
|
272 | const fastify = Fastify()
|
273 |
|
274 | try {
|
275 | fastify.get('/foo/1', '')
|
276 | t.fail()
|
277 | } catch (e) {
|
278 | t.pass()
|
279 | }
|
280 |
|
281 | try {
|
282 | fastify.get('/foo/2', 1)
|
283 | t.fail()
|
284 | } catch (e) {
|
285 | t.pass()
|
286 | }
|
287 |
|
288 | try {
|
289 | fastify.get('/foo/3', [])
|
290 | t.fail()
|
291 | } catch (e) {
|
292 | t.pass()
|
293 | }
|
294 |
|
295 | try {
|
296 | fastify.get('/foo/4', undefined)
|
297 | t.fail()
|
298 | } catch (e) {
|
299 | t.pass()
|
300 | }
|
301 |
|
302 | try {
|
303 | fastify.get('/foo/5', null)
|
304 | t.fail()
|
305 | } catch (e) {
|
306 | t.pass()
|
307 | }
|
308 | })
|
309 |
|
310 | test('Should throw if there is handler function as the third parameter to the shortcut method and options as the second parameter is not an object', t => {
|
311 | t.plan(5)
|
312 |
|
313 | const fastify = Fastify()
|
314 |
|
315 | try {
|
316 | fastify.get('/foo/1', '', (req, res) => {})
|
317 | t.fail()
|
318 | } catch (e) {
|
319 | t.pass()
|
320 | }
|
321 |
|
322 | try {
|
323 | fastify.get('/foo/2', 1, (req, res) => {})
|
324 | t.fail()
|
325 | } catch (e) {
|
326 | t.pass()
|
327 | }
|
328 |
|
329 | try {
|
330 | fastify.get('/foo/3', [], (req, res) => {})
|
331 | t.fail()
|
332 | } catch (e) {
|
333 | t.pass()
|
334 | }
|
335 |
|
336 | try {
|
337 | fastify.get('/foo/4', undefined, (req, res) => {})
|
338 | t.fail()
|
339 | } catch (e) {
|
340 | t.pass()
|
341 | }
|
342 |
|
343 | try {
|
344 | fastify.get('/foo/5', null, (req, res) => {})
|
345 | t.fail()
|
346 | } catch (e) {
|
347 | t.pass()
|
348 | }
|
349 | })
|
350 |
|
351 | test('Should throw if found duplicate handler as the third parameter to the shortcut method and in options', t => {
|
352 | t.plan(1)
|
353 |
|
354 | const fastify = Fastify()
|
355 |
|
356 | try {
|
357 | fastify.get('/foo/abc', {
|
358 | handler: (req, res) => {}
|
359 | }, (req, res) => {})
|
360 | t.fail()
|
361 | } catch (e) {
|
362 | t.pass()
|
363 | }
|
364 | })
|