Skip to content

[TestServer] Fix 488 and add simple test lambda #489

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

Merged
merged 3 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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: 6 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ let package = Package(
.byName(name: "AWSLambdaTesting")
]
),
.executableTarget(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this was for your own test. I think we can remove this

name: "HelloWorldLambda",
dependencies: [
"AWSLambdaRuntime"
]
),
// for perf testing
.executableTarget(
name: "MockServer",
Expand Down
23 changes: 17 additions & 6 deletions Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -352,33 +352,44 @@ private struct LambdaHTTPServer {
// :requestID/response endpoint is called by the lambda posting the response
case (.POST, let url) where url.hasSuffix(Consts.postResponseURLSuffix):
let parts = head.uri.split(separator: "/")
guard let requestId = parts.count > 2 ? String(parts[parts.count - 2]) : nil else {
guard let requestID = parts.count > 2 ? String(parts[parts.count - 2]) : nil else {
// the request is malformed, since we were expecting a requestId in the path
return .init(status: .badRequest)
}
// enqueue the lambda function response to be served as response to the client /invoke
logger.trace("/:requestID/response received response", metadata: ["requestId": "\(requestId)"])
logger.trace("/:requestID/response received response", metadata: ["requestId": "\(requestID)"])
await self.responsePool.push(
LocalServerResponse(
id: requestId,
id: requestID,
status: .ok,
headers: [("Content-Type", "application/json")],
body: body
)
)

// tell the Lambda function we accepted the response
return .init(id: requestId, status: .accepted)
return .init(id: requestID, status: .accepted)

// :requestID/error endpoint is called by the lambda posting an error response
// we accept all requestID and we do not handle the body, we just acknowledge the request
case (.POST, let url) where url.hasSuffix(Consts.postErrorURLSuffix):
let parts = head.uri.split(separator: "/")
guard let _ = parts.count > 2 ? String(parts[parts.count - 2]) : nil else {
guard let requestID = parts.count > 2 ? String(parts[parts.count - 2]) : nil else {
// the request is malformed, since we were expecting a requestId in the path
return .init(status: .badRequest)
}
return .init(status: .ok)
// enqueue the lambda function response to be served as response to the client /invoke
logger.trace("/:requestID/response received response", metadata: ["requestId": "\(requestID)"])
await self.responsePool.push(
LocalServerResponse(
id: requestID,
status: .internalServerError,
headers: [("Content-Type", "application/json")],
body: body
)
)

return .init(status: .accepted)

// unknown call
default:
Expand Down
29 changes: 29 additions & 0 deletions Sources/HelloWorldLambda/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//===----------------------------------------------------------------------===//
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be remove for the PR too,

//
// This source file is part of the SwiftAWSLambdaRuntime open source project
//
// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import AWSLambdaRuntime

struct Request: Codable {
var name: String
}

struct Response: Codable {
var greeting: String
}

let runtime = LambdaRuntime { (_ request: Request, context) -> Response in
Response(greeting: "Hello \(request.name)")
}

try await runtime.run()
Loading