@@ -144,3 +144,146 @@ describe('request', () => {
144144 } ) ;
145145 } ) ;
146146} ) ;
147+
148+ describe ( 'get encoding' , ( ) => {
149+ it ( 'should return binary by default' , ( ) => {
150+ const result = request . getEncoding ( null ) ;
151+
152+ should ( result ) . be . eql ( 'binary' ) ;
153+ } ) ;
154+
155+ it ( 'should return binary when no content-type header supplies' , ( ) => {
156+ const result = request . getEncoding ( {
157+ headers : { }
158+ } ) ;
159+
160+ should ( result ) . be . eql ( 'binary' ) ;
161+ } ) ;
162+
163+ it ( 'should return binary when content type header doesn\'t include utf-8' , ( ) => {
164+ const result = request . getEncoding ( {
165+ headers : { }
166+ } ) ;
167+
168+ should ( result ) . be . eql ( 'binary' ) ;
169+ } ) ;
170+
171+ it ( 'should return binary when content type header doesn\'t include utf-8' , ( ) => {
172+ const result = request . getEncoding ( {
173+ headers : {
174+ 'content-type' : 'text/html'
175+ }
176+ } ) ;
177+
178+ should ( result ) . be . eql ( 'binary' ) ;
179+ } ) ;
180+
181+ it ( 'should return utf8 when content type includes utf-8' , ( ) => {
182+ const result = request . getEncoding ( {
183+ headers : {
184+ 'content-type' : 'text/html; charset=utf-8'
185+ }
186+ } ) ;
187+
188+ should ( result ) . be . eql ( 'utf8' ) ;
189+ } ) ;
190+
191+ it ( 'should return utf8 response object includes it' , ( ) => {
192+ const result = request . getEncoding ( {
193+ encoding : 'utf8'
194+ } ) ;
195+
196+ should ( result ) . be . eql ( 'utf8' ) ;
197+ } ) ;
198+ } ) ;
199+
200+ describe ( 'transformResult' , ( ) => {
201+ it ( 'should throw with weird shaped response' , ( ) => {
202+ try {
203+ request . transformResult ( [ 1 , 2 , 3 ] ) ;
204+
205+ // We shouldn't get here.
206+ should ( true ) . eql ( false ) ;
207+ } catch ( e ) {
208+ should ( e ) . be . instanceOf ( Error ) ;
209+ should ( e . message ) . eql ( 'Wrong response handler result. Expected string or object, but received array' ) ;
210+ }
211+ } ) ;
212+
213+ it ( 'should pass through error' , ( ) => {
214+ try {
215+ request . transformResult ( new Error ( 'Oh no' ) ) ;
216+
217+ // We shouldn't get here.
218+ should ( true ) . eql ( false ) ;
219+ } catch ( e ) {
220+ should ( e ) . be . instanceOf ( Error ) ;
221+ should ( e . message ) . eql ( 'Oh no' ) ;
222+ }
223+ } ) ;
224+
225+ it ( 'should throw with boolean input' , ( ) => {
226+ try {
227+ request . transformResult ( true ) ;
228+
229+ // We shouldn't get here.
230+ should ( true ) . eql ( false ) ;
231+ } catch ( e ) {
232+ should ( e ) . be . instanceOf ( Error ) ;
233+ should ( e . message ) . eql ( 'Wrong response handler result. Expected string or object, but received boolean' ) ;
234+ }
235+ } ) ;
236+
237+ it ( 'should handle object' , ( ) => {
238+ const result = request . transformResult ( {
239+ body : 'SOME BODY' ,
240+ encoding : 'utf8' ,
241+ metadata : { foo : 'bar' }
242+ } ) ;
243+
244+ should ( result ) . have . property ( 'body' , 'SOME BODY' ) ;
245+ should ( result ) . have . property ( 'encoding' , 'utf8' ) ;
246+ should ( result ) . have . property ( 'metadata' , { foo : 'bar' } ) ;
247+ } ) ;
248+
249+ it ( 'should handle object with empty body string' , ( ) => {
250+ const result = request . transformResult ( {
251+ body : '' ,
252+ encoding : 'utf8' ,
253+ } ) ;
254+
255+ should ( result ) . have . property ( 'body' , '' ) ;
256+ should ( result ) . have . property ( 'encoding' , 'utf8' ) ;
257+ should ( result ) . have . property ( 'metadata' , null ) ;
258+ } ) ;
259+
260+ it ( 'should handle object with defaults and buffer body' , ( ) => {
261+ const result = request . transformResult ( {
262+ body : Buffer . from ( 'SOME BODY' ) ,
263+ } ) ;
264+
265+ should ( result ) . have . property ( 'body' , 'SOME BODY' ) ;
266+ should ( result ) . have . property ( 'encoding' , 'binary' ) ;
267+ should ( result ) . have . property ( 'metadata' , null ) ;
268+ } ) ;
269+
270+ it ( 'should handle raw string input' , ( ) => {
271+ const result = request . transformResult ( 'SOME BODY' ) ;
272+
273+ should ( result ) . have . property ( 'body' , 'SOME BODY' ) ;
274+ should ( result ) . have . property ( 'encoding' , 'binary' ) ;
275+ should ( result ) . have . property ( 'metadata' , null ) ;
276+ } ) ;
277+
278+ it ( 'should handle null input' , ( ) => {
279+ const result = request . transformResult ( null ) ;
280+
281+ should ( result ) . eqls ( null ) ;
282+ } ) ;
283+
284+ it ( 'should handle undefined input' , ( ) => {
285+ const result = request . transformResult ( undefined ) ;
286+
287+ should ( result ) . eqls ( null ) ;
288+ } ) ;
289+ } ) ;
0 commit comments