-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
helper.js
299 lines (272 loc) · 7.8 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
'use strict'
const sget = require('simple-get').concat
const stream = require('stream')
module.exports.payloadMethod = function (method, t) {
const test = t.test
const fastify = require('..')()
const upMethod = method.toUpperCase()
const loMethod = method.toLowerCase()
const schema = {
schema: {
response: {
'2xx': {
type: 'object',
properties: {
hello: {
type: 'string'
}
}
}
}
}
}
test(`${upMethod} can be created`, t => {
t.plan(1)
try {
fastify[loMethod]('/', schema, function (req, reply) {
reply.code(200).send(req.body)
})
t.pass()
} catch (e) {
t.fail()
}
})
test(`${upMethod} without schema can be created`, t => {
t.plan(1)
try {
fastify[loMethod]('/missing', function (req, reply) {
reply.code(200).send(req.body)
})
t.pass()
} catch (e) {
t.fail()
}
})
test(`${upMethod} with body and querystring`, t => {
t.plan(1)
try {
fastify[loMethod]('/with-query', function (req, reply) {
req.body.hello = req.body.hello + req.query.foo
reply.code(200).send(req.body)
})
t.pass()
} catch (e) {
t.fail()
}
})
test(`${upMethod} with jsonBodyLimit option`, t => {
t.plan(1)
try {
fastify[loMethod]('/with-limit', { bodyLimit: 1 }, function (req, reply) {
reply.send(req.body)
})
t.pass()
} catch (e) {
t.fail()
}
})
fastify.listen(0, function (err) {
if (err) {
t.error(err)
}
fastify.server.unref()
test(`${upMethod} - correctly replies`, t => {
t.plan(3)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port,
body: {
hello: 'world'
},
json: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(body, { hello: 'world' })
})
})
test(`${upMethod} - correctly replies with very large body`, t => {
t.plan(3)
const largeString = 'world'.repeat(13200)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port,
body: { hello: largeString },
json: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(body, { hello: largeString })
})
})
test(`${upMethod} - correctly replies if the content type has the charset`, t => {
t.plan(3)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port,
body: JSON.stringify({ hello: 'world' }),
headers: {
'content-type': 'application/json;charset=utf-8'
}
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(body.toString(), JSON.stringify({ hello: 'world' }))
})
})
test(`${upMethod} without schema - correctly replies`, t => {
t.plan(3)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port + '/missing',
body: {
hello: 'world'
},
json: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(body, { hello: 'world' })
})
})
test(`${upMethod} with body and querystring - correctly replies`, t => {
t.plan(3)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port + '/with-query?foo=hello',
body: {
hello: 'world'
},
json: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.deepEqual(body, { hello: 'worldhello' })
})
})
test(`${upMethod} with no body - correctly replies`, t => {
t.plan(6)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port + '/missing',
headers: { 'Content-Length': '0' },
timeout: 500
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 200)
t.strictEqual(JSON.parse(body.toString()), null)
})
// Must use inject to make a request without a Content-Length header
fastify.inject({
method: upMethod,
url: '/missing'
}, (err, res) => {
t.error(err)
t.strictEqual(res.statusCode, 200)
t.strictEqual(JSON.parse(res.payload), null)
})
})
test(`${upMethod} returns 415 - incorrect media type if body is not json`, t => {
t.plan(2)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port + '/missing',
body: 'hello world',
timeout: 500
}, (err, response, body) => {
t.error(err)
if (upMethod === 'OPTIONS') {
t.strictEqual(response.statusCode, 200)
} else {
t.strictEqual(response.statusCode, 415)
}
})
})
if (loMethod === 'options') {
test(`OPTIONS returns 415 - should return 415 if Content-Type is not json`, t => {
t.plan(2)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port + '/missing',
body: 'hello world',
headers: {
'Content-Type': 'text/plain'
},
timeout: 500
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 415)
})
})
}
test(`${upMethod} returns 400 - Bad Request`, t => {
t.plan(4)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port,
body: 'hello world',
headers: {
'Content-Type': 'application/json'
},
timeout: 500
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 400)
})
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port,
body: '',
headers: { 'Content-Type': 'application/json' },
timeout: 500
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 400)
})
})
test(`${upMethod} returns 413 - Payload Too Large`, t => {
t.plan(upMethod === 'OPTIONS' ? 4 : 6)
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port,
headers: {
'Content-Type': 'application/json',
'Content-Length': 1024 * 1024 + 1
}
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 413)
})
// Node errors for OPTIONS requests with a stream body and no Content-Length header
if (upMethod !== 'OPTIONS') {
var chunk = Buffer.allocUnsafe(1024 * 1024 + 1)
const largeStream = new stream.Readable({
read () {
this.push(chunk)
chunk = null
}
})
sget({
method: upMethod,
url: 'http://localhost:' + fastify.server.address().port,
headers: { 'Content-Type': 'application/json' },
body: largeStream,
timeout: 500
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 413)
})
}
sget({
method: upMethod,
url: `http://localhost:${fastify.server.address().port}/with-limit`,
headers: { 'Content-Type': 'application/json' },
body: {},
json: true
}, (err, response, body) => {
t.error(err)
t.strictEqual(response.statusCode, 413)
})
})
})
}