Skip to content

Commit bddcce7

Browse files
Add tests for HTTP Lambda API
1 parent 291c739 commit bddcce7

5 files changed

+122
-39
lines changed

Tests/SLSAdapterTests/Fixtures/serverless_webhook.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ functions:
4848
githubWebHook:
4949
handler: github-webhook
5050
memorySize: 256
51-
description: '[${sls:stage}] get /github-webhook'
51+
description: '[${sls:stage}] post /github-webhook'
5252
package:
5353
artifact: build/GitHubWebHook/GitHubWebHook.zip
5454
environment:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-sprinter
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
import Foundation
18+
import SLSAdapter
19+
import Yams
20+
21+
public struct HttpAPILambdaParams {
22+
public let name: String
23+
public let handler: String
24+
public let event: EventHTTPAPI
25+
public let environment: YAMLContent?
26+
public let artifact: String
27+
28+
public init(name: String, handler: String, event: EventHTTPAPI, environment: YAMLContent?, artifact: String) {
29+
self.name = name
30+
self.handler = handler
31+
self.event = event
32+
self.environment = environment
33+
self.artifact = artifact
34+
}
35+
}
36+
37+
public extension Function {
38+
static func httpAPILambda(params: HttpAPILambdaParams, memorySize: Int?) throws -> Function {
39+
try .httpApiLambda(
40+
handler: params.handler,
41+
description: nil,
42+
memorySize: memorySize,
43+
environment: params.environment,
44+
runtime: nil,
45+
package: .init(patterns: nil,
46+
individually: nil,
47+
artifact: params.artifact),
48+
event: params.event
49+
)
50+
}
51+
}
52+
53+
extension Array where Element == HttpAPILambdaParams {
54+
func buildFunctions(memorySize: Int) throws -> [String: Function] {
55+
var functions: [String: Function] = [:]
56+
for lambdasParam in self {
57+
functions[lambdasParam.name] = try Function.httpAPILambda(params: lambdasParam, memorySize: memorySize)
58+
}
59+
return functions
60+
}
61+
}

Tests/SLSAdapterTests/SLSAdapterTests.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
3131
return try Data(contentsOf: fixtureUrl)
3232
}
3333

34-
func testReadServerlessYml() throws {
34+
func test_ReadServerlessYml() throws {
3535
let serverlessYml = try fixture(name: "serverless", type: "yml")
3636

3737
let decoder = YAMLDecoder()
@@ -162,7 +162,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
162162
XCTAssertEqual(productTableProperties.dictionary?["BillingMode"]?.string, "PAY_PER_REQUEST")
163163
}
164164

165-
func testReadWriteServerlessYml() throws {
165+
func test_ReadWriteServerlessYml() throws {
166166
let serverlessYml = try fixture(name: "serverless", type: "yml")
167167
let decoder = YAMLDecoder()
168168
let serverlessConfig = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
@@ -179,7 +179,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
179179
let path: String
180180
}
181181

182-
func testInitServerlessYml() throws {
182+
func test_InitServerlessYml() throws {
183183
let decoder = YAMLDecoder()
184184
let serverlessYml = try fixture(name: "serverless", type: "yml")
185185
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
@@ -215,7 +215,7 @@ final class SwiftSlsAdapterTests: XCTestCase {
215215
XCTAssertEqual(serverlessConfig.resources, serverlessConfig2.resources)
216216
}
217217

218-
func testInitServerlessNolLayerYml() throws {
218+
func test_InitServerlessNolLayerYml() throws {
219219
let decoder = YAMLDecoder()
220220
let serverlessYml = try fixture(name: "serverless_no_layer", type: "yml")
221221
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: serverlessYml)

Tests/SLSAdapterTests/ServerlessConfig+Exensions.swift

+6-31
Original file line numberDiff line numberDiff line change
@@ -203,57 +203,32 @@ extension ServerlessConfig {
203203

204204
static func webhookLambdaAPI(
205205
service: String,
206-
httpAPIPath: String,
207206
region: Region,
208207
runtime: Runtime,
209208
architecture: Architecture,
210209
memorySize: Int,
211-
executable: String,
212-
artifact: String
210+
lambdasParams: [HttpAPILambdaParams]
213211
) throws -> ServerlessConfig {
214212
let iam = Iam(
215213
role: Role(
216214
statements: [.allowLogAccess(resource: try YAMLContent(with: "*"))]
217215
)
218216
)
219-
let environment = try YAMLContent(with: ["WEBHOOK_SECRET": "${ssm:/dev/swift-webhook/webhook_secret}"])
220217
let provider = Provider(
221218
name: .aws,
222219
region: region,
223220
runtime: runtime,
224-
environment: environment,
221+
environment: nil,
225222
architecture: architecture,
226-
httpAPI: .init(
227-
payload: "2.0",
228-
cors: false
229-
),
223+
httpAPI: .init(payload: "2.0", cors: false),
230224
iam: iam
231225
)
232-
let endpoints = [
233-
Endpoint(handler: "postWebHook", method: .post, path: "/webhook"),
234-
Endpoint(handler: "getWebHook", method: .get, path: "/webhook"),
235-
Endpoint(handler: "githubWebHook", method: .post, path: "/github-webhook")
236-
]
237-
var functions: [String: Function] = [:]
238-
for endpoint in endpoints {
239-
let function = try Function.httpApiLambda(
240-
handler: "\(endpoint.handler)",
241-
description: nil,
242-
memorySize: memorySize,
243-
environment: environment,
244-
runtime: nil,
245-
package: .init(patterns: nil,
246-
individually: nil,
247-
artifact: "build/GitHubWebHook/GitHubWebHook.zip"),
248-
event: .init(path: endpoint.path, method: endpoint.method)
249-
)
250-
functions["\(endpoint.handler)\(executable)"] = function
251-
}
252-
226+
let package = Package(patterns: nil, individually: true)
227+
let functions = try lambdasParams.buildFunctions(memorySize: memorySize)
253228
return ServerlessConfig(
254229
service: service,
255230
provider: provider,
256-
package: .init(patterns: nil, individually: nil, artifact: artifact),
231+
package: package,
257232
custom: nil,
258233
layers: nil,
259234
functions: functions,

Tests/SLSAdapterTests/ServerlessHttpAPILambdaTests.swift

+50-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
3232
return try Data(contentsOf: fixtureUrl)
3333
}
3434

35-
func testReadServerlessWebhook() throws {
35+
func test_ReadServerlessWebhook() throws {
3636
let serverlessYml = try fixture(name: "serverless_webhook", type: "yml")
3737

3838
let decoder = YAMLDecoder()
@@ -97,7 +97,7 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
9797
let githubWebHook = try XCTUnwrap(serverlessConfig.functions?["githubWebHook"])
9898
XCTAssertEqual(githubWebHook.handler, "github-webhook")
9999
XCTAssertEqual(githubWebHook.memorySize, 256)
100-
XCTAssertEqual(githubWebHook.description, "[${sls:stage}] get /github-webhook")
100+
XCTAssertEqual(githubWebHook.description, "[${sls:stage}] post /github-webhook")
101101
let artifact3 = githubWebHook.package?.artifact
102102
XCTAssertEqual(artifact3, "build/GitHubWebHook/GitHubWebHook.zip")
103103
let environment = githubWebHook.environment?.dictionary
@@ -107,7 +107,7 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
107107
XCTAssertEqual(githubWebHook.events.first?.httpAPI?.method, .post)
108108
}
109109

110-
func testWriteServerlessWebook() throws {
110+
func test_WriteServerlessWebook() throws {
111111
let serverlessYml = try fixture(name: "serverless_webhook", type: "yml")
112112
let decoder = YAMLDecoder()
113113
let serverlessConfig = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
@@ -117,4 +117,51 @@ final class ServerlessHttpAPILambdaTests: XCTestCase {
117117
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: data)
118118
XCTAssertEqual(serverlessConfig, serverlessConfig2)
119119
}
120+
121+
func test_InitServerlessYml() throws {
122+
let decoder = YAMLDecoder()
123+
let serverlessYml = try fixture(name: "serverless_webhook", type: "yml")
124+
let serverlessConfig2 = try decoder.decode(ServerlessConfig.self, from: serverlessYml)
125+
126+
let service: String = "swift-webhook"
127+
let region: Region = .us_east_1
128+
let runtime: Runtime = .providedAl2
129+
let architecture: Architecture = .arm64
130+
let memorySize: Int = 256
131+
132+
let lambdasParams: [HttpAPILambdaParams] = [
133+
HttpAPILambdaParams(
134+
name: "postWebHook",
135+
handler: "post-webhook",
136+
event: .init(path: "/webhook", method: .post),
137+
environment: nil,
138+
artifact: "build/WebHook/WebHook.zip"
139+
),
140+
HttpAPILambdaParams(
141+
name: "getWebHook",
142+
handler: "get-webhook",
143+
event: .init(path: "/webhook", method: .get),
144+
environment: nil,
145+
artifact: "build/WebHook/WebHook.zip"
146+
),
147+
HttpAPILambdaParams(
148+
name: "githubWebHook",
149+
handler: "github-webhook",
150+
event: .init(path: "/github-webhook", method: .post),
151+
environment: YAMLContent.dictionary(
152+
["WEBHOOK_SECRET": .string("${ssm:/dev/swift-webhook/webhook_secret}")]
153+
),
154+
artifact: "build/GitHubWebHook/GitHubWebHook.zip"
155+
)
156+
]
157+
let serverlessConfig = try ServerlessConfig.webhookLambdaAPI(
158+
service: service,
159+
region: region,
160+
runtime: runtime,
161+
architecture: architecture,
162+
memorySize: memorySize,
163+
lambdasParams: lambdasParams
164+
)
165+
XCTAssertEqual(serverlessConfig, serverlessConfig2)
166+
}
120167
}

0 commit comments

Comments
 (0)