|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftAWSLambdaRuntime open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2017-2020 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 | +@testable import AWSLambdaEvents |
| 16 | +import XCTest |
| 17 | + |
| 18 | +class LambdaGatewayProxyEventTests: XCTestCase { |
| 19 | + static let exampleLambdaProxyEvent = """ |
| 20 | + { |
| 21 | + "resource": "/hello", |
| 22 | + "path": "/hello", |
| 23 | + "httpMethod": "GET", |
| 24 | + "headers": { |
| 25 | + "x-forwarded-proto":"https", |
| 26 | + "x-forwarded-for":"91.64.117.86", |
| 27 | + "x-forwarded-port":"443", |
| 28 | + "authorization":"Bearer abc123", |
| 29 | + "host":"hello.test.com", |
| 30 | + "x-amzn-trace-id":"Root=1-5ea3263d-07c5d5ddfd0788bed7dad831", |
| 31 | + "user-agent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest", |
| 32 | + "content-length":"0" |
| 33 | + }, |
| 34 | + "multiValueHeaders": { |
| 35 | + "header1": [ |
| 36 | + "value1" |
| 37 | + ], |
| 38 | + "header2": [ |
| 39 | + "value1", |
| 40 | + "value2" |
| 41 | + ] |
| 42 | + }, |
| 43 | + "queryStringParameters": { |
| 44 | + "foo":"bar" |
| 45 | + }, |
| 46 | + "multiValueQueryStringParameters": { |
| 47 | + "parameter1": [ |
| 48 | + "value1", |
| 49 | + "value2" |
| 50 | + ], |
| 51 | + "parameter2": [ |
| 52 | + "value" |
| 53 | + ] |
| 54 | + }, |
| 55 | + "requestContext": { |
| 56 | + "accountId": "123456789012", |
| 57 | + "apiId":"pb5dg6g3rg", |
| 58 | + "authorizer": { |
| 59 | + "scopes":[ |
| 60 | + "hello" |
| 61 | + ], |
| 62 | + "claims":{ |
| 63 | + "aud":"customers", |
| 64 | + "iss":"https://hello.test.com/", |
| 65 | + "iat":"1587749276", |
| 66 | + "exp":"1587756476" |
| 67 | + } |
| 68 | + }, |
| 69 | + "domainName": "id.execute-api.us-east-1.amazonaws.com", |
| 70 | + "domainPrefix": "id", |
| 71 | + "extendedRequestId": "request-id", |
| 72 | + "httpMethod": "GET", |
| 73 | + "identity": { |
| 74 | + "accessKey": null, |
| 75 | + "accountId": null, |
| 76 | + "caller": null, |
| 77 | + "cognitoAuthenticationProvider": null, |
| 78 | + "cognitoAuthenticationType": null, |
| 79 | + "cognitoIdentityId": null, |
| 80 | + "cognitoIdentityPoolId": null, |
| 81 | + "principalOrgId": null, |
| 82 | + "sourceIp":"91.64.117.86", |
| 83 | + "user": null, |
| 84 | + "userAgent":"Paw/3.1.10 (Macintosh; OS X/10.15.4) GCDHTTPRequest", |
| 85 | + "userArn": null, |
| 86 | + "clientCert": { |
| 87 | + "clientCertPem": "CERT_CONTENT", |
| 88 | + "subjectDN": "www.example.com", |
| 89 | + "issuerDN": "Example issuer", |
| 90 | + "serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1", |
| 91 | + "validity": { |
| 92 | + "notBefore": "May 28 12:30:02 2019 GMT", |
| 93 | + "notAfter": "Aug 5 09:36:04 2021 GMT" |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + "path": "/hello", |
| 98 | + "protocol": "HTTP/1.1", |
| 99 | + "requestId":"LgLpnibOFiAEPCA=", |
| 100 | + "requestTime": "04/Mar/2020:19:15:17 +0000", |
| 101 | + "requestTimeEpoch": 1587750461466, |
| 102 | + "resourceId": null, |
| 103 | + "resourcePath": "/my/path", |
| 104 | + "stage": "$default" |
| 105 | + }, |
| 106 | + "pathParameters": null, |
| 107 | + "stageVariables":{ |
| 108 | + "foo":"bar" |
| 109 | + }, |
| 110 | + "body": "Hello from Lambda!", |
| 111 | + "isBase64Encoded": false |
| 112 | + } |
| 113 | + """ |
| 114 | + |
| 115 | + // MARK: - Request - |
| 116 | + |
| 117 | + // MARK: Decoding |
| 118 | + |
| 119 | + func testRequestDecodingExampleProxyRequest() { |
| 120 | + let data = LambdaGatewayProxyEventTests.exampleLambdaProxyEvent.data(using: .utf8)! |
| 121 | + var req: LambdaGatewayProxyEvent? |
| 122 | + XCTAssertNoThrow(req = try JSONDecoder().decode(LambdaGatewayProxyEvent.self, from: data)) |
| 123 | + |
| 124 | + XCTAssertEqual(req?.path, "/hello") |
| 125 | + XCTAssertEqual(req?.requestContext.httpMethod, .GET) |
| 126 | + XCTAssertEqual(req?.queryStringParameters?.count, 1) |
| 127 | + XCTAssertEqual(req?.headers.count, 8) |
| 128 | + XCTAssertNotNil(req?.body) |
| 129 | + } |
| 130 | +} |
0 commit comments