3
3
const tap = require ( 'tap' )
4
4
const test = tap . test
5
5
const Fastify = require ( 'fastify' )
6
- const request = require ( 'request' )
7
6
const plugin = require ( '../' )
8
7
const qs = require ( 'qs' )
8
+ const formAutoContent = require ( 'form-auto-content' )
9
9
10
10
test ( 'succes route succeeds' , ( t ) => {
11
11
t . plan ( 3 )
@@ -20,15 +20,10 @@ test('succes route succeeds', (t) => {
20
20
if ( err ) tap . error ( err )
21
21
fastify . server . unref ( )
22
22
23
- const reqOpts = {
24
- method : 'POST' ,
25
- baseUrl : 'http://localhost:' + fastify . server . address ( ) . port
26
- }
27
- const req = request . defaults ( reqOpts )
28
- req ( { uri : '/test1' , form : { foo : 'foo' } } , ( err , response , body ) => {
23
+ fastify . inject ( { path : '/test1' , method : 'POST' , ...formAutoContent ( { foo : 'foo' } ) } , ( err , response ) => {
29
24
t . error ( err )
30
25
t . equal ( response . statusCode , 200 )
31
- t . same ( JSON . parse ( body ) , { foo : 'foo' , message : 'done' } )
26
+ t . same ( JSON . parse ( response . body ) , { foo : 'foo' , message : 'done' } )
32
27
} )
33
28
} )
34
29
} )
@@ -46,16 +41,11 @@ test('cannot exceed body limit', (t) => {
46
41
if ( err ) tap . error ( err )
47
42
fastify . server . unref ( )
48
43
49
- const reqOpts = {
50
- method : 'POST' ,
51
- baseUrl : 'http://localhost:' + fastify . server . address ( ) . port
52
- }
53
- const req = request . defaults ( reqOpts )
54
44
const payload = require ( 'crypto' ) . randomBytes ( 128 ) . toString ( 'hex' )
55
- req ( { uri : '/limited' , form : { foo : payload } } , ( err , response , body ) => {
45
+ fastify . inject ( { path : '/limited' , method : 'POST' , ... formAutoContent ( { foo : payload } ) } , ( err , response ) => {
56
46
t . error ( err )
57
47
t . equal ( response . statusCode , 413 )
58
- t . equal ( JSON . parse ( body ) . message , 'Request body is too large' )
48
+ t . equal ( JSON . parse ( response . body ) . message , 'Request body is too large' )
59
49
} )
60
50
} )
61
51
} )
@@ -80,25 +70,17 @@ test('cannot exceed body limit when Content-Length is not available', (t) => {
80
70
if ( err ) tap . error ( err )
81
71
fastify . server . unref ( )
82
72
83
- const reqOpts = {
84
- method : 'POST' ,
85
- baseUrl : 'http://localhost:' + fastify . server . address ( ) . port ,
86
- headers : {
87
- 'content-type' : 'application/x-www-form-urlencoded'
88
- }
89
- }
90
- const req = request . defaults ( reqOpts )
91
73
let sent = false
92
74
const payload = require ( 'stream' ) . Readable ( {
93
75
read : function ( ) {
94
76
this . push ( sent ? null : Buffer . alloc ( 70000 , 'a' ) )
95
77
sent = true
96
78
}
97
79
} )
98
- req ( { uri : '/limited' , body : payload } , ( err , response , body ) => {
80
+ fastify . inject ( { path : '/limited' , method : 'POST' , headers : { 'content-type' : 'application/x-www-form-urlencoded' } , body : payload } , ( err , response ) => {
99
81
t . error ( err )
100
82
t . equal ( response . statusCode , 413 )
101
- t . equal ( JSON . parse ( body ) . message , 'Request body is too large' )
83
+ t . equal ( JSON . parse ( response . body ) . message , 'Request body is too large' )
102
84
} )
103
85
} )
104
86
} )
@@ -116,16 +98,11 @@ test('cannot exceed body limit set on Fastify instance', (t) => {
116
98
if ( err ) tap . error ( err )
117
99
fastify . server . unref ( )
118
100
119
- const reqOpts = {
120
- method : 'POST' ,
121
- baseUrl : 'http://localhost:' + fastify . server . address ( ) . port
122
- }
123
- const req = request . defaults ( reqOpts )
124
101
const payload = require ( 'crypto' ) . randomBytes ( 128 ) . toString ( 'hex' )
125
- req ( { uri : '/limited' , form : { foo : payload } } , ( err , response , body ) => {
102
+ fastify . inject ( { path : '/limited' , method : 'POST' , ... formAutoContent ( { foo : payload } ) } , ( err , response ) => {
126
103
t . error ( err )
127
104
t . equal ( response . statusCode , 413 )
128
- t . equal ( JSON . parse ( body ) . message , 'Request body is too large' )
105
+ t . equal ( JSON . parse ( response . body ) . message , 'Request body is too large' )
129
106
} )
130
107
} )
131
108
} )
@@ -143,16 +120,11 @@ test('plugin bodyLimit should overwrite Fastify instance bodyLimit', (t) => {
143
120
if ( err ) tap . error ( err )
144
121
fastify . server . unref ( )
145
122
146
- const reqOpts = {
147
- method : 'POST' ,
148
- baseUrl : 'http://localhost:' + fastify . server . address ( ) . port
149
- }
150
- const req = request . defaults ( reqOpts )
151
123
const payload = require ( 'crypto' ) . randomBytes ( 128 ) . toString ( 'hex' )
152
- req ( { uri : '/limited' , form : { foo : payload } } , ( err , response , body ) => {
124
+ fastify . inject ( { path : '/limited' , method : 'POST' , ... formAutoContent ( { foo : payload } ) } , ( err , response ) => {
153
125
t . error ( err )
154
126
t . equal ( response . statusCode , 413 )
155
- t . equal ( JSON . parse ( body ) . message , 'Request body is too large' )
127
+ t . equal ( JSON . parse ( response . body ) . message , 'Request body is too large' )
156
128
} )
157
129
} )
158
130
} )
@@ -181,15 +153,10 @@ test('plugin should not parse nested objects by default', (t) => {
181
153
if ( err ) tap . error ( err )
182
154
fastify . server . unref ( )
183
155
184
- const reqOpts = {
185
- method : 'POST' ,
186
- baseUrl : 'http://localhost:' + fastify . server . address ( ) . port
187
- }
188
- const req = request . defaults ( reqOpts )
189
- req ( { uri : '/test1' , form : { 'foo[one]' : 'foo' , 'foo[two]' : 'bar' } } , ( err , response , body ) => {
156
+ fastify . inject ( { path : '/test1' , method : 'POST' , ...formAutoContent ( { 'foo[one]' : 'foo' , 'foo[two]' : 'bar' } ) } , ( err , response ) => {
190
157
t . error ( err )
191
158
t . equal ( response . statusCode , 200 )
192
- t . same ( JSON . parse ( body ) , { 'foo[one]' : 'foo' , 'foo[two]' : 'bar' , message : 'done' } )
159
+ t . same ( JSON . parse ( response . body ) , { 'foo[one]' : 'foo' , 'foo[two]' : 'bar' , message : 'done' } )
193
160
} )
194
161
} )
195
162
} )
@@ -208,15 +175,10 @@ test('plugin should allow providing custom parser as option', (t) => {
208
175
if ( err ) tap . error ( err )
209
176
fastify . server . unref ( )
210
177
211
- const reqOpts = {
212
- method : 'POST' ,
213
- baseUrl : 'http://localhost:' + fastify . server . address ( ) . port
214
- }
215
- const req = request . defaults ( reqOpts )
216
- req ( { uri : '/test1' , form : { 'foo[one]' : 'foo' , 'foo[two]' : 'bar' } } , ( err , response , body ) => {
178
+ fastify . inject ( { path : '/test1' , method : 'POST' , ...formAutoContent ( { 'foo[one]' : 'foo' , 'foo[two]' : 'bar' } ) } , ( err , response ) => {
217
179
t . error ( err )
218
180
t . equal ( response . statusCode , 200 )
219
- t . same ( JSON . parse ( body ) , { foo : { one : 'foo' , two : 'bar' } , message : 'done' } )
181
+ t . same ( JSON . parse ( response . body ) , { foo : { one : 'foo' , two : 'bar' } , message : 'done' } )
220
182
} )
221
183
} )
222
184
} )
0 commit comments