@@ -8,6 +8,7 @@ const silentLog = require('../silentlog.js')
88const ssri = require ( 'ssri' )
99const test = require ( 'tap' ) . test
1010const tnock = require ( './util/tnock.js' )
11+ const zlib = require ( 'zlib' )
1112
1213const fetch = require ( '../index.js' )
1314
@@ -117,6 +118,98 @@ test('stream body param', t => {
117118 . then ( json => t . deepEqual ( json , { hello : 'world' } ) )
118119} )
119120
121+ test ( 'JSON body param' , t => {
122+ tnock ( t , OPTS . registry )
123+ . matchHeader ( 'content-type' , ctype => {
124+ t . equal ( ctype [ 0 ] , 'application/json' , 'content-type automatically set' )
125+ return ctype [ 0 ] === 'application/json'
126+ } )
127+ . matchHeader ( 'content-encoding' , enc => {
128+ t . equal ( enc [ 0 ] , 'gzip' , 'content-encoding automatically set' )
129+ return enc [ 0 ] === 'gzip'
130+ } )
131+ . post ( '/hello' )
132+ // NOTE: can't really test the body itself here because nock freaks out.
133+ . reply ( 200 )
134+ const opts = Object . assign ( {
135+ method : 'POST' ,
136+ body : { hello : 'world' } ,
137+ gzip : true
138+ } , OPTS )
139+ return fetch ( '/hello' , opts )
140+ . then ( res => {
141+ t . equal ( res . status , 200 , 'request succeeded' )
142+ } )
143+ } )
144+
145+ test ( 'gzip + buffer body param' , t => {
146+ tnock ( t , OPTS . registry )
147+ . matchHeader ( 'content-type' , ctype => {
148+ t . equal ( ctype [ 0 ] , 'application/octet-stream' , 'content-type automatically set' )
149+ return ctype [ 0 ] === 'application/octet-stream'
150+ } )
151+ . matchHeader ( 'content-encoding' , enc => {
152+ t . equal ( enc [ 0 ] , 'gzip' , 'content-encoding automatically set' )
153+ return enc [ 0 ] === 'gzip'
154+ } )
155+ . post ( '/hello' )
156+ . reply ( 200 , ( uri , reqBody ) => {
157+ reqBody = zlib . gunzipSync ( Buffer . from ( reqBody , 'hex' ) )
158+ t . deepEqual (
159+ Buffer . from ( reqBody , 'utf8' ) . toString ( 'utf8' ) ,
160+ 'hello' ,
161+ 'got the JSON version of the body'
162+ )
163+ return reqBody
164+ } )
165+ const opts = Object . assign ( {
166+ method : 'POST' ,
167+ body : Buffer . from ( 'hello' , 'utf8' ) ,
168+ gzip : true
169+ } , OPTS )
170+ return fetch ( '/hello' , opts )
171+ . then ( res => {
172+ t . equal ( res . status , 200 )
173+ return res . buffer ( )
174+ } )
175+ . then ( buf =>
176+ t . deepEqual ( buf , Buffer . from ( 'hello' , 'utf8' ) , 'got response' )
177+ )
178+ } )
179+
180+ test ( 'gzip + stream body param' , t => {
181+ tnock ( t , OPTS . registry )
182+ . matchHeader ( 'content-type' , ctype => {
183+ t . equal ( ctype [ 0 ] , 'application/octet-stream' , 'content-type automatically set' )
184+ return ctype [ 0 ] === 'application/octet-stream'
185+ } )
186+ . matchHeader ( 'content-encoding' , enc => {
187+ t . equal ( enc [ 0 ] , 'gzip' , 'content-encoding automatically set' )
188+ return enc [ 0 ] === 'gzip'
189+ } )
190+ . post ( '/hello' )
191+ . reply ( 200 , ( uri , reqBody ) => {
192+ reqBody = zlib . gunzipSync ( Buffer . from ( reqBody , 'hex' ) )
193+ t . deepEqual ( JSON . parse ( reqBody . toString ( 'utf8' ) ) , {
194+ hello : 'world'
195+ } , 'got the stringified version of the body' )
196+ return reqBody
197+ } )
198+ const stream = new PassThrough ( )
199+ setImmediate ( ( ) => stream . end ( JSON . stringify ( { hello : 'world' } ) ) )
200+ const opts = Object . assign ( {
201+ method : 'POST' ,
202+ body : stream ,
203+ gzip : true
204+ } , OPTS )
205+ return fetch ( '/hello' , opts )
206+ . then ( res => {
207+ t . equal ( res . status , 200 )
208+ return res . json ( )
209+ } )
210+ . then ( json => t . deepEqual ( json , { hello : 'world' } ) )
211+ } )
212+
120213test ( 'query strings' , t => {
121214 tnock ( t , OPTS . registry )
122215 . get ( '/hello?hi=there&who=wor%20ld' )
0 commit comments