swift-aws-lambda-events
swift-aws-lambda-events copied to clipboard
Allow APIGatewayV2Request to created for testing
Expected behavior
I'm trying to test a LambdaHandler
struct MyHandler: LambdaHandler {
init(context: LambdaInitializationContext) async throws { }
func handle(_ request: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response {
...
}
}
using Lambda.test(MyHandler.self, with: <APIGatewayV2Request>)
but I can't create an APIGatewayV2Request to test with because init isn't public.
Actual behavior
I would expect to be able to create an APIGatewayV2Request via init or some convenience method so that I can write tests for my LambdaHandler.
Steps to reproduce
- Create a
LambdaHandlerwhoseEventis of typeAPIGatewayV2Request - Attempt to write a test using
Lambda.testfrom theAWSLambdaTestinglibrary.
If possible, minimal yet complete reproducer code (or URL to code)
Handler
import AWSLambdaEvents
import AWSLambdaRuntime
import Foundation
struct MyHandler: LambdaHandler {
init(context: LambdaInitializationContext) async throws { }
func handle(_ request: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response {
let response = APIGatewayV2Response(statusCode: .ok, body: "Hello World!")
return response
}
}
Test
import AWSLambdaEvents
import AWSLambdaRuntime
import AWSLambdaTesting
@testable import <MyLambda Target>
import XCTest
final class MyHandlerTests: XCTestCase {
func testOKResponse() async throws {
let request = APIGatewayV2Request() // Can't create this
let response = try await Lambda.test(MyHandler.self, with: request)
XCTAssertEqual(response.statusCode, .ok)
}
}
SwiftAWSLambdaRuntime version/commit hash
1.0.0-alpha.2
Swift & OS version (output of swift --version && uname -a)
swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0
Darwin xxx.local 23.4.0 Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:49 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6020 arm64
Hello @adamayoung
Apologies we haven't responded yet. Is this still an issue ? Did you write code to fix it ? If yes, please send the PR for review.