|
13 | 13 | //===----------------------------------------------------------------------===//
|
14 | 14 |
|
15 | 15 | @testable import AWSLambdaRuntimeCore
|
| 16 | +import Logging |
| 17 | +import NIO |
| 18 | +import NIOFoundationCompat |
| 19 | +import NIOHTTP1 |
| 20 | +import NIOTestUtils |
16 | 21 | import XCTest
|
17 | 22 |
|
18 | 23 | class LambdaRuntimeClientTest: XCTestCase {
|
@@ -209,6 +214,110 @@ class LambdaRuntimeClientTest: XCTestCase {
|
209 | 214 | XCTAssertEqual(#"{"errorType":"error","errorMessage":"🥑👨👩👧👧👩👩👧👧👨👨👧"}"#, String(decoding: emojiBytes, as: Unicode.UTF8.self))
|
210 | 215 | }
|
211 | 216 |
|
| 217 | + func testInitializationErrorReport() { |
| 218 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 219 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 220 | + |
| 221 | + let server = NIOHTTP1TestServer(group: eventLoopGroup) |
| 222 | + defer { XCTAssertNoThrow(try server.stop()) } |
| 223 | + |
| 224 | + let logger = Logger(label: "TestLogger") |
| 225 | + let client = Lambda.RuntimeClient(eventLoop: eventLoopGroup.next(), configuration: .init(address: "127.0.0.1:\(server.serverPort)")) |
| 226 | + let result = client.reportInitializationError(logger: logger, error: TestError("boom")) |
| 227 | + |
| 228 | + var inboundHeader: HTTPServerRequestPart? |
| 229 | + XCTAssertNoThrow(inboundHeader = try server.readInbound()) |
| 230 | + guard case .head(let head) = try? XCTUnwrap(inboundHeader) else { XCTFail("Expected to get a head first"); return } |
| 231 | + XCTAssertEqual(head.headers["lambda-runtime-function-error-type"], ["Unhandled"]) |
| 232 | + XCTAssertEqual(head.headers["user-agent"], ["Swift-Lambda/Unknown"]) |
| 233 | + |
| 234 | + var inboundBody: HTTPServerRequestPart? |
| 235 | + XCTAssertNoThrow(inboundBody = try server.readInbound()) |
| 236 | + guard case .body(let body) = try? XCTUnwrap(inboundBody) else { XCTFail("Expected body after head"); return } |
| 237 | + XCTAssertEqual(try JSONDecoder().decode(ErrorResponse.self, from: body).errorMessage, "boom") |
| 238 | + |
| 239 | + XCTAssertEqual(try server.readInbound(), .end(nil)) |
| 240 | + |
| 241 | + XCTAssertNoThrow(try server.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .accepted)))) |
| 242 | + XCTAssertNoThrow(try server.writeOutbound(.end(nil))) |
| 243 | + XCTAssertNoThrow(try result.wait()) |
| 244 | + } |
| 245 | + |
| 246 | + func testInvocationErrorReport() { |
| 247 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 248 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 249 | + |
| 250 | + let server = NIOHTTP1TestServer(group: eventLoopGroup) |
| 251 | + defer { XCTAssertNoThrow(try server.stop()) } |
| 252 | + |
| 253 | + let logger = Logger(label: "TestLogger") |
| 254 | + let client = Lambda.RuntimeClient(eventLoop: eventLoopGroup.next(), configuration: .init(address: "127.0.0.1:\(server.serverPort)")) |
| 255 | + |
| 256 | + let header = HTTPHeaders([ |
| 257 | + (AmazonHeaders.requestID, "test"), |
| 258 | + (AmazonHeaders.deadline, String(Date(timeIntervalSinceNow: 60).millisSinceEpoch)), |
| 259 | + (AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"), |
| 260 | + (AmazonHeaders.traceID, "Root=1-5bef4de7-ad49b0e87f6ef6c87fc2e700;Parent=9a9197af755a6419;Sampled=1"), |
| 261 | + ]) |
| 262 | + var inv: Lambda.Invocation? |
| 263 | + XCTAssertNoThrow(inv = try Lambda.Invocation(headers: header)) |
| 264 | + guard let invocation = inv else { return } |
| 265 | + |
| 266 | + let result = client.reportResults(logger: logger, invocation: invocation, result: Result.failure(TestError("boom"))) |
| 267 | + |
| 268 | + var inboundHeader: HTTPServerRequestPart? |
| 269 | + XCTAssertNoThrow(inboundHeader = try server.readInbound()) |
| 270 | + guard case .head(let head) = try? XCTUnwrap(inboundHeader) else { XCTFail("Expected to get a head first"); return } |
| 271 | + XCTAssertEqual(head.headers["lambda-runtime-function-error-type"], ["Unhandled"]) |
| 272 | + XCTAssertEqual(head.headers["user-agent"], ["Swift-Lambda/Unknown"]) |
| 273 | + |
| 274 | + var inboundBody: HTTPServerRequestPart? |
| 275 | + XCTAssertNoThrow(inboundBody = try server.readInbound()) |
| 276 | + guard case .body(let body) = try? XCTUnwrap(inboundBody) else { XCTFail("Expected body after head"); return } |
| 277 | + XCTAssertEqual(try JSONDecoder().decode(ErrorResponse.self, from: body).errorMessage, "boom") |
| 278 | + |
| 279 | + XCTAssertEqual(try server.readInbound(), .end(nil)) |
| 280 | + |
| 281 | + XCTAssertNoThrow(try server.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .accepted)))) |
| 282 | + XCTAssertNoThrow(try server.writeOutbound(.end(nil))) |
| 283 | + XCTAssertNoThrow(try result.wait()) |
| 284 | + } |
| 285 | + |
| 286 | + func testInvocationSuccessResponse() { |
| 287 | + let eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 288 | + defer { XCTAssertNoThrow(try eventLoopGroup.syncShutdownGracefully()) } |
| 289 | + |
| 290 | + let server = NIOHTTP1TestServer(group: eventLoopGroup) |
| 291 | + defer { XCTAssertNoThrow(try server.stop()) } |
| 292 | + |
| 293 | + let logger = Logger(label: "TestLogger") |
| 294 | + let client = Lambda.RuntimeClient(eventLoop: eventLoopGroup.next(), configuration: .init(address: "127.0.0.1:\(server.serverPort)")) |
| 295 | + |
| 296 | + let header = HTTPHeaders([ |
| 297 | + (AmazonHeaders.requestID, "test"), |
| 298 | + (AmazonHeaders.deadline, String(Date(timeIntervalSinceNow: 60).millisSinceEpoch)), |
| 299 | + (AmazonHeaders.invokedFunctionARN, "arn:aws:lambda:us-east-1:123456789012:function:custom-runtime"), |
| 300 | + (AmazonHeaders.traceID, "Root=1-5bef4de7-ad49b0e87f6ef6c87fc2e700;Parent=9a9197af755a6419;Sampled=1"), |
| 301 | + ]) |
| 302 | + var inv: Lambda.Invocation? |
| 303 | + XCTAssertNoThrow(inv = try Lambda.Invocation(headers: header)) |
| 304 | + guard let invocation = inv else { return } |
| 305 | + |
| 306 | + let result = client.reportResults(logger: logger, invocation: invocation, result: Result.success(nil)) |
| 307 | + |
| 308 | + var inboundHeader: HTTPServerRequestPart? |
| 309 | + XCTAssertNoThrow(inboundHeader = try server.readInbound()) |
| 310 | + guard case .head(let head) = try? XCTUnwrap(inboundHeader) else { XCTFail("Expected to get a head first"); return } |
| 311 | + XCTAssertFalse(head.headers.contains(name: "lambda-runtime-function-error-type")) |
| 312 | + XCTAssertEqual(head.headers["user-agent"], ["Swift-Lambda/Unknown"]) |
| 313 | + |
| 314 | + XCTAssertEqual(try server.readInbound(), .end(nil)) |
| 315 | + |
| 316 | + XCTAssertNoThrow(try server.writeOutbound(.head(.init(version: .init(major: 1, minor: 1), status: .accepted)))) |
| 317 | + XCTAssertNoThrow(try server.writeOutbound(.end(nil))) |
| 318 | + XCTAssertNoThrow(try result.wait()) |
| 319 | + } |
| 320 | + |
212 | 321 | class Behavior: LambdaServerBehavior {
|
213 | 322 | var state = 0
|
214 | 323 |
|
|
0 commit comments