forked from fastify/fastify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbody-limit.test.js
197 lines (175 loc) · 5.05 KB
/
body-limit.test.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
'use strict'
const Fastify = require('../fastify')
const sget = require('simple-get').concat
const zlib = require('node:zlib')
const { test } = require('node:test')
test('bodyLimit', (t, done) => {
t.plan(5)
try {
Fastify({ bodyLimit: 1.3 })
t.assert.fail('option must be an integer')
} catch (err) {
t.assert.ok(err)
}
try {
Fastify({ bodyLimit: [] })
t.assert.fail('option must be an integer')
} catch (err) {
t.assert.ok(err)
}
const fastify = Fastify({ bodyLimit: 1 })
fastify.post('/', (request, reply) => {
reply.send({ error: 'handler should not be called' })
})
fastify.listen({ port: 0 }, function (err) {
t.assert.ifError(err)
t.after(() => { fastify.close() })
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 413)
done()
})
})
})
test('bodyLimit is applied to decoded content', async (t) => {
t.plan(6)
const body = { x: 'x'.repeat(30000) }
const json = JSON.stringify(body)
const encoded = zlib.gzipSync(json)
const fastify = Fastify()
fastify.addHook('preParsing', async (req, reply, payload) => {
t.assert.strictEqual(req.headers['content-length'], `${encoded.length}`)
const unzip = zlib.createGunzip()
Object.defineProperty(unzip, 'receivedEncodedLength', {
get () {
return unzip.bytesWritten
}
})
payload.pipe(unzip)
return unzip
})
fastify.post('/body-limit-40k', {
bodyLimit: 40000,
onError: async (req, res, err) => {
t.fail('should not be called')
}
}, (request, reply) => {
reply.send({ x: request.body.x })
})
fastify.post('/body-limit-20k', {
bodyLimit: 20000,
onError: async (req, res, err) => {
t.assert.strictEqual(err.code, 'FST_ERR_CTP_BODY_TOO_LARGE')
t.assert.strictEqual(err.statusCode, 413)
}
}, (request, reply) => {
reply.send({ x: 'handler should not be called' })
})
await t.test('bodyLimit 40k', async (t) => {
const result = await fastify.inject({
method: 'POST',
url: '/body-limit-40k',
headers: {
'content-encoding': 'gzip',
'content-type': 'application/json'
},
payload: encoded
})
t.assert.strictEqual(result.statusCode, 200)
t.assert.deepStrictEqual(result.json(), body)
})
await t.test('bodyLimit 20k', async (t) => {
const result = await fastify.inject({
method: 'POST',
url: '/body-limit-20k',
headers: {
'content-encoding': 'gzip',
'content-type': 'application/json'
},
payload: encoded
})
t.assert.strictEqual(result.statusCode, 413)
})
})
test('default request.routeOptions.bodyLimit should be 1048576', (t, done) => {
t.plan(4)
const fastify = Fastify()
fastify.post('/default-bodylimit', {
handler (request, reply) {
t.assert.strictEqual(1048576, request.routeOptions.bodyLimit)
reply.send({ })
}
})
fastify.listen({ port: 0 }, function (err) {
t.assert.ifError(err)
t.after(() => { fastify.close() })
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port + '/default-bodylimit',
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
done()
})
})
})
test('request.routeOptions.bodyLimit should be equal to route limit', (t, done) => {
t.plan(4)
const fastify = Fastify({ bodyLimit: 1 })
fastify.post('/route-limit', {
bodyLimit: 1000,
handler (request, reply) {
t.assert.strictEqual(1000, request.routeOptions.bodyLimit)
reply.send({})
}
})
fastify.listen({ port: 0 }, function (err) {
t.assert.ifError(err)
t.after(() => { fastify.close() })
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port + '/route-limit',
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
done()
})
})
})
test('request.routeOptions.bodyLimit should be equal to server limit', (t, done) => {
t.plan(4)
const fastify = Fastify({ bodyLimit: 100 })
fastify.post('/server-limit', {
handler (request, reply) {
t.assert.strictEqual(100, request.routeOptions.bodyLimit)
reply.send({})
}
})
fastify.listen({ port: 0 }, function (err) {
t.assert.ifError(err)
t.after(() => { fastify.close() })
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port + '/server-limit',
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.assert.ifError(err)
t.assert.strictEqual(response.statusCode, 200)
done()
})
})
})