|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) YEARS 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 Foundation |
| 16 | +import Testing |
| 17 | + |
| 18 | +@testable import AWSLambdaEvents |
| 19 | + |
| 20 | +@Suite |
| 21 | +class APIGatewayWebSocketsTests { |
| 22 | + static let exampleConnectEventBody = """ |
| 23 | + { |
| 24 | + "headers": { |
| 25 | + "Host": "lqrlmblaa2.execute-api.us-east-1.amazonaws.com", |
| 26 | + "Origin": "wss://lqrlmblaa2.execute-api.us-east-1.amazonaws.com", |
| 27 | + "Sec-WebSocket-Extensions": "", |
| 28 | + "Sec-WebSocket-Key": "am5ubWVpbHd3bmNyYXF0ag==", |
| 29 | + "Sec-WebSocket-Version": "13", |
| 30 | + "X-Amzn-Trace-Id": "Root=1-64b83950-42de8e247b4c2b43091ef67c", |
| 31 | + "X-Forwarded-For": "24.148.42.16", |
| 32 | + "X-Forwarded-Port": "443", |
| 33 | + "X-Forwarded-Proto": "https" |
| 34 | + }, |
| 35 | + "multiValueHeaders": { |
| 36 | + "Host": [ "lqrlmblaa2.execute-api.us-east-1.amazonaws.com" ], |
| 37 | + "Origin": [ "wss://lqrlmblaa2.execute-api.us-east-1.amazonaws.com" ], |
| 38 | + "Sec-WebSocket-Extensions": [ |
| 39 | + "permessage-deflate; client_max_window_bits; server_max_window_bits=15" |
| 40 | + ], |
| 41 | + "Sec-WebSocket-Key": [ "am5ubWVpbHd3bmNyYXF0ag==" ], |
| 42 | + "Sec-WebSocket-Version": [ "13" ], |
| 43 | + "X-Amzn-Trace-Id": [ "Root=1-64b83950-42de8e247b4c2b43091ef67c" ], |
| 44 | + "X-Forwarded-For": [ "24.148.42.16" ], |
| 45 | + "X-Forwarded-Port": [ "443" ], |
| 46 | + "X-Forwarded-Proto": [ "https" ] |
| 47 | + }, |
| 48 | + "requestContext": { |
| 49 | + "routeKey": "$connect", |
| 50 | + "eventType": "CONNECT", |
| 51 | + "extendedRequestId": "IU3kkGyEoAMFwZQ=", |
| 52 | + "requestTime": "19/Jul/2023:19:28:16 +0000", |
| 53 | + "messageDirection": "IN", |
| 54 | + "stage": "dev", |
| 55 | + "connectedAt": 1689794896145, |
| 56 | + "requestTimeEpoch": 1689794896162, |
| 57 | + "identity": { "sourceIp": "24.148.42.16" }, |
| 58 | + "requestId": "IU3kkGyEoAMFwZQ=", |
| 59 | + "domainName": "lqrlmblaa2.execute-api.us-east-1.amazonaws.com", |
| 60 | + "connectionId": "IU3kkeN4IAMCJwA=", |
| 61 | + "apiId": "lqrlmblaa2" |
| 62 | + }, |
| 63 | + "isBase64Encoded": false |
| 64 | + } |
| 65 | + """ |
| 66 | + |
| 67 | + // MARK: - Request - |
| 68 | + |
| 69 | + // MARK: Decoding |
| 70 | + @Test func testRequestDecodingExampleConnectRequest() async throws { |
| 71 | + let data = APIGatewayWebSocketsTests.exampleConnectEventBody.data(using: .utf8)! |
| 72 | + let req = try JSONDecoder().decode(APIGatewayWebSocketRequest.self, from: data) |
| 73 | + |
| 74 | + #expect(req.context.routeKey == "$connect") |
| 75 | + #expect(req.context.connectionId == "IU3kkeN4IAMCJwA=") |
| 76 | + #expect(req.body == nil) |
| 77 | + } |
| 78 | +} |
0 commit comments