Skip to content

Commit f977894

Browse files
committed
[TestServer] Fix 488 and add simple test lambda
1 parent 61cd5d5 commit f977894

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

Package.swift

+6
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@ let package = Package(
8585
.byName(name: "AWSLambdaTesting")
8686
]
8787
),
88+
.executableTarget(
89+
name: "HelloWorldLambda",
90+
dependencies: [
91+
"AWSLambdaRuntime",
92+
]
93+
),
8894
// for perf testing
8995
.executableTarget(
9096
name: "MockServer",

Sources/AWSLambdaRuntimeCore/Lambda+LocalServer.swift

+17-6
Original file line numberDiff line numberDiff line change
@@ -352,33 +352,44 @@ private struct LambdaHTTPServer {
352352
// :requestID/response endpoint is called by the lambda posting the response
353353
case (.POST, let url) where url.hasSuffix(Consts.postResponseURLSuffix):
354354
let parts = head.uri.split(separator: "/")
355-
guard let requestId = parts.count > 2 ? String(parts[parts.count - 2]) : nil else {
355+
guard let requestID = parts.count > 2 ? String(parts[parts.count - 2]) : nil else {
356356
// the request is malformed, since we were expecting a requestId in the path
357357
return .init(status: .badRequest)
358358
}
359359
// enqueue the lambda function response to be served as response to the client /invoke
360-
logger.trace("/:requestID/response received response", metadata: ["requestId": "\(requestId)"])
360+
logger.trace("/:requestID/response received response", metadata: ["requestId": "\(requestID)"])
361361
await self.responsePool.push(
362362
LocalServerResponse(
363-
id: requestId,
363+
id: requestID,
364364
status: .ok,
365365
headers: [("Content-Type", "application/json")],
366366
body: body
367367
)
368368
)
369369

370370
// tell the Lambda function we accepted the response
371-
return .init(id: requestId, status: .accepted)
371+
return .init(id: requestID, status: .accepted)
372372

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

383394
// unknown call
384395
default:

Sources/HelloWorldLambda/main.swift

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the SwiftAWSLambdaRuntime open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the SwiftAWSLambdaRuntime project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of SwiftAWSLambdaRuntime project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import AWSLambdaRuntime
16+
17+
struct Request: Codable {
18+
var name: String
19+
}
20+
21+
struct Response: Codable {
22+
var greeting: String
23+
}
24+
25+
let runtime = LambdaRuntime { (_ request: Request, context) -> Response in
26+
Response(greeting: "Hello \(request.name)")
27+
}
28+
29+
try await runtime.run()

0 commit comments

Comments
 (0)