Skip to content

Commit

Permalink
Improve code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrea Scuderi committed Mar 30, 2024
1 parent 248ffbf commit 7f3494e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
6 changes: 4 additions & 2 deletions Tests/BreezeLambdaWebHookTests/BreezeLambdaWebHookTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyPostWebHook>.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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"body": "body value",
"body": "{ \"value\": \"body value\" }",
"headers": {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
Expand Down
55 changes: 55 additions & 0 deletions Tests/BreezeLambdaWebHookTests/Lambda.swift
Original file line number Diff line number Diff line change
@@ -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<Handler: LambdaHandler>(
_ 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)
}
}
12 changes: 11 additions & 1 deletion Tests/BreezeLambdaWebHookTests/MyPostWebHook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 7f3494e

Please sign in to comment.