UNPKG

7.16 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 querySchema = {
24 schema: {
25 querystring: {
26 type: 'object',
27 properties: {
28 hello: {
29 type: 'integer'
30 }
31 }
32 }
33 }
34}
35
36const paramsSchema = {
37 schema: {
38 params: {
39 type: 'object',
40 properties: {
41 foo: {
42 type: 'string'
43 },
44 test: {
45 type: 'integer'
46 }
47 }
48 }
49 }
50}
51
52const headersSchema = {
53 schema: {
54 headers: {
55 type: 'object',
56 properties: {
57 'x-test': {
58 type: 'number'
59 }
60 }
61 }
62 }
63}
64
65const bodySchema = {
66 schema: {
67 body: {
68 type: 'object',
69 properties: {
70 hello: {
71 type: 'string'
72 }
73 }
74 },
75 response: {
76 '2xx': {
77 type: 'object',
78 properties: {
79 hello: {
80 type: 'string'
81 }
82 }
83 }
84 }
85 }
86}
87
88test('shorthand - delete', t => {
89 t.plan(1)
90 try {
91 fastify.delete('/', schema, function (req, reply) {
92 reply.code(200).send({ hello: 'world' })
93 })
94 t.pass()
95 } catch (e) {
96 t.fail()
97 }
98})
99
100test('shorthand - delete params', t => {
101 t.plan(1)
102 try {
103 fastify.delete('/params/:foo/:test', paramsSchema, function (req, reply) {
104 reply.code(200).send(req.params)
105 })
106 t.pass()
107 } catch (e) {
108 t.fail()
109 }
110})
111
112test('shorthand - delete, querystring schema', t => {
113 t.plan(1)
114 try {
115 fastify.delete('/query', querySchema, function (req, reply) {
116 reply.send(req.query)
117 })
118 t.pass()
119 } catch (e) {
120 t.fail()
121 }
122})
123
124test('shorthand - get, headers schema', t => {
125 t.plan(1)
126 try {
127 fastify.delete('/headers', headersSchema, function (req, reply) {
128 reply.code(200).send(req.headers)
129 })
130 t.pass()
131 } catch (e) {
132 t.fail()
133 }
134})
135
136test('missing schema - delete', t => {
137 t.plan(1)
138 try {
139 fastify.delete('/missing', function (req, reply) {
140 reply.code(200).send({ hello: 'world' })
141 })
142 t.pass()
143 } catch (e) {
144 t.fail()
145 }
146})
147
148test('body - delete', t => {
149 t.plan(1)
150 try {
151 fastify.delete('/body', bodySchema, function (req, reply) {
152 reply.send(req.body)
153 })
154 t.pass()
155 } catch (e) {
156 t.fail()
157 }
158})
159
160fastify.listen(0, err => {
161 t.error(err)
162 fastify.server.unref()
163
164 test('shorthand - request delete', t => {
165 t.plan(4)
166 sget({
167 method: 'DELETE',
168 url: 'http://localhost:' + fastify.server.address().port
169 }, (err, response, body) => {
170 t.error(err)
171 t.strictEqual(response.statusCode, 200)
172 t.strictEqual(response.headers['content-length'], '' + body.length)
173 t.deepEqual(JSON.parse(body), { hello: 'world' })
174 })
175 })
176
177 test('shorthand - request delete params schema', t => {
178 t.plan(4)
179 sget({
180 method: 'DELETE',
181 url: 'http://localhost:' + fastify.server.address().port + '/params/world/123'
182 }, (err, response, body) => {
183 t.error(err)
184 t.strictEqual(response.statusCode, 200)
185 t.strictEqual(response.headers['content-length'], '' + body.length)
186 t.deepEqual(JSON.parse(body), { foo: 'world', test: 123 })
187 })
188 })
189
190 test('shorthand - request delete params schema error', t => {
191 t.plan(3)
192 sget({
193 method: 'DELETE',
194 url: 'http://localhost:' + fastify.server.address().port + '/params/world/string'
195 }, (err, response, body) => {
196 t.error(err)
197 t.strictEqual(response.statusCode, 400)
198 t.deepEqual(JSON.parse(body), {
199 error: 'Bad Request',
200 message: 'params.test should be integer',
201 statusCode: 400
202 })
203 })
204 })
205
206 test('shorthand - request delete headers schema', t => {
207 t.plan(4)
208 sget({
209 method: 'DELETE',
210 headers: {
211 'x-test': 1
212 },
213 url: 'http://localhost:' + fastify.server.address().port + '/headers'
214 }, (err, response, body) => {
215 t.error(err)
216 t.strictEqual(response.statusCode, 200)
217 t.strictEqual(response.headers['content-length'], '' + body.length)
218 t.strictEqual(JSON.parse(body)['x-test'], 1)
219 })
220 })
221
222 test('shorthand - request delete headers schema error', t => {
223 t.plan(3)
224 sget({
225 method: 'DELETE',
226 headers: {
227 'x-test': 'abc'
228 },
229 url: 'http://localhost:' + fastify.server.address().port + '/headers'
230 }, (err, response, body) => {
231 t.error(err)
232 t.strictEqual(response.statusCode, 400)
233 t.deepEqual(JSON.parse(body), {
234 error: 'Bad Request',
235 message: "headers['x-test'] should be number",
236 statusCode: 400
237 })
238 })
239 })
240
241 test('shorthand - request delete querystring schema', t => {
242 t.plan(4)
243 sget({
244 method: 'DELETE',
245 url: 'http://localhost:' + fastify.server.address().port + '/query?hello=123'
246 }, (err, response, body) => {
247 t.error(err)
248 t.strictEqual(response.statusCode, 200)
249 t.strictEqual(response.headers['content-length'], '' + body.length)
250 t.deepEqual(JSON.parse(body), { hello: 123 })
251 })
252 })
253
254 test('shorthand - request delete querystring schema error', t => {
255 t.plan(3)
256 sget({
257 method: 'DELETE',
258 url: 'http://localhost:' + fastify.server.address().port + '/query?hello=world'
259 }, (err, response, body) => {
260 t.error(err)
261 t.strictEqual(response.statusCode, 400)
262 t.deepEqual(JSON.parse(body), {
263 error: 'Bad Request',
264 message: 'querystring.hello should be integer',
265 statusCode: 400
266 })
267 })
268 })
269
270 test('shorthand - request delete missing schema', t => {
271 t.plan(4)
272 sget({
273 method: 'DELETE',
274 url: 'http://localhost:' + fastify.server.address().port + '/missing'
275 }, (err, response, body) => {
276 t.error(err)
277 t.strictEqual(response.statusCode, 200)
278 t.strictEqual(response.headers['content-length'], '' + body.length)
279 t.deepEqual(JSON.parse(body), { hello: 'world' })
280 })
281 })
282
283 test('shorthand - delete with body', t => {
284 t.plan(3)
285 sget({
286 method: 'DELETE',
287 url: 'http://localhost:' + fastify.server.address().port + '/body',
288 body: {
289 hello: 'world'
290 },
291 json: true
292 }, (err, response, body) => {
293 t.error(err)
294 t.strictEqual(response.statusCode, 200)
295 t.deepEqual(body, { hello: 'world' })
296 })
297 })
298})
299
300// https://github.com/fastify/fastify/issues/936
301test('shorthand - delete with application/json Content-Type header and without body', t => {
302 t.plan(4)
303 const fastify = require('..')()
304 fastify.delete('/', {}, (req, reply) => {
305 t.equal(req.body, null)
306 reply.send(req.body)
307 })
308 fastify.inject({
309 method: 'DELETE',
310 url: '/',
311 headers: { 'Content-Type': 'application/json' },
312 body: null
313 }, (err, response) => {
314 t.error(err)
315 t.strictEqual(response.statusCode, 200)
316 t.deepEqual(JSON.parse(response.payload), null)
317 })
318})