Skip to content
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

Improve code coverage #1

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading