1 | 'use strict'
|
2 |
|
3 | const sget = require('simple-get').concat
|
4 | const Ajv = require('ajv')
|
5 | const Joi = require('joi')
|
6 |
|
7 | module.exports.payloadMethod = function (method, t) {
|
8 | const test = t.test
|
9 | const fastify = require('..')()
|
10 | const upMethod = method.toUpperCase()
|
11 | const loMethod = method.toLowerCase()
|
12 |
|
13 | const opts = {
|
14 | schema: {
|
15 | body: {
|
16 | type: 'object',
|
17 | properties: {
|
18 | hello: {
|
19 | type: 'integer'
|
20 | }
|
21 | }
|
22 | }
|
23 | }
|
24 | }
|
25 |
|
26 | const ajv = new Ajv({ coerceTypes: true, removeAdditional: true })
|
27 | const optsWithCustomValidator = {
|
28 | schema: {
|
29 | body: {
|
30 | type: 'object',
|
31 | properties: {
|
32 | hello: {
|
33 | type: 'integer'
|
34 | }
|
35 | },
|
36 | additionalProperties: false
|
37 | }
|
38 | },
|
39 | schemaCompiler: function (schema) {
|
40 | return ajv.compile(schema)
|
41 | }
|
42 | }
|
43 |
|
44 | const optsWithJoiValidator = {
|
45 | schema: {
|
46 | body: Joi.object().keys({
|
47 | hello: Joi.string().required()
|
48 | }).required()
|
49 | },
|
50 | schemaCompiler: function (schema) {
|
51 | return schema.validate.bind(schema)
|
52 | }
|
53 | }
|
54 |
|
55 | test(`${upMethod} can be created`, t => {
|
56 | t.plan(1)
|
57 | try {
|
58 | fastify[loMethod]('/', opts, function (req, reply) {
|
59 | reply.send(req.body)
|
60 | })
|
61 | fastify[loMethod]('/custom', optsWithCustomValidator, function (req, reply) {
|
62 | reply.send(req.body)
|
63 | })
|
64 | fastify[loMethod]('/joi', optsWithJoiValidator, function (req, reply) {
|
65 | reply.send(req.body)
|
66 | })
|
67 |
|
68 | fastify.register(function (fastify2, opts, next) {
|
69 | fastify2.setSchemaCompiler(function schema (schema) {
|
70 | return body => ({ error: new Error('From custom schema compiler!') })
|
71 | })
|
72 | const withInstanceCustomCompiler = {
|
73 | schema: {
|
74 | body: {
|
75 | type: 'object',
|
76 | properties: { },
|
77 | additionalProperties: false
|
78 | }
|
79 | }
|
80 | }
|
81 | fastify2[loMethod]('/plugin', withInstanceCustomCompiler, (req, reply) => reply.send({ hello: 'never here!' }))
|
82 |
|
83 | const optsWithCustomValidator2 = {
|
84 | schema: {
|
85 | body: {
|
86 | type: 'object',
|
87 | properties: { },
|
88 | additionalProperties: false
|
89 | }
|
90 | },
|
91 | schemaCompiler: function (schema) {
|
92 | return function (body) {
|
93 | return { error: new Error('Always fail!') }
|
94 | }
|
95 | }
|
96 | }
|
97 | fastify2[loMethod]('/plugin/custom', optsWithCustomValidator2, (req, reply) => reply.send({ hello: 'never here!' }))
|
98 |
|
99 | next()
|
100 | })
|
101 | t.pass()
|
102 | } catch (e) {
|
103 | t.fail()
|
104 | }
|
105 | })
|
106 |
|
107 | fastify.listen(0, function (err) {
|
108 | if (err) {
|
109 | t.error(err)
|
110 | }
|
111 |
|
112 | fastify.server.unref()
|
113 |
|
114 | test(`${upMethod} - correctly replies`, t => {
|
115 | if (upMethod === 'HEAD') {
|
116 | t.plan(2)
|
117 | sget({
|
118 | method: upMethod,
|
119 | url: 'http://localhost:' + fastify.server.address().port
|
120 | }, (err, response) => {
|
121 | t.error(err)
|
122 | t.strictEqual(response.statusCode, 200)
|
123 | })
|
124 | } else {
|
125 | t.plan(3)
|
126 | sget({
|
127 | method: upMethod,
|
128 | url: 'http://localhost:' + fastify.server.address().port,
|
129 | body: {
|
130 | hello: 42
|
131 | },
|
132 | json: true
|
133 | }, (err, response, body) => {
|
134 | t.error(err)
|
135 | t.strictEqual(response.statusCode, 200)
|
136 | t.deepEqual(body, { hello: 42 })
|
137 | })
|
138 | }
|
139 | })
|
140 |
|
141 | test(`${upMethod} - 400 on bad parameters`, t => {
|
142 | t.plan(3)
|
143 | sget({
|
144 | method: upMethod,
|
145 | url: 'http://localhost:' + fastify.server.address().port,
|
146 | body: {
|
147 | hello: 'world'
|
148 | },
|
149 | json: true
|
150 | }, (err, response, body) => {
|
151 | t.error(err)
|
152 | t.strictEqual(response.statusCode, 400)
|
153 | t.deepEqual(body, {
|
154 | error: 'Bad Request',
|
155 | message: 'body.hello should be integer',
|
156 | statusCode: 400
|
157 | })
|
158 | })
|
159 | })
|
160 |
|
161 | test(`${upMethod} - input-validation coerce`, t => {
|
162 | t.plan(3)
|
163 | sget({
|
164 | method: upMethod,
|
165 | url: 'http://localhost:' + fastify.server.address().port,
|
166 | body: {
|
167 | hello: '42'
|
168 | },
|
169 | json: true
|
170 | }, (err, response, body) => {
|
171 | t.error(err)
|
172 | t.strictEqual(response.statusCode, 200)
|
173 | t.deepEqual(body, { hello: 42 })
|
174 | })
|
175 | })
|
176 |
|
177 | test(`${upMethod} - input-validation custom schema compiler`, t => {
|
178 | t.plan(3)
|
179 | sget({
|
180 | method: upMethod,
|
181 | url: 'http://localhost:' + fastify.server.address().port + '/custom',
|
182 | body: {
|
183 | hello: '42',
|
184 | world: 55
|
185 | },
|
186 | json: true
|
187 | }, (err, response, body) => {
|
188 | t.error(err)
|
189 | t.strictEqual(response.statusCode, 200)
|
190 | t.deepEqual(body, { hello: 42 })
|
191 | })
|
192 | })
|
193 |
|
194 | test(`${upMethod} - input-validation joi schema compiler ok`, t => {
|
195 | t.plan(3)
|
196 | sget({
|
197 | method: upMethod,
|
198 | url: 'http://localhost:' + fastify.server.address().port + '/joi',
|
199 | body: {
|
200 | hello: '42'
|
201 | },
|
202 | json: true
|
203 | }, (err, response, body) => {
|
204 | t.error(err)
|
205 | t.strictEqual(response.statusCode, 200)
|
206 | t.deepEqual(body, { hello: 42 })
|
207 | })
|
208 | })
|
209 |
|
210 | test(`${upMethod} - input-validation joi schema compiler ko`, t => {
|
211 | t.plan(3)
|
212 | sget({
|
213 | method: upMethod,
|
214 | url: 'http://localhost:' + fastify.server.address().port + '/joi',
|
215 | body: {
|
216 | hello: 44
|
217 | },
|
218 | json: true
|
219 | }, (err, response, body) => {
|
220 | t.error(err)
|
221 | t.strictEqual(response.statusCode, 400)
|
222 | t.deepEqual(body, {
|
223 | error: 'Bad Request',
|
224 | message: 'child "hello" fails because ["hello" must be a string]',
|
225 | statusCode: 400
|
226 | })
|
227 | })
|
228 | })
|
229 |
|
230 | test(`${upMethod} - input-validation instance custom schema compiler encapsulated`, t => {
|
231 | t.plan(3)
|
232 | sget({
|
233 | method: upMethod,
|
234 | url: 'http://localhost:' + fastify.server.address().port + '/plugin',
|
235 | body: { },
|
236 | json: true
|
237 | }, (err, response, body) => {
|
238 | t.error(err)
|
239 | t.strictEqual(response.statusCode, 400)
|
240 | t.deepEqual(body, {
|
241 | error: 'Bad Request',
|
242 | message: 'From custom schema compiler!',
|
243 | statusCode: '400'
|
244 | })
|
245 | })
|
246 | })
|
247 |
|
248 | test(`${upMethod} - input-validation custom schema compiler encapsulated`, t => {
|
249 | t.plan(3)
|
250 | sget({
|
251 | method: upMethod,
|
252 | url: 'http://localhost:' + fastify.server.address().port + '/plugin/custom',
|
253 | body: { },
|
254 | json: true
|
255 | }, (err, response, body) => {
|
256 | t.error(err)
|
257 | t.strictEqual(response.statusCode, 400)
|
258 | t.deepEqual(body, {
|
259 | error: 'Bad Request',
|
260 | message: 'Always fail!',
|
261 | statusCode: '400'
|
262 | })
|
263 | })
|
264 | })
|
265 | })
|
266 | }
|