@@ -154,4 +154,70 @@ describe('when proxy request is a POST', function () {
154154 . end ( done ) ;
155155 } ) ;
156156
157+ describe ( 'when body-parser.json() is using strict=false' , function ( ) {
158+ beforeEach ( function ( ) {
159+ localServer = createLocalApplicationServer ( ) ;
160+ localServer . use ( bodyParser . json ( { strict : false } ) ) ;
161+ } ) ;
162+
163+ var testCases = [
164+ { value : false } ,
165+ { value : null } ,
166+ { value : '' } ,
167+ ] ;
168+
169+ testCases . forEach ( function ( test ) {
170+ var valueString = JSON . stringify ( test . value ) ;
171+
172+ it ( 'errors when body is ' + valueString , function ( done ) {
173+ localServer . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' ) ) ;
174+ localServer . use ( function ( err , req , res , next ) { res . send ( err ) ; next ( ) ; } ) ;
175+
176+ request ( localServer )
177+ . post ( '/proxy' )
178+ . send ( valueString )
179+ . set ( 'Content-Type' , 'application/json' )
180+ . expect ( function ( res ) {
181+ assert (
182+ res . text === (
183+ 'Tried to parse body after request body has already been read.' +
184+ ' Try setting parseReqBody to false and manually specify the body' +
185+ ' you want to send in decorateProxyReqBody.'
186+ )
187+ ) ;
188+ } )
189+ . end ( done ) ;
190+ } ) ;
191+
192+ it (
193+ 'succeeds when parseReqBody=false and proxyReqBodyDecorator explicitly returns ' + valueString ,
194+ function ( done ) {
195+ var scope = nock ( 'http://127.0.0.1:12345' )
196+ . post ( '/' , valueString )
197+ . matchHeader ( 'Content-Type' , 'application/json' )
198+ . reply ( 200 , valueString , {
199+ 'Content-Type' : 'application/json' ,
200+ } ) ;
201+
202+ localServer . use ( '/proxy' , proxy ( 'http://127.0.0.1:12345' , {
203+ parseReqBody : false ,
204+ proxyReqBodyDecorator : function ( ) {
205+ return valueString ;
206+ } ,
207+ } ) ) ;
208+
209+ request ( localServer )
210+ . post ( '/proxy' )
211+ . send ( valueString )
212+ . set ( 'Content-Type' , 'application/json' )
213+ . expect ( function ( res ) {
214+ assert ( res . body === test . value ) ;
215+ scope . done ( ) ;
216+ } )
217+ . end ( done ) ;
218+ }
219+ ) ;
220+ } ) ;
221+ } ) ;
222+
157223} ) ;
0 commit comments