@@ -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,13 +16,34 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
15
16
jest . clearAllMocks ( ) ;
16
17
} ) ;
17
18
18
- describe ( "exceptions for code < 200 and >= 300" , ( ) => {
19
+ describe ( "tests for statusCode < 200 and >= 300" , ( ) => {
19
20
mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
20
21
mockUtf8Encoder . mockReturnValue ( "" ) ;
21
- it ( "throws an exception if code is less than 200" , async ( ) => {
22
+
23
+ it . each ( [ 199 , 300 ] ) ( "results for statusCode %i" , async ( statusCode ) => {
24
+ mockNextHandler . mockReturnValue ( {
25
+ response : mockResponse ,
26
+ statusCode,
27
+ // headers: {},
28
+ // body: "",
29
+ } ) ;
30
+ } ) ;
31
+ const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
32
+ const result = await handler ( {
33
+ input : { } ,
34
+ request : new HttpRequest ( {
35
+ hostname : "s3.us-east-1.amazonaws.com" ,
36
+ } ) ,
37
+ } ) ;
38
+ expect ( result . response ) . toBe ( mockResponse ) ;
39
+
40
+ it ( "should throw if response body is empty" , async ( ) => {
41
+ expect . assertions ( 3 ) ;
42
+ mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
43
+ mockUtf8Encoder . mockReturnValue ( "" ) ;
22
44
mockNextHandler . mockReturnValue ( {
23
45
response : new HttpResponse ( {
24
- statusCode : 199 ,
46
+ statusCode : 200 ,
25
47
headers : { } ,
26
48
body : "" ,
27
49
} ) ,
@@ -40,81 +62,34 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
40
62
expect ( e . message ) . toEqual ( "S3 aborted request" ) ;
41
63
}
42
64
} ) ;
43
- it ( "throws an exception if code is greater than or equal to 300" , async ( ) => {
65
+
66
+ it ( "should throw if response body contains Error tag" , async ( ) => {
67
+ const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
68
+ <Error>
69
+ <Code>InternalError</Code>
70
+ <Message>We encountered an internal error. Please try again.</Message>
71
+ <RequestId>656c76696e6727732072657175657374</RequestId>
72
+ <HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
73
+ </Error>` ;
74
+ mockStreamCollector . mockResolvedValue ( Buffer . from ( errorBody ) ) ;
75
+ mockUtf8Encoder . mockReturnValue ( errorBody ) ;
44
76
mockNextHandler . mockReturnValue ( {
45
77
response : new HttpResponse ( {
46
- statusCode : 300 ,
78
+ statusCode : 200 ,
47
79
headers : { } ,
48
80
body : "" ,
49
81
} ) ,
50
82
} ) ;
51
83
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
- } ) ;
65
- } ) ;
66
- it ( "should throw if response body is empty" , async ( ) => {
67
- expect . assertions ( 3 ) ;
68
- mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
69
- mockUtf8Encoder . mockReturnValue ( "" ) ;
70
- mockNextHandler . mockReturnValue ( {
71
- response : new HttpResponse ( {
72
- statusCode : 200 ,
73
- headers : { } ,
74
- body : "" ,
75
- } ) ,
76
- } ) ;
77
- const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
78
- try {
79
- await handler ( {
84
+ const { response } = await handler ( {
80
85
input : { } ,
81
86
request : new HttpRequest ( {
82
87
hostname : "s3.us-east-1.amazonaws.com" ,
83
88
} ) ,
84
89
} ) ;
85
- } catch ( e ) {
86
- expect ( e ) . toBeDefined ( ) ;
87
- expect ( e . name ) . toEqual ( "InternalError" ) ;
88
- expect ( e . message ) . toEqual ( "S3 aborted request" ) ;
89
- }
90
- } ) ;
91
-
92
- it ( "should throw if response body contains Error tag" , async ( ) => {
93
- const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
94
- <Error>
95
- <Code>InternalError</Code>
96
- <Message>We encountered an internal error. Please try again.</Message>
97
- <RequestId>656c76696e6727732072657175657374</RequestId>
98
- <HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
99
- </Error>` ;
100
- mockStreamCollector . mockResolvedValue ( Buffer . from ( errorBody ) ) ;
101
- mockUtf8Encoder . mockReturnValue ( errorBody ) ;
102
- mockNextHandler . mockReturnValue ( {
103
- response : new HttpResponse ( {
104
- statusCode : 200 ,
105
- headers : { } ,
106
- body : "" ,
107
- } ) ,
108
- } ) ;
109
- const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
110
- const { response } = await handler ( {
111
- input : { } ,
112
- request : new HttpRequest ( {
113
- hostname : "s3.us-east-1.amazonaws.com" ,
114
- } ) ,
90
+ expect ( HttpResponse . isInstance ( response ) ) . toBe ( true ) ;
91
+ // @ts -ignore
92
+ expect ( response . statusCode ) . toBeGreaterThanOrEqual ( 400 ) ;
115
93
} ) ;
116
- expect ( HttpResponse . isInstance ( response ) ) . toBe ( true ) ;
117
- // @ts -ignore
118
- expect ( response . statusCode ) . toBeGreaterThanOrEqual ( 400 ) ;
119
94
} ) ;
120
95
} ) ;
0 commit comments