1 | 'use strict'
|
2 |
|
3 | const t = require('tap')
|
4 | const test = t.test
|
5 | const Fastify = require('..')
|
6 | const ajvMergePatch = require('ajv-merge-patch')
|
7 |
|
8 | test('case insensitive header validation', t => {
|
9 | t.plan(2)
|
10 | const fastify = Fastify()
|
11 | fastify.route({
|
12 | method: 'GET',
|
13 | url: '/',
|
14 | handler: (req, reply) => {
|
15 | reply.code(200).send(req.headers.foobar)
|
16 | },
|
17 | schema: {
|
18 | headers: {
|
19 | type: 'object',
|
20 | required: ['FooBar'],
|
21 | properties: {
|
22 | FooBar: { type: 'string' }
|
23 | }
|
24 | }
|
25 | }
|
26 | })
|
27 | fastify.inject({
|
28 | method: 'GET',
|
29 | url: '/',
|
30 | headers: {
|
31 | FooBar: 'Baz'
|
32 | }
|
33 | }, (err, res) => {
|
34 | t.error(err)
|
35 | t.equal(res.payload, 'Baz')
|
36 | })
|
37 | })
|
38 |
|
39 | test('not evaluate json-schema $schema keyword', t => {
|
40 | t.plan(2)
|
41 | const fastify = Fastify()
|
42 | fastify.route({
|
43 | method: 'POST',
|
44 | url: '/',
|
45 | handler: (req, reply) => {
|
46 | reply.code(200).send(req.body.hello)
|
47 | },
|
48 | schema: {
|
49 | body: {
|
50 | $schema: 'http://json-schema.org/draft-07/schema#',
|
51 | type: 'object',
|
52 | properties: {
|
53 | hello: {
|
54 | type: 'string'
|
55 | }
|
56 | }
|
57 | }
|
58 | }
|
59 | })
|
60 | fastify.inject({
|
61 | method: 'POST',
|
62 | url: '/',
|
63 | body: {
|
64 | hello: 'world'
|
65 | }
|
66 | }, (err, res) => {
|
67 | t.error(err)
|
68 | t.equal(res.payload, 'world')
|
69 | })
|
70 | })
|
71 |
|
72 | test('validation context in validation result', t => {
|
73 | t.plan(3)
|
74 | const fastify = Fastify()
|
75 |
|
76 | fastify.setErrorHandler((err, request, reply) => {
|
77 | t.equal(err instanceof Error, true)
|
78 | t.equal(err.validationContext, 'body')
|
79 | reply.send()
|
80 | })
|
81 | fastify.route({
|
82 | method: 'GET',
|
83 | url: '/',
|
84 | handler: (req, reply) => {
|
85 | reply.code(200).send(req.body.hello)
|
86 | },
|
87 | schema: {
|
88 | body: {
|
89 | type: 'object',
|
90 | required: ['hello'],
|
91 | properties: {
|
92 | hello: { type: 'string' }
|
93 | }
|
94 | }
|
95 | }
|
96 | })
|
97 | fastify.inject({
|
98 | method: 'GET',
|
99 | url: '/',
|
100 |
|
101 | body: {}
|
102 | }, (err, res) => {
|
103 | t.error(err)
|
104 | })
|
105 | })
|
106 |
|
107 | test('Should handle $merge keywords in body', t => {
|
108 | t.plan(5)
|
109 | const fastify = Fastify({
|
110 | ajv: {
|
111 | plugins: [
|
112 | ajvMergePatch
|
113 | ]
|
114 | }
|
115 | })
|
116 |
|
117 | fastify.route({
|
118 | method: 'POST',
|
119 | url: '/',
|
120 | schema: {
|
121 | body: {
|
122 | $merge: {
|
123 | source: {
|
124 | type: 'object',
|
125 | properties: {
|
126 | q: {
|
127 | type: 'string'
|
128 | }
|
129 | }
|
130 | },
|
131 | with: {
|
132 | required: ['q']
|
133 | }
|
134 | }
|
135 | }
|
136 | },
|
137 | handler (req, reply) {
|
138 | reply.send({ ok: 1 })
|
139 | }
|
140 | })
|
141 |
|
142 | fastify.ready(err => {
|
143 | t.error(err)
|
144 |
|
145 | fastify.inject({
|
146 | method: 'POST',
|
147 | url: '/'
|
148 | }, (err, res) => {
|
149 | t.error(err)
|
150 | t.equals(res.statusCode, 400)
|
151 | })
|
152 |
|
153 | fastify.inject({
|
154 | method: 'POST',
|
155 | url: '/',
|
156 | body: {
|
157 | q: 'foo'
|
158 | }
|
159 | }, (err, res) => {
|
160 | t.error(err)
|
161 | t.equals(res.statusCode, 200)
|
162 | })
|
163 | })
|
164 | })
|
165 |
|
166 | test('Should handle $patch keywords in body', t => {
|
167 | t.plan(5)
|
168 | const fastify = Fastify({
|
169 | ajv: {
|
170 | plugins: [
|
171 | ajvMergePatch
|
172 | ]
|
173 | }
|
174 | })
|
175 |
|
176 | fastify.route({
|
177 | method: 'POST',
|
178 | url: '/',
|
179 | schema: {
|
180 | body: {
|
181 | $patch: {
|
182 | source: {
|
183 | type: 'object',
|
184 | properties: {
|
185 | q: {
|
186 | type: 'string'
|
187 | }
|
188 | }
|
189 | },
|
190 | with: [
|
191 | {
|
192 | op: 'add',
|
193 | path: '/properties/q',
|
194 | value: { type: 'number' }
|
195 | }
|
196 | ]
|
197 | }
|
198 | }
|
199 | },
|
200 | handler (req, reply) {
|
201 | reply.send({ ok: 1 })
|
202 | }
|
203 | })
|
204 |
|
205 | fastify.ready(err => {
|
206 | t.error(err)
|
207 |
|
208 | fastify.inject({
|
209 | method: 'POST',
|
210 | url: '/',
|
211 | body: {
|
212 | q: 'foo'
|
213 | }
|
214 | }, (err, res) => {
|
215 | t.error(err)
|
216 | t.equals(res.statusCode, 400)
|
217 | })
|
218 |
|
219 | fastify.inject({
|
220 | method: 'POST',
|
221 | url: '/',
|
222 | body: {
|
223 | q: 10
|
224 | }
|
225 | }, (err, res) => {
|
226 | t.error(err)
|
227 | t.equals(res.statusCode, 200)
|
228 | })
|
229 | })
|
230 | })
|