@@ -20,7 +20,7 @@ test('properties', withServer, async (t, server, got) => {
2020
2121 const url = new URL ( server . url ) ;
2222
23- const error = ( await t . throwsAsync < HTTPError > ( got ( '' ) ) ) ! ;
23+ const error = ( await t . throwsAsync < HTTPError < string > > ( got ( '' ) ) ) ! ;
2424 t . truthy ( error ) ;
2525 t . truthy ( error . response ) ;
2626 t . truthy ( error . options ) ;
@@ -30,11 +30,12 @@ test('properties', withServer, async (t, server, got) => {
3030 t . is ( error . message , 'Response code 404 (Not Found)' ) ;
3131 t . deepEqual ( error . options . url , url ) ;
3232 t . is ( error . response . headers . connection , 'keep-alive' ) ;
33- t . is ( error . response . body , 'not' ) ;
33+ // Assert is used for type checking
34+ t . assert ( error . response . body === 'not' ) ;
3435} ) ;
3536
3637test ( 'catches dns errors' , async t => {
37- const error = ( await t . throwsAsync < RequestError > ( got ( 'http://doesntexist' , { retry : { limit : 0 } } ) ) ) ! ;
38+ const error = ( await t . throwsAsync < RequestError < undefined > > ( got ( 'http://doesntexist' , { retry : { limit : 0 } } ) ) ) ! ;
3839 t . truthy ( error ) ;
3940 t . regex ( error . message , / E N O T F O U N D | E A I _ A G A I N / ) ;
4041 t . is ( ( error . options . url as URL ) . host , 'doesntexist' ) ;
@@ -110,7 +111,27 @@ test('custom body', withServer, async (t, server, got) => {
110111 message : 'Response code 404 (Not Found)' ,
111112 } ) ;
112113 t . is ( error ?. response . statusCode , 404 ) ;
113- t . is ( error ?. response . body , 'not' ) ;
114+ // Typecheck for default `any` type
115+ t . assert ( error ?. response . body === 'not' ) ;
116+ } ) ;
117+
118+ test ( 'custom json body' , withServer , async ( t , server , got ) => {
119+ server . get ( '/' , ( _request , response ) => {
120+ response . statusCode = 404 ;
121+ response . header ( 'content-type' , 'application/json' ) ;
122+ response . end ( JSON . stringify ( {
123+ message : 'not found' ,
124+ } ) ) ;
125+ } ) ;
126+
127+ const error = await t . throwsAsync < HTTPError < { message : string } > > ( got ( '' , { responseType : 'json' } ) ,
128+ {
129+ instanceOf : HTTPError ,
130+ message : 'Response code 404 (Not Found)' ,
131+ } ) ;
132+ t . is ( error ?. response . statusCode , 404 ) ;
133+ // Assert is used for body typecheck
134+ t . assert ( error ?. response . body . message === 'not found' ) ;
114135} ) ;
115136
116137test ( 'contains Got options' , withServer , async ( t , server , got ) => {
0 commit comments