Skip to content

Commit 81d3dc1

Browse files
committed
add unit tests
1 parent eee1f21 commit 81d3dc1

File tree

4 files changed

+250
-1
lines changed

4 files changed

+250
-1
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ let package = Package(
2626
.testTarget(
2727
name: "OpenAPILambdaTests",
2828
dependencies: [
29-
.byName(name: "OpenAPILambda")
29+
.byName(name: "OpenAPILambda"),
30+
.product(name: "AWSLambdaEvents", package: "swift-aws-lambda-events"),
3031
]
3132
),
3233
]
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift OpenAPI Lambda open source project
4+
//
5+
// Copyright Swift OpenAPI Lambda project authors
6+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates.
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
import AWSLambdaEvents
16+
import Foundation
17+
import HTTPTypes
18+
import Testing
19+
20+
@testable import OpenAPILambda
21+
22+
struct ALBConversionTests {
23+
24+
static let albEventJSON = """
25+
{
26+
"requestContext": {
27+
"elb": {
28+
"targetGroupArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/lambda-target/50dc6c495c0c9188"
29+
}
30+
},
31+
"httpMethod": "GET",
32+
"path": "/stocks/AAPL",
33+
"queryStringParameters": {},
34+
"headers": {
35+
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
36+
"host": "lambda-alb-123578498.us-east-1.elb.amazonaws.com",
37+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
38+
},
39+
"body": "",
40+
"isBase64Encoded": false
41+
}
42+
"""
43+
44+
@Test("ALB request to HTTPRequest conversion")
45+
func testALBRequestToHTTPRequest() throws {
46+
let data = ALBConversionTests.albEventJSON.data(using: .utf8)!
47+
let albRequest = try JSONDecoder().decode(ALBTargetGroupRequest.self, from: data)
48+
49+
let httpRequest = try albRequest.httpRequest()
50+
51+
#expect(httpRequest.method == HTTPRequest.Method.get)
52+
#expect(httpRequest.path == "/stocks/AAPL")
53+
#expect(httpRequest.headerFields[HTTPField.Name.accept] == "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
54+
#expect(httpRequest.headerFields[HTTPField.Name("host")!] == "lambda-alb-123578498.us-east-1.elb.amazonaws.com")
55+
#expect(httpRequest.headerFields[HTTPField.Name.userAgent] == "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
56+
}
57+
58+
@Test("HTTPResponse to ALB response conversion")
59+
func testHTTPResponseToALBResponse() throws {
60+
var httpResponse = HTTPResponse(status: .ok)
61+
httpResponse.headerFields[HTTPField.Name.contentType] = "application/json"
62+
httpResponse.headerFields[HTTPField.Name.contentLength] = "42"
63+
64+
let albResponse = ALBTargetGroupResponse(from: httpResponse)
65+
66+
#expect(albResponse.statusCode == .ok)
67+
#expect(albResponse.headers?[HTTPField.Name.contentType.rawName] == "application/json")
68+
#expect(albResponse.headers?[HTTPField.Name.contentLength.rawName] == "42")
69+
#expect(albResponse.isBase64Encoded == false)
70+
}
71+
72+
73+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift OpenAPI Lambda open source project
4+
//
5+
// Copyright Swift OpenAPI Lambda project authors
6+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates.
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
import AWSLambdaEvents
16+
import Foundation
17+
import HTTPTypes
18+
import Testing
19+
20+
@testable import OpenAPILambda
21+
22+
struct APIGatewayV2ConversionTests {
23+
24+
static let apiGatewayEventJSON = """
25+
{
26+
"rawQueryString": "",
27+
"headers": {
28+
"host": "b2k1t8fon7.execute-api.us-east-1.amazonaws.com",
29+
"accept": "*/*",
30+
"user-agent": "curl/8.1.2",
31+
"authorization": "Bearer 123"
32+
},
33+
"requestContext": {
34+
"apiId": "b2k1t8fon7",
35+
"http": {
36+
"sourceIp": "191.95.148.219",
37+
"userAgent": "curl/8.1.2",
38+
"method": "GET",
39+
"path": "/stocks/AAPL",
40+
"protocol": "HTTP/1.1"
41+
},
42+
"timeEpoch": 1701957940365,
43+
"domainPrefix": "b2k1t8fon7",
44+
"accountId": "486652066693",
45+
"time": "07/Dec/2023:14:05:40 +0000",
46+
"stage": "$default",
47+
"domainName": "b2k1t8fon7.execute-api.us-east-1.amazonaws.com",
48+
"requestId": "Pk2gOia2IAMEPOw="
49+
},
50+
"isBase64Encoded": false,
51+
"version": "2.0",
52+
"routeKey": "$default",
53+
"rawPath": "/stocks/AAPL"
54+
}
55+
"""
56+
57+
static let apiGatewayEventWithQueryJSON = """
58+
{
59+
"rawQueryString": "limit=10&offset=0",
60+
"headers": {
61+
"host": "b2k1t8fon7.execute-api.us-east-1.amazonaws.com"
62+
},
63+
"requestContext": {
64+
"apiId": "b2k1t8fon7",
65+
"http": {
66+
"sourceIp": "191.95.148.219",
67+
"userAgent": "curl/8.1.2",
68+
"method": "GET",
69+
"path": "/stocks/AAPL",
70+
"protocol": "HTTP/1.1"
71+
},
72+
"timeEpoch": 1701957940365,
73+
"domainPrefix": "b2k1t8fon7",
74+
"accountId": "486652066693",
75+
"time": "07/Dec/2023:14:05:40 +0000",
76+
"stage": "$default",
77+
"domainName": "b2k1t8fon7.execute-api.us-east-1.amazonaws.com",
78+
"requestId": "Pk2gOia2IAMEPOw="
79+
},
80+
"isBase64Encoded": false,
81+
"version": "2.0",
82+
"routeKey": "$default",
83+
"rawPath": "/stocks/AAPL"
84+
}
85+
"""
86+
87+
@Test("API Gateway v2 request to HTTPRequest conversion")
88+
func testAPIGatewayV2RequestToHTTPRequest() throws {
89+
let data = APIGatewayV2ConversionTests.apiGatewayEventJSON.data(using: .utf8)!
90+
let apiGatewayRequest = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
91+
92+
let httpRequest = try apiGatewayRequest.httpRequest()
93+
94+
#expect(httpRequest.method == HTTPRequest.Method.get)
95+
#expect(httpRequest.path == "/stocks/AAPL")
96+
#expect(httpRequest.scheme == "https")
97+
#expect(httpRequest.headerFields[HTTPField.Name("host")!] == "b2k1t8fon7.execute-api.us-east-1.amazonaws.com")
98+
#expect(httpRequest.headerFields[HTTPField.Name.accept] == "*/*")
99+
#expect(httpRequest.headerFields[HTTPField.Name.userAgent] == "curl/8.1.2")
100+
#expect(httpRequest.headerFields[HTTPField.Name.authorization] == "Bearer 123")
101+
}
102+
103+
@Test("API Gateway v2 request with query string")
104+
func testAPIGatewayV2RequestWithQueryString() throws {
105+
let data = APIGatewayV2ConversionTests.apiGatewayEventWithQueryJSON.data(using: .utf8)!
106+
let apiGatewayRequest = try JSONDecoder().decode(APIGatewayV2Request.self, from: data)
107+
108+
let httpRequest = try apiGatewayRequest.httpRequest()
109+
110+
#expect(httpRequest.path == "/stocks/AAPL?limit=10&offset=0")
111+
}
112+
113+
@Test("HTTPResponse to API Gateway v2 response conversion")
114+
func testHTTPResponseToAPIGatewayV2Response() throws {
115+
var httpResponse = HTTPResponse(status: .ok)
116+
httpResponse.headerFields[HTTPField.Name.contentType] = "application/json"
117+
httpResponse.headerFields[HTTPField.Name.contentLength] = "42"
118+
119+
let apiGatewayResponse = APIGatewayV2Response(from: httpResponse)
120+
121+
#expect(apiGatewayResponse.statusCode == .ok)
122+
#expect(apiGatewayResponse.headers?[HTTPField.Name.contentType.rawName] == "application/json")
123+
#expect(apiGatewayResponse.headers?[HTTPField.Name.contentLength.rawName] == "42")
124+
#expect(apiGatewayResponse.isBase64Encoded == false)
125+
#expect(apiGatewayResponse.cookies == nil)
126+
}
127+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift OpenAPI Lambda open source project
4+
//
5+
// Copyright Swift OpenAPI Lambda project authors
6+
// Copyright (c) 2023 Amazon.com, Inc. or its affiliates.
7+
// Licensed under Apache License v2.0
8+
//
9+
// See LICENSE.txt for license information
10+
// See CONTRIBUTORS.txt for the list of Swift OpenAPI Lambda project authors
11+
//
12+
// SPDX-License-Identifier: Apache-2.0
13+
//
14+
//===----------------------------------------------------------------------===//
15+
import AWSLambdaEvents
16+
import HTTPTypes
17+
import Testing
18+
19+
@testable import OpenAPILambda
20+
21+
@Suite("HTTP Headers Conversion Tests")
22+
struct HTTPHeadersConversionTests {
23+
24+
@Test("Multi-value headers preserved as comma-separated")
25+
func testMultiValueHeadersPreserved() throws {
26+
var httpResponse = HTTPResponse(status: .ok)
27+
httpResponse.headerFields.append(HTTPField(name: HTTPField.Name.setCookie, value: "session=abc123"))
28+
httpResponse.headerFields.append(HTTPField(name: HTTPField.Name.setCookie, value: "theme=dark"))
29+
30+
let albResponse = ALBTargetGroupResponse(from: httpResponse)
31+
32+
#expect(albResponse.headers?[HTTPField.Name.setCookie.rawName] == "session=abc123, theme=dark")
33+
#expect(albResponse.multiValueHeaders == nil)
34+
}
35+
36+
@Test("HTTPHeaders to HTTPFields conversion")
37+
func testHTTPHeadersToHTTPFields() throws {
38+
let headers: HTTPHeaders = [
39+
"Set-Cookie": "session=abc123, theme=dark",
40+
"Content-Type": "application/json"
41+
]
42+
43+
let httpFields = headers.httpFields()
44+
45+
#expect(httpFields[HTTPField.Name.setCookie] == "session=abc123, theme=dark")
46+
#expect(httpFields[HTTPField.Name.contentType] == "application/json")
47+
}
48+
}

0 commit comments

Comments
 (0)