@@ -6,6 +6,7 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
6
6
const mockNextHandler = jest . fn ( ) ;
7
7
const mockStreamCollector = jest . fn ( ) ;
8
8
const mockUtf8Encoder = jest . fn ( ) ;
9
+ const mockResponse = jest . fn ( ) ;
9
10
const mockConfig = {
10
11
streamCollector : mockStreamCollector ,
11
12
utf8Encoder : mockUtf8Encoder ,
@@ -15,58 +16,80 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
15
16
jest . clearAllMocks ( ) ;
16
17
} ) ;
17
18
18
- it ( "should throw if response body is empty" , async ( ) => {
19
- expect . assertions ( 3 ) ;
19
+ describe ( "tests for statusCode < 200 and >= 300" , ( ) => {
20
20
mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
21
21
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,
25
27
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 ( {
32
32
input : { } ,
33
33
request : new HttpRequest ( {
34
34
hostname : "s3.us-east-1.amazonaws.com" ,
35
35
} ) ,
36
36
} ) ;
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
+ } ) ;
43
65
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"?>
46
68
<Error>
47
69
<Code>InternalError</Code>
48
70
<Message>We encountered an internal error. Please try again.</Message>
49
71
<RequestId>656c76696e6727732072657175657374</RequestId>
50
72
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
51
73
</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 ) ;
67
93
} ) ;
68
- expect ( HttpResponse . isInstance ( response ) ) . toBe ( true ) ;
69
- // @ts -ignore
70
- expect ( response . statusCode ) . toBeGreaterThanOrEqual ( 400 ) ;
71
94
} ) ;
72
95
} ) ;
0 commit comments