UNPKG

8.37 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const sget = require('simple-get').concat
6const fastify = require('..')()
7
8const schema = {
9 schema: {
10 response: {
11 '2xx': {
12 type: 'object',
13 properties: {
14 hello: {
15 type: 'string'
16 }
17 }
18 }
19 }
20 }
21}
22
23const nullSchema = {
24 schema: {
25 response: {
26 '2xx': {
27 type: 'null'
28 }
29 }
30 }
31}
32
33const numberSchema = {
34 schema: {
35 response: {
36 '2xx': {
37 type: 'object',
38 properties: {
39 hello: {
40 type: 'number'
41 }
42 }
43 }
44 }
45 }
46}
47
48const querySchema = {
49 schema: {
50 querystring: {
51 type: 'object',
52 properties: {
53 hello: {
54 type: 'integer'
55 }
56 }
57 }
58 }
59}
60
61const paramsSchema = {
62 schema: {
63 params: {
64 type: 'object',
65 properties: {
66 foo: {
67 type: 'string'
68 },
69 test: {
70 type: 'integer'
71 }
72 }
73 }
74 }
75}
76
77const headersSchema = {
78 schema: {
79 headers: {
80 type: 'object',
81 properties: {
82 'x-test': {
83 type: 'number'
84 },
85 'Y-Test': {
86 type: 'number'
87 }
88 }
89 }
90 }
91}
92
93test('shorthand - get', t => {
94 t.plan(1)
95 try {
96 fastify.get('/', schema, function (req, reply) {
97 reply.code(200).send({ hello: 'world' })
98 })
99 t.pass()
100 } catch (e) {
101 t.fail()
102 }
103})
104
105test('shorthand - get (return null)', t => {
106 t.plan(1)
107 try {
108 fastify.get('/null', nullSchema, function (req, reply) {
109 reply.code(200).send(null)
110 })
111 t.pass()
112 } catch (e) {
113 t.fail()
114 }
115})
116
117test('shorthand - get params', t => {
118 t.plan(1)
119 try {
120 fastify.get('/params/:foo/:test', paramsSchema, function (req, reply) {
121 reply.code(200).send(req.params)
122 })
123 t.pass()
124 } catch (e) {
125 t.fail()
126 }
127})
128
129test('shorthand - get, querystring schema', t => {
130 t.plan(1)
131 try {
132 fastify.get('/query', querySchema, function (req, reply) {
133 reply.code(200).send(req.query)
134 })
135 t.pass()
136 } catch (e) {
137 t.fail()
138 }
139})
140
141test('shorthand - get, headers schema', t => {
142 t.plan(1)
143 try {
144 fastify.get('/headers', headersSchema, function (req, reply) {
145 reply.code(200).send(req.headers)
146 })
147 t.pass()
148 } catch (e) {
149 t.fail()
150 }
151})
152
153test('missing schema - get', t => {
154 t.plan(1)
155 try {
156 fastify.get('/missing', function (req, reply) {
157 reply.code(200).send({ hello: 'world' })
158 })
159 t.pass()
160 } catch (e) {
161 t.fail()
162 }
163})
164
165test('custom serializer - get', t => {
166 t.plan(1)
167
168 function customSerializer (data) {
169 return JSON.stringify(data)
170 }
171
172 try {
173 fastify.get('/custom-serializer', numberSchema, function (req, reply) {
174 reply.code(200).serializer(customSerializer).send({ hello: 'world' })
175 })
176 t.pass()
177 } catch (e) {
178 t.fail()
179 }
180})
181
182test('empty response', t => {
183 t.plan(1)
184 try {
185 fastify.get('/empty', function (req, reply) {
186 reply.code(200).send()
187 })
188 t.pass()
189 } catch (e) {
190 t.fail()
191 }
192})
193
194test('send a falsy boolean', t => {
195 t.plan(1)
196 try {
197 fastify.get('/boolean', function (req, reply) {
198 reply.code(200).send(false)
199 })
200 t.pass()
201 } catch (e) {
202 t.fail()
203 }
204})
205
206fastify.listen(0, err => {
207 t.error(err)
208 fastify.server.unref()
209
210 test('shorthand - request get', t => {
211 t.plan(4)
212 sget({
213 method: 'GET',
214 url: 'http://localhost:' + fastify.server.address().port
215 }, (err, response, body) => {
216 t.error(err)
217 t.strictEqual(response.statusCode, 200)
218 t.strictEqual(response.headers['content-length'], '' + body.length)
219 t.deepEqual(JSON.parse(body), { hello: 'world' })
220 })
221 })
222
223 test('shorthand - request get params schema', t => {
224 t.plan(4)
225 sget({
226 method: 'GET',
227 url: 'http://localhost:' + fastify.server.address().port + '/params/world/123'
228 }, (err, response, body) => {
229 t.error(err)
230 t.strictEqual(response.statusCode, 200)
231 t.strictEqual(response.headers['content-length'], '' + body.length)
232 t.deepEqual(JSON.parse(body), { foo: 'world', test: 123 })
233 })
234 })
235
236 test('shorthand - request get params schema error', t => {
237 t.plan(3)
238 sget({
239 method: 'GET',
240 url: 'http://localhost:' + fastify.server.address().port + '/params/world/string'
241 }, (err, response, body) => {
242 t.error(err)
243 t.strictEqual(response.statusCode, 400)
244 t.deepEqual(JSON.parse(body), {
245 error: 'Bad Request',
246 message: 'params.test should be integer',
247 statusCode: 400
248 })
249 })
250 })
251
252 test('shorthand - request get headers schema', t => {
253 t.plan(4)
254 sget({
255 method: 'GET',
256 headers: {
257 'x-test': '1',
258 'Y-Test': '3'
259 },
260 json: true,
261 url: 'http://localhost:' + fastify.server.address().port + '/headers'
262 }, (err, response, body) => {
263 t.error(err)
264 t.strictEqual(response.statusCode, 200)
265 t.strictEqual(body['x-test'], 1)
266 t.strictEqual(body['y-test'], 3)
267 })
268 })
269
270 test('shorthand - request get headers schema error', t => {
271 t.plan(3)
272 sget({
273 method: 'GET',
274 headers: {
275 'x-test': 'abc'
276 },
277 url: 'http://localhost:' + fastify.server.address().port + '/headers'
278 }, (err, response, body) => {
279 t.error(err)
280 t.strictEqual(response.statusCode, 400)
281 t.deepEqual(JSON.parse(body), {
282 error: 'Bad Request',
283 message: "headers['x-test'] should be number",
284 statusCode: 400
285 })
286 })
287 })
288
289 test('shorthand - request get querystring schema', t => {
290 t.plan(4)
291 sget({
292 method: 'GET',
293 url: 'http://localhost:' + fastify.server.address().port + '/query?hello=123'
294 }, (err, response, body) => {
295 t.error(err)
296 t.strictEqual(response.statusCode, 200)
297 t.strictEqual(response.headers['content-length'], '' + body.length)
298 t.deepEqual(JSON.parse(body), { hello: 123 })
299 })
300 })
301
302 test('shorthand - request get querystring schema error', t => {
303 t.plan(3)
304 sget({
305 method: 'GET',
306 url: 'http://localhost:' + fastify.server.address().port + '/query?hello=world'
307 }, (err, response, body) => {
308 t.error(err)
309 t.strictEqual(response.statusCode, 400)
310 t.deepEqual(JSON.parse(body), {
311 error: 'Bad Request',
312 message: 'querystring.hello should be integer',
313 statusCode: 400
314 })
315 })
316 })
317
318 test('shorthand - request get missing schema', t => {
319 t.plan(4)
320 sget({
321 method: 'GET',
322 url: 'http://localhost:' + fastify.server.address().port + '/missing'
323 }, (err, response, body) => {
324 t.error(err)
325 t.strictEqual(response.statusCode, 200)
326 t.strictEqual(response.headers['content-length'], '' + body.length)
327 t.deepEqual(JSON.parse(body), { hello: 'world' })
328 })
329 })
330
331 test('shorthand - custom serializer', t => {
332 t.plan(4)
333 sget({
334 method: 'GET',
335 url: 'http://localhost:' + fastify.server.address().port + '/custom-serializer'
336 }, (err, response, body) => {
337 t.error(err)
338 t.strictEqual(response.statusCode, 200)
339 t.strictEqual(response.headers['content-length'], '' + body.length)
340 t.deepEqual(JSON.parse(body), { hello: 'world' })
341 })
342 })
343
344 test('shorthand - empty response', t => {
345 t.plan(4)
346 sget({
347 method: 'GET',
348 url: 'http://localhost:' + fastify.server.address().port + '/empty'
349 }, (err, response, body) => {
350 t.error(err)
351 t.strictEqual(response.statusCode, 200)
352 t.strictEqual(response.headers['content-length'], '0')
353 t.deepEqual(body.toString(), '')
354 })
355 })
356
357 test('shorthand - send a falsy boolean', t => {
358 t.plan(3)
359 sget({
360 method: 'GET',
361 url: 'http://localhost:' + fastify.server.address().port + '/boolean'
362 }, (err, response, body) => {
363 t.error(err)
364 t.strictEqual(response.statusCode, 200)
365 t.deepEqual(body.toString(), 'false')
366 })
367 })
368
369 test('shorthand - send null value', t => {
370 t.plan(3)
371 sget({
372 method: 'GET',
373 url: 'http://localhost:' + fastify.server.address().port + '/null'
374 }, (err, response, body) => {
375 t.error(err)
376 t.strictEqual(response.statusCode, 200)
377 t.deepEqual(body.toString(), 'null')
378 })
379 })
380})