@@ -6,6 +6,7 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
66 const mockNextHandler = jest . fn ( ) ;
77 const mockStreamCollector = jest . fn ( ) ;
88 const mockUtf8Encoder = jest . fn ( ) ;
9+ const mockResponse = jest . fn ( ) ;
910 const mockConfig = {
1011 streamCollector : mockStreamCollector ,
1112 utf8Encoder : mockUtf8Encoder ,
@@ -15,58 +16,80 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
1516 jest . clearAllMocks ( ) ;
1617 } ) ;
1718
18- it ( "should throw if response body is empty" , async ( ) => {
19- expect . assertions ( 3 ) ;
19+ describe ( "tests for statusCode < 200 and >= 300" , ( ) => {
2020 mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
2121 mockUtf8Encoder . mockReturnValue ( "" ) ;
22- mockNextHandler . mockReturnValue ( {
23- response : new HttpResponse ( {
24- statusCode : 200 ,
22+
23+ it . each ( [ 199 , 300 ] ) ( "results for statusCode %i" , async ( statusCode ) => {
24+ mockNextHandler . mockReturnValue ( {
25+ response : mockResponse ,
26+ statusCode,
2527 headers : { } ,
26- body : "" ,
27- } ) ,
28- } ) ;
29- const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
30- try {
31- await handler ( {
28+ body : "body" ,
29+ } ) ;
30+ const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
31+ const result = await handler ( {
3232 input : { } ,
3333 request : new HttpRequest ( {
3434 hostname : "s3.us-east-1.amazonaws.com" ,
3535 } ) ,
3636 } ) ;
37- } catch ( e ) {
38- expect ( e ) . toBeDefined ( ) ;
39- expect ( e . name ) . toEqual ( "InternalError" ) ;
40- expect ( e . message ) . toEqual ( "S3 aborted request" ) ;
41- }
42- } ) ;
37+ expect ( result . response ) . toBe ( mockResponse ) ;
38+ } ) ;
39+
40+ it ( "should throw if response body is empty" , async ( ) => {
41+ expect . assertions ( 3 ) ;
42+ mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
43+ mockUtf8Encoder . mockReturnValue ( "" ) ;
44+ mockNextHandler . mockReturnValue ( {
45+ response : new HttpResponse ( {
46+ statusCode : 200 ,
47+ headers : { } ,
48+ body : "" ,
49+ } ) ,
50+ } ) ;
51+ const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
52+ try {
53+ await handler ( {
54+ input : { } ,
55+ request : new HttpRequest ( {
56+ hostname : "s3.us-east-1.amazonaws.com" ,
57+ } ) ,
58+ } ) ;
59+ } catch ( e ) {
60+ expect ( e ) . toBeDefined ( ) ;
61+ expect ( e . name ) . toEqual ( "InternalError" ) ;
62+ expect ( e . message ) . toEqual ( "S3 aborted request" ) ;
63+ }
64+ } ) ;
4365
44- it ( "should throw if response body contains Error tag" , async ( ) => {
45- const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
66+ it ( "should throw if response body contains Error tag" , async ( ) => {
67+ const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
4668 <Error>
4769 <Code>InternalError</Code>
4870 <Message>We encountered an internal error. Please try again.</Message>
4971 <RequestId>656c76696e6727732072657175657374</RequestId>
5072 <HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
5173 </Error>` ;
52- mockStreamCollector . mockResolvedValue ( Buffer . from ( errorBody ) ) ;
53- mockUtf8Encoder . mockReturnValue ( errorBody ) ;
54- mockNextHandler . mockReturnValue ( {
55- response : new HttpResponse ( {
56- statusCode : 200 ,
57- headers : { } ,
58- body : "" ,
59- } ) ,
60- } ) ;
61- const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
62- const { response } = await handler ( {
63- input : { } ,
64- request : new HttpRequest ( {
65- hostname : "s3.us-east-1.amazonaws.com" ,
66- } ) ,
74+ mockStreamCollector . mockResolvedValue ( Buffer . from ( errorBody ) ) ;
75+ mockUtf8Encoder . mockReturnValue ( errorBody ) ;
76+ mockNextHandler . mockReturnValue ( {
77+ response : new HttpResponse ( {
78+ statusCode : 200 ,
79+ headers : { } ,
80+ body : "" ,
81+ } ) ,
82+ } ) ;
83+ const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
84+ const { response } = await handler ( {
85+ input : { } ,
86+ request : new HttpRequest ( {
87+ hostname : "s3.us-east-1.amazonaws.com" ,
88+ } ) ,
89+ } ) ;
90+ expect ( HttpResponse . isInstance ( response ) ) . toBe ( true ) ;
91+ // @ts -ignore
92+ expect ( response . statusCode ) . toBeGreaterThanOrEqual ( 400 ) ;
6793 } ) ;
68- expect ( HttpResponse . isInstance ( response ) ) . toBe ( true ) ;
69- // @ts -ignore
70- expect ( response . statusCode ) . toBeGreaterThanOrEqual ( 400 ) ;
7194 } ) ;
7295} ) ;
0 commit comments