Skip to content

Commit 38254f2

Browse files
committed
Add tests
1 parent 9c13ba5 commit 38254f2

File tree

4 files changed

+639
-0
lines changed

4 files changed

+639
-0
lines changed

Tests/AWSLambdaEventsTests/APIGateway+EncodableTests.swift

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,110 @@ struct APIGatewayEncodableResponseTests {
6969
#expect(encodedBody == businessResponse)
7070
}
7171
}
72+
73+
// MARK: APIGatewayV2 Encoding Helper Tests
74+
75+
@Test
76+
func testAPIGatewayV2ResponseEncodingHelper() throws {
77+
// given
78+
let businessResponse = BusinessResponse(message: "Hello World", code: 200)
79+
80+
// when
81+
let response = APIGatewayV2Response.encoding(businessResponse)
82+
83+
// then
84+
#expect(response.statusCode == .ok)
85+
#expect(response.body != nil)
86+
87+
let bodyData = try #require(response.body?.data(using: .utf8))
88+
let decodedResponse = try JSONDecoder().decode(BusinessResponse.self, from: bodyData)
89+
#expect(decodedResponse == businessResponse)
90+
}
91+
92+
@Test
93+
func testAPIGatewayV2ResponseEncodingHelperWithCustomStatus() throws {
94+
// given
95+
let businessResponse = BusinessResponse(message: "Created", code: 201)
96+
97+
// when
98+
let response = APIGatewayV2Response.encoding(businessResponse, status: .created)
99+
100+
// then
101+
#expect(response.statusCode == .created)
102+
#expect(response.body != nil)
103+
104+
let bodyData = try #require(response.body?.data(using: .utf8))
105+
let decodedResponse = try JSONDecoder().decode(BusinessResponse.self, from: bodyData)
106+
#expect(decodedResponse == businessResponse)
107+
}
108+
109+
@Test
110+
func testAPIGatewayV2ResponseEncodingHelperWithCustomEncoder() throws {
111+
// given
112+
let businessResponse = BusinessResponse(message: "Hello World", code: 200)
113+
let customEncoder = JSONEncoder()
114+
customEncoder.outputFormatting = .prettyPrinted
115+
116+
// when
117+
let response = APIGatewayV2Response.encoding(businessResponse, using: customEncoder)
118+
119+
// then
120+
#expect(response.statusCode == .ok)
121+
#expect(response.body != nil)
122+
#expect(response.body?.contains("\n") == true) // Pretty printed JSON should contain newlines
123+
124+
let bodyData = try #require(response.body?.data(using: .utf8))
125+
let decodedResponse = try JSONDecoder().decode(BusinessResponse.self, from: bodyData)
126+
#expect(decodedResponse == businessResponse)
127+
}
128+
129+
@Test
130+
func testAPIGatewayV2ResponseEncodingHelperWithError() throws {
131+
// given
132+
struct InvalidEncodable: Encodable {
133+
func encode(to encoder: Encoder) throws {
134+
throw TestError.encodingFailed
135+
}
136+
}
137+
138+
enum TestError: Error {
139+
case encodingFailed
140+
}
141+
142+
let invalidObject = InvalidEncodable()
143+
144+
// when
145+
let response = APIGatewayV2Response.encoding(invalidObject)
146+
147+
// then
148+
#expect(response.statusCode == .internalServerError)
149+
#expect(response.body?.contains("Internal Server Error") == true)
150+
}
151+
152+
@Test
153+
func testAPIGatewayV2ResponseEncodingHelperWithCustomErrorHandler() throws {
154+
// given
155+
struct InvalidEncodable: Encodable {
156+
func encode(to encoder: Encoder) throws {
157+
throw TestError.encodingFailed
158+
}
159+
}
160+
161+
enum TestError: Error {
162+
case encodingFailed
163+
}
164+
165+
let invalidObject = InvalidEncodable()
166+
let customErrorHandler: (Error) -> APIGatewayV2Response = { _ in
167+
APIGatewayV2Response(statusCode: .badRequest, body: "Custom error message")
168+
}
169+
170+
// when
171+
let response = APIGatewayV2Response.encoding(invalidObject, onError: customErrorHandler)
172+
173+
// then
174+
#expect(response.statusCode == .badRequest)
175+
#expect(response.body == "Custom error message")
176+
}
177+
72178
}

Tests/AWSLambdaEventsTests/APIGateway+V2Tests.swift

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,4 +224,154 @@ struct APIGatewayV2Tests {
224224
_ = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
225225
}
226226
}
227+
228+
// MARK: Codable Helpers Tests
229+
230+
@Test func decodeBodyWithNilBody() throws {
231+
let data = APIGatewayV2Tests.exampleGetEventBodyNilHeaders.data(using: .utf8)!
232+
let request = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
233+
234+
let decodedBody = try request.decodeBody()
235+
#expect(decodedBody == nil)
236+
}
237+
238+
@Test func decodeBodyWithPlainTextBody() throws {
239+
let requestWithBody = """
240+
{
241+
"routeKey":"POST /hello",
242+
"version":"2.0",
243+
"rawPath":"/hello",
244+
"rawQueryString":"",
245+
"requestContext":{
246+
"timeEpoch":1587750461466,
247+
"domainPrefix":"hello",
248+
"accountId":"0123456789",
249+
"stage":"$default",
250+
"domainName":"hello.test.com",
251+
"apiId":"pb5dg6g3rg",
252+
"requestId":"LgLpnibOFiAEPCA=",
253+
"http":{
254+
"path":"/hello",
255+
"userAgent":"test",
256+
"method":"POST",
257+
"protocol":"HTTP/1.1",
258+
"sourceIp":"127.0.0.1"
259+
},
260+
"time":"24/Apr/2020:17:47:41 +0000"
261+
},
262+
"isBase64Encoded":false,
263+
"body":"Hello World!"
264+
}
265+
"""
266+
267+
let data = requestWithBody.data(using: .utf8)!
268+
let request = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
269+
270+
let decodedBody = try request.decodeBody()
271+
let expectedBody = "Hello World!".data(using: .utf8)
272+
#expect(decodedBody == expectedBody)
273+
}
274+
275+
@Test func decodeBodyWithBase64EncodedBody() throws {
276+
let requestWithBase64Body = """
277+
{
278+
"routeKey":"POST /hello",
279+
"version":"2.0",
280+
"rawPath":"/hello",
281+
"rawQueryString":"",
282+
"requestContext":{
283+
"timeEpoch":1587750461466,
284+
"domainPrefix":"hello",
285+
"accountId":"0123456789",
286+
"stage":"$default",
287+
"domainName":"hello.test.com",
288+
"apiId":"pb5dg6g3rg",
289+
"requestId":"LgLpnibOFiAEPCA=",
290+
"http":{
291+
"path":"/hello",
292+
"userAgent":"test",
293+
"method":"POST",
294+
"protocol":"HTTP/1.1",
295+
"sourceIp":"127.0.0.1"
296+
},
297+
"time":"24/Apr/2020:17:47:41 +0000"
298+
},
299+
"isBase64Encoded":true,
300+
"body":"SGVsbG8gV29ybGQh"
301+
}
302+
"""
303+
304+
let data = requestWithBase64Body.data(using: .utf8)!
305+
let request = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
306+
307+
let decodedBody = try request.decodeBody()
308+
let expectedBody = "Hello World!".data(using: .utf8)
309+
#expect(decodedBody == expectedBody)
310+
}
311+
312+
@Test func decodeBodyAsDecodableType() throws {
313+
struct TestPayload: Codable, Equatable {
314+
let message: String
315+
let count: Int
316+
}
317+
318+
// Use the fullExamplePayload which already has a simple JSON body
319+
let data = APIGatewayV2Tests.fullExamplePayload.data(using: .utf8)!
320+
let request = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
321+
322+
// Test that we can decode the body as String
323+
// The fullExamplePayload has body: "Hello from Lambda" which is not valid JSON, so this should fail
324+
#expect(throws: DecodingError.self) {
325+
_ = try request.decodeBody(TestPayload.self)
326+
}
327+
}
328+
329+
@Test func decodeBodyAsDecodableTypeWithCustomDecoder() throws {
330+
struct TestPayload: Codable, Equatable {
331+
let messageText: String
332+
let count: Int
333+
334+
enum CodingKeys: String, CodingKey {
335+
case messageText = "message_text"
336+
case count
337+
}
338+
}
339+
340+
let testPayload = TestPayload(messageText: "test", count: 42)
341+
342+
let requestWithBase64JSONBody = """
343+
{
344+
"routeKey":"POST /hello",
345+
"version":"2.0",
346+
"rawPath":"/hello",
347+
"rawQueryString":"",
348+
"requestContext":{
349+
"timeEpoch":1587750461466,
350+
"domainPrefix":"hello",
351+
"accountId":"0123456789",
352+
"stage":"$default",
353+
"domainName":"hello.test.com",
354+
"apiId":"pb5dg6g3rg",
355+
"requestId":"LgLpnibOFiAEPCA=",
356+
"http":{
357+
"path":"/hello",
358+
"userAgent":"test",
359+
"method":"POST",
360+
"protocol":"HTTP/1.1",
361+
"sourceIp":"127.0.0.1"
362+
},
363+
"time":"24/Apr/2020:17:47:41 +0000"
364+
},
365+
"isBase64Encoded":true,
366+
"body":"eyJtZXNzYWdlX3RleHQiOiJ0ZXN0IiwiY291bnQiOjQyfQ==",
367+
"headers":{}
368+
}
369+
"""
370+
371+
let data = requestWithBase64JSONBody.data(using: .utf8)!
372+
let request = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
373+
374+
let decodedPayload = try request.decodeBody(TestPayload.self)
375+
#expect(decodedPayload == testPayload)
376+
}
227377
}

0 commit comments

Comments
 (0)