-
Notifications
You must be signed in to change notification settings - Fork 3
/
Lambda+Testing.swift
51 lines (49 loc) · 1.79 KB
/
Lambda+Testing.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2021-2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//
import HummingbirdLambda
import Logging
extension HBLambda where Event: LambdaTestableEvent {
/// Test `HBLambda`
///
/// The `test` closure uses the provided test client to make calls to the
/// lambda via `execute`. You can verify the contents of the output
/// event returned.
///
/// The example below is using the `.router` framework to test
/// ```swift
/// struct HelloLambda: HBAPIGatewayLambda {
/// init(context: LambdaInitializationContext) {}
///
/// func buildResponder() -> some HBResponder<Context> {
/// let router = HBRouter(context: Context.self)
/// router.get("hello") { request, _ in
/// return "Hello"
/// }
/// return router.buildResponder()
/// }
/// }
/// try await HelloLambda.test { client in
/// try await client.execute(uri: "/hello", method: .get) { response in
/// XCTAssertEqual(response.body, "Hello")
/// }
/// }
/// ```
public static func test<Value>(
logLevel: Logger.Level = .debug,
_ test: @escaping @Sendable (HBLambdaTestClient<Self>) async throws -> Value
) async throws -> Value {
let lambda = HBLambdaTestFramework<Self>(logLevel: logLevel)
return try await lambda.run(test)
}
}