|
1 | | -// Copyright 2023 (c) Andrea Scuderi - https://github.com/swift-serverless |
| 1 | +// Copyright 2024 (c) Andrea Scuderi - https://github.com/swift-serverless |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | import Testing |
| 16 | +@testable import AsyncHTTPClient |
16 | 17 | import AWSLambdaEvents |
17 | | -import AWSLambdaRuntime |
18 | | -import AsyncHTTPClient |
| 18 | +@testable import AWSLambdaRuntime |
| 19 | +@testable import ServiceLifecycle |
| 20 | +import ServiceLifecycleTestKit |
19 | 21 | @testable import BreezeLambdaWebHook |
20 | 22 | import Logging |
| 23 | +#if canImport(FoundationEssentials) |
| 24 | +import FoundationEssentials |
| 25 | +#else |
21 | 26 | import Foundation |
| 27 | +#endif |
| 28 | +import NIOCore |
22 | 29 |
|
23 | | -@Suite("BreezeLambdaWebHookSuite") |
24 | | -struct BreezeLambdaWebHookTests: ~Copyable { |
25 | 30 |
|
26 | | - let decoder = JSONDecoder() |
27 | | - let config = BreezeHTTPClientConfig( |
28 | | - timeout: .seconds(1), |
29 | | - logger: Logger(label: "test") |
30 | | - ) |
31 | | - |
32 | | - init() { |
33 | | - setEnvironmentVar(name: "_HANDLER", value: "build/webhook", overwrite: true) |
34 | | - setEnvironmentVar(name: "LOCAL_LAMBDA_SERVER_ENABLED", value: "true", overwrite: true) |
35 | | - } |
36 | | - |
37 | | - deinit { |
38 | | - unsetenv("LOCAL_LAMBDA_SERVER_ENABLED") |
39 | | - unsetenv("_HANDLER") |
40 | | - } |
| 31 | +@Suite(.serialized) |
| 32 | +struct BreezeLambdaWebHookTests { |
41 | 33 |
|
42 | | - @Test("PostWhenMissingBody_ThenError") |
43 | | - func postWhenMissingBody_thenError() async throws { |
44 | | - let createRequest = try Fixtures.fixture(name: Fixtures.getWebHook, type: "json") |
45 | | - let request = try decoder.decode(APIGatewayV2Request.self, from: createRequest) |
46 | | - let apiResponse: APIGatewayV2Response = try await Lambda.test(MyPostWebHook.self, config: config, with: request) |
47 | | - let response: APIGatewayV2Response.BodyError = try apiResponse.decodeBody() |
48 | | - |
49 | | - #expect(apiResponse.statusCode == .badRequest) |
50 | | - #expect(apiResponse.headers == [ "Content-Type": "application/json" ]) |
51 | | - #expect(response.error == "invalidRequest") |
52 | | - } |
| 34 | + let decoder = JSONDecoder() |
53 | 35 |
|
54 | | - @Test("PostWhenBody_ThenValue") |
55 | | - func postWhenBody_thenValue() async throws { |
56 | | - let createRequest = try Fixtures.fixture(name: Fixtures.postWebHook, type: "json") |
57 | | - let request = try decoder.decode(APIGatewayV2Request.self, from: createRequest) |
58 | | - let apiResponse: APIGatewayV2Response = try await Lambda.test(MyPostWebHook.self, config: config, with: request) |
59 | | - let response: MyPostResponse = try apiResponse.decodeBody() |
60 | | - let body: MyPostRequest = try request.bodyObject() |
61 | | - |
62 | | - #expect(apiResponse.statusCode == .ok) |
63 | | - #expect(apiResponse.headers == [ "Content-Type": "application/json" ]) |
64 | | - #expect(response.body == body.value) |
65 | | - #expect(response.handler == "build/webhook") |
| 36 | + @Test("BreezeLambdaWebHook can be shutdown gracefully") |
| 37 | + func breezeLambdaWebHookCanBeShutdownGracefully() async throws { |
| 38 | + await testGracefulShutdown { gracefulShutdownTestTrigger in |
| 39 | + let (gracefulStream, continuation) = AsyncStream<Void>.makeStream() |
| 40 | + await withThrowingTaskGroup(of: Void.self) { group in |
| 41 | + let logger = Logger(label: "test") |
| 42 | + let config = BreezeHTTPClientConfig(timeout: .seconds(30), logger: logger) |
| 43 | + let sut = BreezeLambdaWebHook<MockHandler>.init(name: "Test", config: config) |
| 44 | + group.addTask { |
| 45 | + try await Task.sleep(nanoseconds: 1_000_000_000) |
| 46 | + gracefulShutdownTestTrigger.triggerGracefulShutdown() |
| 47 | + } |
| 48 | + group.addTask { |
| 49 | + await withGracefulShutdownHandler { |
| 50 | + do { |
| 51 | + try await sut.run() |
| 52 | + } catch { |
| 53 | + Issue.record("Error running BreezeLambdaWebHook: \(error.localizedDescription)") |
| 54 | + } |
| 55 | + } onGracefulShutdown: { |
| 56 | + logger.info("On Graceful Shutdown") |
| 57 | + continuation.yield() |
| 58 | + } |
| 59 | + } |
| 60 | + for await _ in gracefulStream { |
| 61 | + #expect(sut.name == "Test") |
| 62 | + #expect(sut.config.timeout == .seconds(30)) |
| 63 | + continuation.finish() |
| 64 | + logger.info("Graceful shutdown stream received") |
| 65 | + group.cancelAll() |
| 66 | + } |
| 67 | + } |
| 68 | + } |
66 | 69 | } |
| 70 | +} |
| 71 | + |
| 72 | +struct MockHandler: BreezeLambdaWebHookHandler { |
| 73 | + let handlerContext: HandlerContext |
67 | 74 |
|
68 | | - @Test("GetWhenMissingQuery_ThenError") |
69 | | - func getWhenMissingQuery_thenError() async throws { |
70 | | - let createRequest = try Fixtures.fixture(name: Fixtures.postWebHook, type: "json") |
71 | | - let request = try decoder.decode(APIGatewayV2Request.self, from: createRequest) |
72 | | - let apiResponse: APIGatewayV2Response = try await Lambda.test(MyGetWebHook.self, config: config, with: request) |
73 | | - let response: APIGatewayV2Response.BodyError = try apiResponse.decodeBody() |
74 | | - |
75 | | - #expect(apiResponse.statusCode == .badRequest) |
76 | | - #expect(apiResponse.headers == [ "Content-Type": "application/json" ]) |
77 | | - #expect(response.error == "invalidRequest") |
| 75 | + init(handlerContext: HandlerContext) { |
| 76 | + self.handlerContext = handlerContext |
78 | 77 | } |
79 | 78 |
|
80 | | - @Test("GetWhenQuery_ThenValue") |
81 | | - func getWhenQuery_thenValue() async throws { |
82 | | - let createRequest = try Fixtures.fixture(name: Fixtures.getWebHook, type: "json") |
83 | | - let request = try decoder.decode(APIGatewayV2Request.self, from: createRequest) |
84 | | - let apiResponse: APIGatewayV2Response = try await Lambda.test(MyGetWebHook.self, config: config, with: request) |
85 | | - let response: [String: String] = try apiResponse.decodeBody() |
86 | | - |
87 | | - #expect(apiResponse.statusCode == .ok) |
88 | | - #expect(apiResponse.headers == [ "Content-Type": "application/json" ]) |
89 | | - #expect(response.count == 2) |
| 79 | + func handle(_ event: APIGatewayV2Request, context: LambdaContext) async throws -> APIGatewayV2Response { |
| 80 | + return APIGatewayV2Response( |
| 81 | + statusCode: .ok, |
| 82 | + body: "Mock response" |
| 83 | + ) |
90 | 84 | } |
91 | 85 | } |
0 commit comments