UNPKG

3.55 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 opts = {
9 schema: {
10 response: {
11 200: {
12 type: 'object',
13 properties: {
14 hello: {
15 type: 'string'
16 }
17 }
18 },
19 '2xx': {
20 type: 'object',
21 properties: {
22 hello: {
23 type: 'number'
24 }
25 }
26 }
27 }
28 }
29}
30
31test('shorthand - output string', t => {
32 t.plan(1)
33 try {
34 fastify.get('/string', opts, function (req, reply) {
35 reply.code(200).send({ hello: 'world' })
36 })
37 t.pass()
38 } catch (e) {
39 t.fail()
40 }
41})
42
43test('shorthand - output number', t => {
44 t.plan(1)
45 try {
46 fastify.get('/number', opts, function (req, reply) {
47 reply.code(201).send({ hello: 55 })
48 })
49 t.pass()
50 } catch (e) {
51 t.fail()
52 }
53})
54
55test('wrong object for schema - output', t => {
56 t.plan(1)
57 try {
58 fastify.get('/wrong-object-for-schema', opts, function (req, reply) {
59 // will send { }
60 reply.code(201).send({ hello: 'world' })
61 })
62 t.pass()
63 } catch (e) {
64 t.fail()
65 }
66})
67
68test('empty response', t => {
69 t.plan(1)
70 try {
71 // no checks
72 fastify.get('/empty', opts, function (req, reply) {
73 reply.code(204).send()
74 })
75 t.pass()
76 } catch (e) {
77 t.fail()
78 }
79})
80
81test('unlisted response code', t => {
82 t.plan(1)
83 try {
84 fastify.get('/400', opts, function (req, reply) {
85 reply.code(400).send({ hello: 'DOOM' })
86 })
87 t.pass()
88 } catch (e) {
89 t.fail()
90 }
91})
92
93fastify.listen(0, err => {
94 t.error(err)
95 fastify.server.unref()
96
97 test('shorthand - string get ok', t => {
98 t.plan(4)
99 sget({
100 method: 'GET',
101 url: 'http://localhost:' + fastify.server.address().port + '/string'
102 }, (err, response, body) => {
103 t.error(err)
104 t.strictEqual(response.statusCode, 200)
105 t.strictEqual(response.headers['content-length'], '' + body.length)
106 t.deepEqual(JSON.parse(body), { hello: 'world' })
107 })
108 })
109
110 test('shorthand - number get ok', t => {
111 t.plan(4)
112 sget({
113 method: 'GET',
114 url: 'http://localhost:' + fastify.server.address().port + '/number'
115 }, (err, response, body) => {
116 t.error(err)
117 t.strictEqual(response.statusCode, 201)
118 t.strictEqual(response.headers['content-length'], '' + body.length)
119 t.deepEqual(JSON.parse(body), { hello: 55 })
120 })
121 })
122
123 test('shorthand - wrong-object-for-schema', t => {
124 t.plan(4)
125 sget({
126 method: 'GET',
127 url: 'http://localhost:' + fastify.server.address().port + '/wrong-object-for-schema'
128 }, (err, response, body) => {
129 t.error(err)
130 t.strictEqual(response.statusCode, 201)
131 t.strictEqual(response.headers['content-length'], '' + body.length)
132 t.deepEqual(JSON.parse(body), {})
133 })
134 })
135
136 test('shorthand - empty', t => {
137 t.plan(2)
138 sget({
139 method: 'GET',
140 url: 'http://localhost:' + fastify.server.address().port + '/empty'
141 }, (err, response, body) => {
142 t.error(err)
143 t.strictEqual(response.statusCode, 204)
144 })
145 })
146
147 test('shorthand - 400', t => {
148 t.plan(4)
149 sget({
150 method: 'GET',
151 url: 'http://localhost:' + fastify.server.address().port + '/400'
152 }, (err, response, body) => {
153 t.error(err)
154 t.strictEqual(response.statusCode, 400)
155 t.strictEqual(response.headers['content-length'], '' + body.length)
156 t.deepEqual(JSON.parse(body), { hello: 'DOOM' })
157 })
158 })
159})