Skip to content

Commit a79fa20

Browse files
Merge pull request #1 from swift-serverless/feature/improve-code-coverage
Improve code coverage
2 parents 248ffbf + 7f3494e commit a79fa20

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

Tests/BreezeLambdaWebHookTests/BreezeLambdaWebHookTests.swift

+4-2
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,12 @@ final class BreezeLambdaWebHookTests: XCTestCase {
5252
let createRequest = try Fixtures.fixture(name: Fixtures.postWebHook, type: "json")
5353
let request = try decoder.decode(APIGatewayV2Request.self, from: createRequest)
5454
let apiResponse: APIGatewayV2Response = try await Lambda.test(BreezeLambdaWebHook<MyPostWebHook>.self, with: request)
55-
let response: String = try apiResponse.decodeBody()
55+
let response: MyPostResponse = try apiResponse.decodeBody()
5656
XCTAssertEqual(apiResponse.statusCode, .ok)
5757
XCTAssertEqual(apiResponse.headers, [ "Content-Type": "application/json" ])
58-
XCTAssertEqual(response, "body value")
58+
let body: MyPostRequest = try request.bodyObject()
59+
XCTAssertEqual(response.body, body.value)
60+
XCTAssertEqual(response.handler, "build/webhook.post")
5961
}
6062

6163
func test_getWhenMissingQuery_thenError() async throws {

Tests/BreezeLambdaWebHookTests/Fixtures/post_webhook_api_gtw.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"body": "body value",
2+
"body": "{ \"value\": \"body value\" }",
33
"headers": {
44
"accept": "*/*",
55
"accept-encoding": "gzip, deflate, br",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
import AWSLambdaEvents
16+
import AWSLambdaRuntime
17+
@testable import AWSLambdaRuntimeCore
18+
import AWSLambdaTesting
19+
import Logging
20+
import NIO
21+
22+
extension Lambda {
23+
public static func test<Handler: LambdaHandler>(
24+
_ handlerType: Handler.Type,
25+
with event: Handler.Event,
26+
using config: TestConfig = .init()
27+
) async throws -> Handler.Output {
28+
let logger = Logger(label: "test")
29+
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
30+
defer {
31+
try! eventLoopGroup.syncShutdownGracefully()
32+
}
33+
let eventLoop = eventLoopGroup.next()
34+
35+
let initContext = LambdaInitializationContext.__forTestsOnly(
36+
logger: logger,
37+
eventLoop: eventLoop
38+
)
39+
40+
let context = LambdaContext.__forTestsOnly(
41+
requestID: config.requestID,
42+
traceID: config.traceID,
43+
invokedFunctionARN: config.invokedFunctionARN,
44+
timeout: config.timeout,
45+
logger: logger,
46+
eventLoop: eventLoop
47+
)
48+
let handler = try await Handler(context: initContext)
49+
defer {
50+
let eventLoop = initContext.eventLoop.next()
51+
try? initContext.terminator.terminate(eventLoop: eventLoop).wait()
52+
}
53+
return try await handler.handle(event, context: context)
54+
}
55+
}

Tests/BreezeLambdaWebHookTests/MyPostWebHook.swift

+11-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ import AsyncHTTPClient
1818
import AWSLambdaEvents
1919
import AWSLambdaRuntimeCore
2020

21+
struct MyPostResponse: Codable {
22+
let handler: String?
23+
let body: String
24+
}
25+
26+
struct MyPostRequest: Codable {
27+
let value: String
28+
}
29+
2130
class MyPostWebHook: BreezeLambdaWebHookHandler {
2231

2332
let handlerContext: HandlerContext
@@ -29,9 +38,10 @@ class MyPostWebHook: BreezeLambdaWebHookHandler {
2938
func handle(context: AWSLambdaRuntimeCore.LambdaContext, event: AWSLambdaEvents.APIGatewayV2Request) async -> AWSLambdaEvents.APIGatewayV2Response {
3039
do {
3140
try await Task.sleep(nanoseconds: 1_000_000)
32-
guard let value: String = event.body else {
41+
guard let body: MyPostRequest = try event.bodyObject() else {
3342
throw BreezeLambdaWebHookError.invalidRequest
3443
}
44+
let value = MyPostResponse(handler: handler, body: body.value)
3545
return APIGatewayV2Response(with: value, statusCode: .ok)
3646
} catch {
3747
return APIGatewayV2Response(with: error, statusCode: .badRequest)

0 commit comments

Comments
 (0)