/
bodyLimit.test.js
125 lines (111 loc) · 3 KB
/
bodyLimit.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
'use strict'
const Fastify = require('..')
const sget = require('simple-get').concat
const t = require('tap')
const test = t.test
test('bodyLimit', t => {
t.plan(5)
try {
Fastify({ bodyLimit: 1.3 })
t.fail('option must be an integer')
} catch (err) {
t.ok(err)
}
try {
Fastify({ bodyLimit: [] })
t.fail('option must be an integer')
} catch (err) {
t.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.error(err)
t.teardown(() => { fastify.close() })
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
headers: { 'Content-Type': 'application/json' },
body: [],
json: true
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 413)
})
})
})
test('default request.routeOptions.bodyLimit should be 1048576', t => {
t.plan(4)
const fastify = Fastify()
fastify.post('/default-bodylimit', {
handler (request, reply) {
t.equal(1048576, request.routeOptions.bodyLimit)
reply.send({ })
}
})
fastify.listen({ port: 0 }, function (err) {
t.error(err)
t.teardown(() => { 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.error(err)
t.equal(response.statusCode, 200)
})
})
})
test('request.routeOptions.bodyLimit should be equal to route limit', t => {
t.plan(4)
const fastify = Fastify({ bodyLimit: 1 })
fastify.post('/route-limit', {
bodyLimit: 1000,
handler (request, reply) {
t.equal(1000, request.routeOptions.bodyLimit)
reply.send({})
}
})
fastify.listen({ port: 0 }, function (err) {
t.error(err)
t.teardown(() => { 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.error(err)
t.equal(response.statusCode, 200)
})
})
})
test('request.routeOptions.bodyLimit should be equal to server limit', t => {
t.plan(4)
const fastify = Fastify({ bodyLimit: 100 })
fastify.post('/server-limit', {
handler (request, reply) {
t.equal(100, request.routeOptions.bodyLimit)
reply.send({})
}
})
fastify.listen({ port: 0 }, function (err) {
t.error(err)
t.teardown(() => { 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.error(err)
t.equal(response.statusCode, 200)
})
})
})