1
1
'use strict'
2
2
3
3
const Fastify = require ( '../fastify' )
4
- const sget = require ( 'simple-get' ) . concat
5
4
const zlib = require ( 'node:zlib' )
6
5
const { test } = require ( 'node:test' )
7
6
8
- test ( 'bodyLimit' , ( t , done ) => {
9
- t . plan ( 5 )
7
+ test ( 'bodyLimit' , async t => {
8
+ t . plan ( 4 )
10
9
11
10
try {
12
11
Fastify ( { bodyLimit : 1.3 } )
@@ -28,22 +27,17 @@ test('bodyLimit', (t, done) => {
28
27
reply . send ( { error : 'handler should not be called' } )
29
28
} )
30
29
31
- fastify . listen ( { port : 0 } , function ( err ) {
32
- t . assert . ifError ( err )
33
- t . after ( ( ) => { fastify . close ( ) } )
30
+ const fastifyServer = await fastify . listen ( { port : 0 } )
31
+ t . after ( ( ) => { fastify . close ( ) } )
34
32
35
- sget ( {
36
- method : 'POST' ,
37
- url : 'http://localhost:' + fastify . server . address ( ) . port ,
38
- headers : { 'Content-Type' : 'application/json' } ,
39
- body : [ ] ,
40
- json : true
41
- } , ( err , response , body ) => {
42
- t . assert . ifError ( err )
43
- t . assert . strictEqual ( response . statusCode , 413 )
44
- done ( )
45
- } )
33
+ const result = await fetch ( fastifyServer , {
34
+ method : 'POST' ,
35
+ headers : { 'Content-Type' : 'application/json' } ,
36
+ body : JSON . stringify ( [ ] )
46
37
} )
38
+
39
+ t . assert . ok ( ! result . ok )
40
+ t . assert . strictEqual ( result . status , 413 )
47
41
} )
48
42
49
43
test ( 'bodyLimit is applied to decoded content' , async ( t ) => {
@@ -114,35 +108,29 @@ test('bodyLimit is applied to decoded content', async (t) => {
114
108
} )
115
109
} )
116
110
117
- test ( 'default request.routeOptions.bodyLimit should be 1048576' , ( t , done ) => {
118
- t . plan ( 4 )
111
+ test ( 'default request.routeOptions.bodyLimit should be 1048576' , async t => {
112
+ t . plan ( 3 )
119
113
const fastify = Fastify ( )
120
114
fastify . post ( '/default-bodylimit' , {
121
115
handler ( request , reply ) {
122
116
t . assert . strictEqual ( 1048576 , request . routeOptions . bodyLimit )
123
117
reply . send ( { } )
124
118
}
125
119
} )
126
- fastify . listen ( { port : 0 } , function ( err ) {
127
- t . assert . ifError ( err )
128
- t . after ( ( ) => { fastify . close ( ) } )
120
+ const fastifyServer = await fastify . listen ( { port : 0 } )
121
+ t . after ( ( ) => { fastify . close ( ) } )
129
122
130
- sget ( {
131
- method : 'POST' ,
132
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/default-bodylimit' ,
133
- headers : { 'Content-Type' : 'application/json' } ,
134
- body : [ ] ,
135
- json : true
136
- } , ( err , response , body ) => {
137
- t . assert . ifError ( err )
138
- t . assert . strictEqual ( response . statusCode , 200 )
139
- done ( )
140
- } )
123
+ const result = await fetch ( fastifyServer + '/default-bodylimit' , {
124
+ method : 'POST' ,
125
+ headers : { 'Content-Type' : 'application/json' } ,
126
+ body : JSON . stringify ( [ ] )
141
127
} )
128
+ t . assert . ok ( result . ok )
129
+ t . assert . strictEqual ( result . status , 200 )
142
130
} )
143
131
144
- test ( 'request.routeOptions.bodyLimit should be equal to route limit' , ( t , done ) => {
145
- t . plan ( 4 )
132
+ test ( 'request.routeOptions.bodyLimit should be equal to route limit' , async t => {
133
+ t . plan ( 3 )
146
134
const fastify = Fastify ( { bodyLimit : 1 } )
147
135
fastify . post ( '/route-limit' , {
148
136
bodyLimit : 1000 ,
@@ -151,47 +139,35 @@ test('request.routeOptions.bodyLimit should be equal to route limit', (t, done)
151
139
reply . send ( { } )
152
140
}
153
141
} )
154
- fastify . listen ( { port : 0 } , function ( err ) {
155
- t . assert . ifError ( err )
156
- t . after ( ( ) => { fastify . close ( ) } )
142
+ const fastifyServer = await fastify . listen ( { port : 0 } )
143
+ t . after ( ( ) => { fastify . close ( ) } )
157
144
158
- sget ( {
159
- method : 'POST' ,
160
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/route-limit' ,
161
- headers : { 'Content-Type' : 'application/json' } ,
162
- body : [ ] ,
163
- json : true
164
- } , ( err , response , body ) => {
165
- t . assert . ifError ( err )
166
- t . assert . strictEqual ( response . statusCode , 200 )
167
- done ( )
168
- } )
145
+ const result = await fetch ( fastifyServer + '/route-limit' , {
146
+ method : 'POST' ,
147
+ headers : { 'Content-Type' : 'application/json' } ,
148
+ body : JSON . stringify ( [ ] )
169
149
} )
150
+ t . assert . ok ( result . ok )
151
+ t . assert . strictEqual ( result . status , 200 )
170
152
} )
171
153
172
- test ( 'request.routeOptions.bodyLimit should be equal to server limit' , ( t , done ) => {
173
- t . plan ( 4 )
154
+ test ( 'request.routeOptions.bodyLimit should be equal to server limit' , async t => {
155
+ t . plan ( 3 )
174
156
const fastify = Fastify ( { bodyLimit : 100 } )
175
157
fastify . post ( '/server-limit' , {
176
158
handler ( request , reply ) {
177
159
t . assert . strictEqual ( 100 , request . routeOptions . bodyLimit )
178
160
reply . send ( { } )
179
161
}
180
162
} )
181
- fastify . listen ( { port : 0 } , function ( err ) {
182
- t . assert . ifError ( err )
183
- t . after ( ( ) => { fastify . close ( ) } )
163
+ const fastifyServer = await fastify . listen ( { port : 0 } )
164
+ t . after ( ( ) => { fastify . close ( ) } )
184
165
185
- sget ( {
186
- method : 'POST' ,
187
- url : 'http://localhost:' + fastify . server . address ( ) . port + '/server-limit' ,
188
- headers : { 'Content-Type' : 'application/json' } ,
189
- body : [ ] ,
190
- json : true
191
- } , ( err , response , body ) => {
192
- t . assert . ifError ( err )
193
- t . assert . strictEqual ( response . statusCode , 200 )
194
- done ( )
195
- } )
166
+ const result = await fetch ( fastifyServer + '/server-limit' , {
167
+ method : 'POST' ,
168
+ headers : { 'Content-Type' : 'application/json' } ,
169
+ body : JSON . stringify ( [ ] )
196
170
} )
171
+ t . assert . ok ( result . ok )
172
+ t . assert . strictEqual ( result . status , 200 )
197
173
} )
0 commit comments