Skip to content

Allow APIGatewayV2Request to created for testing #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
adamayoung opened this issue Apr 10, 2024 · 1 comment
Closed

Allow APIGatewayV2Request to created for testing #48

adamayoung opened this issue Apr 10, 2024 · 1 comment
Labels
kind/enhancement Improvements to existing feature.

Comments

@adamayoung
Copy link

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

  1. Create a LambdaHandler whose Event is of type APIGatewayV2Request
  2. Attempt to write a test using Lambda.test from the AWSLambdaTesting library.

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
@sebsto sebsto added the kind/enhancement Improvements to existing feature. label May 29, 2024
@sebsto
Copy link
Contributor

sebsto commented Nov 7, 2024

@adamayoung the behaviour you're seeing is expected.
It is not valid to write

        let request = APIGatewayV2Request() // Can't create this

because it has public properties that are not optional.

You can create a request using a JSON decoder, see an example here

let data = APIGatewayV2Tests.exampleGetEventBody.data(using: .utf8)!

or by passing all mandatory properties to the initializer.

@sebsto sebsto closed this as completed Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/enhancement Improvements to existing feature.
Projects
None yet
Development

No branches or pull requests

2 participants