@@ -38,6 +38,67 @@ describe('http', () => {
3838 } )
3939 } )
4040
41+ it ( 'should include status code and response with HTTP Error' , ( ) => {
42+ xapp = xmock ( )
43+ xapp . get ( 'http://swagger.io' , ( req , res ) => res . status ( 400 ) . send ( 'hi' ) )
44+
45+ return http ( {
46+ url : 'http://swagger.io'
47+ } )
48+ . then (
49+ ( res ) => {
50+ throw new Error ( 'Expected rejection for HTTP status 400' )
51+ } ,
52+ ( err ) => {
53+ expect ( err . status ) . toEqual ( 400 )
54+ expect ( err . statusCode ) . toEqual ( 400 )
55+ expect ( err . response . text ) . toEqual ( 'hi' )
56+ }
57+ )
58+ } )
59+
60+ it ( 'should apply responseInterceptor to error responses' , ( ) => {
61+ xapp = xmock ( )
62+ xapp . get ( 'http://swagger.io' , ( req , res ) => res . status ( 400 ) . send ( 'hi' ) )
63+
64+ return http ( {
65+ url : 'http://swagger.io' ,
66+ responseInterceptor : ( res ) => {
67+ res . testValue = 5
68+ }
69+ } )
70+ . then (
71+ ( res ) => {
72+ throw new Error ( 'Expected rejection for HTTP status 400' )
73+ } ,
74+ ( err ) => {
75+ expect ( err . response . testValue ) . toEqual ( 5 )
76+ }
77+ )
78+ } )
79+
80+ it ( 'should set responseError on responseInterceptor Error' , ( ) => {
81+ xapp = xmock ( )
82+ xapp . get ( 'http://swagger.io' , ( req , res ) => res . status ( 400 ) . send ( 'hi' ) )
83+
84+ const testError = new Error ( )
85+ return http ( {
86+ url : 'http://swagger.io' ,
87+ responseInterceptor : ( res ) => {
88+ throw testError
89+ }
90+ } )
91+ . then (
92+ ( res ) => {
93+ throw new Error ( 'Expected rejection for HTTP status 400' )
94+ } ,
95+ ( err ) => {
96+ expect ( err . response ) . toEqual ( null )
97+ expect ( err . responseError ) . toBe ( testError )
98+ }
99+ )
100+ } )
101+
41102 describe ( 'serializeHeaders' , function ( ) {
42103 it ( 'should handle FetchAPI Headers object, which is iterable' , function ( ) {
43104 // Given
0 commit comments