1
1
import { HttpRequest , HttpResponse } from "@smithy/protocol-http" ;
2
+ import { toUtf8 } from "@smithy/util-utf8" ;
3
+ import { Readable } from "stream" ;
2
4
3
5
import { throw200ExceptionsMiddleware } from "./throw-200-exceptions" ;
4
6
5
7
describe ( "throw200ExceptionsMiddlewareOptions" , ( ) => {
6
8
const mockNextHandler = jest . fn ( ) ;
7
- const mockStreamCollector = jest . fn ( ) ;
8
- const mockUtf8Encoder = jest . fn ( ) ;
9
9
const mockResponse = jest . fn ( ) ;
10
10
const mockConfig = {
11
- streamCollector : mockStreamCollector ,
12
- utf8Encoder : mockUtf8Encoder ,
11
+ utf8Encoder : toUtf8 ,
13
12
} ;
14
13
15
14
beforeEach ( ( ) => {
16
15
jest . clearAllMocks ( ) ;
17
16
} ) ;
18
17
19
18
describe ( "tests for statusCode < 200 and >= 300" , ( ) => {
20
- mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
21
- mockUtf8Encoder . mockReturnValue ( "" ) ;
22
-
23
19
it . each ( [ 199 , 300 ] ) ( "results for statusCode %i" , async ( statusCode ) => {
24
20
mockNextHandler . mockReturnValue ( {
25
21
response : mockResponse ,
@@ -39,13 +35,11 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
39
35
40
36
it ( "should throw if response body is empty" , async ( ) => {
41
37
expect . assertions ( 3 ) ;
42
- mockStreamCollector . mockResolvedValue ( Buffer . from ( "" ) ) ;
43
- mockUtf8Encoder . mockReturnValue ( "" ) ;
44
38
mockNextHandler . mockReturnValue ( {
45
39
response : new HttpResponse ( {
46
40
statusCode : 200 ,
47
41
headers : { } ,
48
- body : "" ,
42
+ body : Readable . from ( Buffer . from ( "" ) ) ,
49
43
} ) ,
50
44
} ) ;
51
45
const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , {
@@ -73,13 +67,11 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
73
67
<RequestId>656c76696e6727732072657175657374</RequestId>
74
68
<HostId>Uuag1LuByRx9e6j5Onimru9pO4ZVKnJ2Qz7/C1NPcfTWAtRPfTaOFg==</HostId>
75
69
</Error>` ;
76
- mockStreamCollector . mockResolvedValue ( Buffer . from ( errorBody ) ) ;
77
- mockUtf8Encoder . mockReturnValue ( errorBody ) ;
78
70
mockNextHandler . mockReturnValue ( {
79
71
response : new HttpResponse ( {
80
72
statusCode : 200 ,
81
73
headers : { } ,
82
- body : "" ,
74
+ body : Readable . from ( Buffer . from ( errorBody ) ) ,
83
75
} ) ,
84
76
} ) ;
85
77
const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
@@ -106,13 +98,40 @@ describe("throw200ExceptionsMiddlewareOptions", () => {
106
98
<Message>Access Denied</Message>
107
99
</Error>
108
100
</DeleteResult>` ;
109
- mockStreamCollector . mockResolvedValue ( Buffer . from ( errorBody ) ) ;
110
- mockUtf8Encoder . mockReturnValue ( errorBody ) ;
111
101
mockNextHandler . mockReturnValue ( {
112
102
response : new HttpResponse ( {
113
103
statusCode : 200 ,
114
104
headers : { } ,
115
- body : "" ,
105
+ body : Readable . from ( Buffer . from ( errorBody ) ) ,
106
+ } ) ,
107
+ } ) ;
108
+ const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
109
+ const { response } = await handler ( {
110
+ input : { } ,
111
+ request : new HttpRequest ( {
112
+ hostname : "s3.us-east-1.amazonaws.com" ,
113
+ } ) ,
114
+ } ) ;
115
+ expect ( HttpResponse . isInstance ( response ) ) . toBe ( true ) ;
116
+ // @ts -ignore
117
+ expect ( response . statusCode ) . toEqual ( 200 ) ;
118
+ } ) ;
119
+
120
+ /**
121
+ * This is an exception to the specification. We cannot afford to read
122
+ * a streaming body for its entire duration just to check for an extremely unlikely
123
+ * terminating XML tag if the stream is very long.
124
+ */
125
+ it ( "should not throw if the Error tag is on an excessively long body" , async ( ) => {
126
+ const errorBody = `<?xml version="1.0" encoding="UTF-8"?>
127
+ <Error xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
128
+ ${ "a" . repeat ( 3000 ) }
129
+ </Error>` ;
130
+ mockNextHandler . mockReturnValue ( {
131
+ response : new HttpResponse ( {
132
+ statusCode : 200 ,
133
+ headers : { } ,
134
+ body : Readable . from ( Buffer . from ( errorBody ) ) ,
116
135
} ) ,
117
136
} ) ;
118
137
const handler = throw200ExceptionsMiddleware ( mockConfig ) ( mockNextHandler , { } as any ) ;
0 commit comments