@@ -81,7 +81,7 @@ class APIGatewayTests: XCTestCase {
81
81
" accept " : " text/html,application/xml,application/json " ,
82
82
" host " : " service-3ei3tii4-251000691.ap-guangzhou.apigateway.myqloud.com " ,
83
83
" user-agent " : " User Agent String " ,
84
- " x-anonymous-consumer " : " true " ,
84
+ " x-anonymous-consumer " : " true " ,
85
85
" x-api-requestid " : " 24281851d905b02add27dad71656f29b " ,
86
86
" x-b3-traceid " : " 24281851d905b02add27dad71656f29b " ,
87
87
" x-qualifier " : " $DEFAULT " ] )
@@ -104,7 +104,7 @@ class APIGatewayTests: XCTestCase {
104
104
func testResponseEncodingWithText( ) {
105
105
let resp = APIGateway . Response (
106
106
statusCode: . ok,
107
- headers : [ " Content-Type " : " text/plain " ] ,
107
+ type : . text,
108
108
body: " abc123 "
109
109
)
110
110
@@ -123,7 +123,6 @@ class APIGatewayTests: XCTestCase {
123
123
let body = #"{"hello":"swift"}"#
124
124
let resp = APIGateway . Response (
125
125
statusCode: . ok,
126
- headers: [ " Content-Type " : " application/json " ] ,
127
126
body: body. data ( using: . utf8) !
128
127
)
129
128
@@ -139,7 +138,66 @@ class APIGatewayTests: XCTestCase {
139
138
140
139
XCTAssertEqual ( newResp. statusCode, resp. statusCode)
141
140
XCTAssertEqual ( newResp. isBase64Encoded, true )
142
- XCTAssertEqual ( newResp. headers [ " Content-Type " ] , " application/json " )
141
+ XCTAssertEqual ( newResp. headers [ " Content-Type " ] , " application/octet-stream " )
143
142
XCTAssertEqual ( Data ( base64Encoded: newResp. body) , body. data ( using: . utf8) )
144
143
}
144
+
145
+ func testResponseEncodingWithCodable( ) {
146
+ struct Point : Codable , Equatable {
147
+ let x , y : Double
148
+ }
149
+ let point = Point ( x: 1.0 , y: - 0.01 )
150
+ let resp = APIGateway . Response (
151
+ statusCode: . ok,
152
+ codableBody: point
153
+ )
154
+
155
+ var data : Data ?
156
+ XCTAssertNoThrow ( data = try JSONEncoder ( ) . encode ( resp) )
157
+ var json : APIGateway . Response ?
158
+ XCTAssertNoThrow ( json = try JSONDecoder ( ) . decode ( APIGateway . Response. self, from: XCTUnwrap ( data) ) )
159
+
160
+ guard let newResp = json else {
161
+ XCTFail ( " Expected to have value " )
162
+ return
163
+ }
164
+
165
+ XCTAssertEqual ( newResp. statusCode, resp. statusCode)
166
+ XCTAssertEqual ( newResp. isBase64Encoded, false )
167
+ XCTAssertEqual ( newResp. headers [ " Content-Type " ] , " application/json " )
168
+ XCTAssertEqual ( try JSONDecoder ( ) . decode ( Point . self, from: ( newResp. body. data ( using: . utf8) ) !) , point)
169
+ }
170
+
171
+ func testResponseEncodingWithNil( ) {
172
+ let resp = APIGateway . Response ( statusCode: . ok)
173
+
174
+ var data : Data ?
175
+ XCTAssertNoThrow ( data = try JSONEncoder ( ) . encode ( resp) )
176
+ var json : APIGateway . Response ?
177
+ XCTAssertNoThrow ( json = try JSONDecoder ( ) . decode ( APIGateway . Response. self, from: XCTUnwrap ( data) ) )
178
+
179
+ XCTAssertEqual ( json? . statusCode, resp. statusCode)
180
+ XCTAssertEqual ( json? . headers [ " Content-Type " ] , " text/plain " )
181
+ XCTAssertEqual ( json? . isBase64Encoded, false )
182
+ XCTAssertEqual ( json? . body, " " )
183
+ }
184
+
185
+ func testResponseEncodingWithCustomMIME( ) {
186
+ let mime = " application/x-javascript "
187
+ let resp = APIGateway . Response (
188
+ statusCode: . ok,
189
+ type: . init( rawValue: mime) ,
190
+ body: " console.log( \" Hello world! \" ); "
191
+ )
192
+
193
+ var data : Data ?
194
+ XCTAssertNoThrow ( data = try JSONEncoder ( ) . encode ( resp) )
195
+ var json : APIGateway . Response ?
196
+ XCTAssertNoThrow ( json = try JSONDecoder ( ) . decode ( APIGateway . Response. self, from: XCTUnwrap ( data) ) )
197
+
198
+ XCTAssertEqual ( json? . statusCode, resp. statusCode)
199
+ XCTAssertEqual ( json? . body, resp. body)
200
+ XCTAssertEqual ( json? . isBase64Encoded, false )
201
+ XCTAssertEqual ( json? . headers [ " Content-Type " ] , mime)
202
+ }
145
203
}
0 commit comments