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

Add Server Tests #15

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
17 changes: 8 additions & 9 deletions Sources/VaporAWSLambdaRuntime/LambdaServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ public class LambdaServer: Server {
}
}

private let application: Application
private let responder: Responder
private let configuration: Configuration
private let eventLoop: EventLoop
private var lambdaLifecycle: Lambda.Lifecycle
let application: Application
let responder: Responder
let configuration: Configuration
let eventLoop: EventLoop
let lambdaHandler: ByteBufferLambdaHandler
let lambdaLifecycle: Lambda.Lifecycle

init(application: Application,
responder: Responder,
Expand Down Expand Up @@ -123,6 +124,7 @@ public class LambdaServer: Server {
) {
$0.eventLoop.makeSucceededFuture(handler)
}
self.lambdaHandler = handler
}

public func start(hostname _: String?, port _: Int?) throws {
Expand All @@ -142,9 +144,6 @@ public class LambdaServer: Server {
}

public func shutdown() {
// this should only be executed after someone has called `app.shutdown()`
// on lambda the ones calling should always be us!
// If we have called shutdown, the lambda server already is shutdown.
// That means, we have nothing to do here.
self.lambdaLifecycle.shutdown()
}
}
74 changes: 74 additions & 0 deletions Tests/VaporAWSLambdaRuntimeTests/LambdaServerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import AWSLambdaTesting
@testable import AWSLambdaRuntimeCore
@testable import AWSLambdaEvents
import Logging
import NIO
import Vapor
@testable import VaporAWSLambdaRuntime
import XCTest

final class LambdaServerTests: XCTestCase {
func testLambdaServer() {
let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) }

let app = Application(.development, .shared(eventLoopGroup))
defer { XCTAssertNoThrow(app.shutdown()) }

struct Name: Codable {
let name: String
}

struct Hello: Content {
let hello: String
}

app.get("hello") { (_) -> Hello in
Hello(hello: "world")
}

app.post("hello") { req -> Hello in
let name = try req.content.decode(Name.self)
return Hello(hello: name.name)
}

let server = app.lambda.server.shared
let handler = server.lambdaHandler as! APIGatewayV2Handler

let request = APIGateway.V2.Request(
version: "1.1",
routeKey: "/test/123",
rawPath: "/test/123",
rawQueryString: "",
cookies: nil,
headers: [:],
queryStringParameters: nil,
pathParameters: nil,
context: .init(
accountId: "123",
apiId: "abc",
domainName: "apigateway.region",
domainPrefix: "",
stage: "default",
requestId: "123",
http: .init(
method: .GET,
path: "/test/123",
protocol: "https",
sourceIp: "127.0.0.1",
userAgent: "test-user-agent"
),
authorizer: nil,
time: "123",
timeEpoch: UInt64(Date().timeIntervalSince1970)
),
stageVariables: nil,
body: nil,
isBase64Encoded: false)

var response: APIGateway.V2.Response?
XCTAssertNoThrow(response = try Lambda.test(handler, with: request))

// XCTAssertNoThrow(try server.onShutdown.wait())
}
}