From 7f3494e78c94705310a279541f034a74c2ed0f84 Mon Sep 17 00:00:00 2001 From: Andrea Scuderi Date: Sat, 30 Mar 2024 17:29:24 +0100 Subject: [PATCH] Improve code coverage --- .../BreezeLambdaWebHookTests.swift | 6 +- .../Fixtures/post_webhook_api_gtw.json | 2 +- Tests/BreezeLambdaWebHookTests/Lambda.swift | 55 +++++++++++++++++++ .../MyPostWebHook.swift | 12 +++- 4 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 Tests/BreezeLambdaWebHookTests/Lambda.swift diff --git a/Tests/BreezeLambdaWebHookTests/BreezeLambdaWebHookTests.swift b/Tests/BreezeLambdaWebHookTests/BreezeLambdaWebHookTests.swift index 11067c4..8fec216 100644 --- a/Tests/BreezeLambdaWebHookTests/BreezeLambdaWebHookTests.swift +++ b/Tests/BreezeLambdaWebHookTests/BreezeLambdaWebHookTests.swift @@ -52,10 +52,12 @@ final class BreezeLambdaWebHookTests: XCTestCase { let createRequest = try Fixtures.fixture(name: Fixtures.postWebHook, type: "json") let request = try decoder.decode(APIGatewayV2Request.self, from: createRequest) let apiResponse: APIGatewayV2Response = try await Lambda.test(BreezeLambdaWebHook.self, with: request) - let response: String = try apiResponse.decodeBody() + let response: MyPostResponse = try apiResponse.decodeBody() XCTAssertEqual(apiResponse.statusCode, .ok) XCTAssertEqual(apiResponse.headers, [ "Content-Type": "application/json" ]) - XCTAssertEqual(response, "body value") + let body: MyPostRequest = try request.bodyObject() + XCTAssertEqual(response.body, body.value) + XCTAssertEqual(response.handler, "build/webhook.post") } func test_getWhenMissingQuery_thenError() async throws { diff --git a/Tests/BreezeLambdaWebHookTests/Fixtures/post_webhook_api_gtw.json b/Tests/BreezeLambdaWebHookTests/Fixtures/post_webhook_api_gtw.json index 575fb73..823a71b 100644 --- a/Tests/BreezeLambdaWebHookTests/Fixtures/post_webhook_api_gtw.json +++ b/Tests/BreezeLambdaWebHookTests/Fixtures/post_webhook_api_gtw.json @@ -1,5 +1,5 @@ { - "body": "body value", + "body": "{ \"value\": \"body value\" }", "headers": { "accept": "*/*", "accept-encoding": "gzip, deflate, br", diff --git a/Tests/BreezeLambdaWebHookTests/Lambda.swift b/Tests/BreezeLambdaWebHookTests/Lambda.swift new file mode 100644 index 0000000..8ec17f5 --- /dev/null +++ b/Tests/BreezeLambdaWebHookTests/Lambda.swift @@ -0,0 +1,55 @@ +// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import AWSLambdaEvents +import AWSLambdaRuntime +@testable import AWSLambdaRuntimeCore +import AWSLambdaTesting +import Logging +import NIO + +extension Lambda { + public static func test( + _ handlerType: Handler.Type, + with event: Handler.Event, + using config: TestConfig = .init() + ) async throws -> Handler.Output { + let logger = Logger(label: "test") + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) + defer { + try! eventLoopGroup.syncShutdownGracefully() + } + let eventLoop = eventLoopGroup.next() + + let initContext = LambdaInitializationContext.__forTestsOnly( + logger: logger, + eventLoop: eventLoop + ) + + let context = LambdaContext.__forTestsOnly( + requestID: config.requestID, + traceID: config.traceID, + invokedFunctionARN: config.invokedFunctionARN, + timeout: config.timeout, + logger: logger, + eventLoop: eventLoop + ) + let handler = try await Handler(context: initContext) + defer { + let eventLoop = initContext.eventLoop.next() + try? initContext.terminator.terminate(eventLoop: eventLoop).wait() + } + return try await handler.handle(event, context: context) + } +} diff --git a/Tests/BreezeLambdaWebHookTests/MyPostWebHook.swift b/Tests/BreezeLambdaWebHookTests/MyPostWebHook.swift index 7a3c3c1..58857d6 100644 --- a/Tests/BreezeLambdaWebHookTests/MyPostWebHook.swift +++ b/Tests/BreezeLambdaWebHookTests/MyPostWebHook.swift @@ -18,6 +18,15 @@ import AsyncHTTPClient import AWSLambdaEvents import AWSLambdaRuntimeCore +struct MyPostResponse: Codable { + let handler: String? + let body: String +} + +struct MyPostRequest: Codable { + let value: String +} + class MyPostWebHook: BreezeLambdaWebHookHandler { let handlerContext: HandlerContext @@ -29,9 +38,10 @@ class MyPostWebHook: BreezeLambdaWebHookHandler { func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: AWSLambdaEvents.APIGatewayV2Request) async -> AWSLambdaEvents.APIGatewayV2Response { do { try await Task.sleep(nanoseconds: 1_000_000) - guard let value: String = event.body else { + guard let body: MyPostRequest = try event.bodyObject() else { throw BreezeLambdaWebHookError.invalidRequest } + let value = MyPostResponse(handler: handler, body: body.value) return APIGatewayV2Response(with: value, statusCode: .ok) } catch { return APIGatewayV2Response(with: error, statusCode: .badRequest)