@@ -17,11 +17,15 @@ import XCTest
17
17
18
18
class APIGatewayTests : XCTestCase {
19
19
static let exampleGetEventBody = """
20
+ { " httpMethod " : " GET " , " body " : null, " resource " : " /test " , " requestContext " : { " resourceId " : " 123456 " , " apiId " : " 1234567890 " , " domainName " : " 1234567890.execute-api.us-east-1.amazonaws.com " , " resourcePath " : " /test " , " httpMethod " : " GET " , " requestId " : " c6af9ac6-7b61-11e6-9a41-93e8deadbeef " , " accountId " : " 123456789012 " , " stage " : " Prod " , " identity " : { " apiKey " : null, " userArn " : null, " cognitoAuthenticationType " : null, " caller " : null, " userAgent " : " Custom User Agent String " , " user " : null, " cognitoIdentityPoolId " : null, " cognitoAuthenticationProvider " : null, " sourceIp " : " 127.0.0.1 " , " accountId " : null}, " extendedRequestId " : null, " path " : " /test " }, " queryStringParameters " : null, " multiValueQueryStringParameters " : null, " headers " : { " Host " : " 127.0.0.1:3000 " , " Connection " : " keep-alive " , " Cache-Control " : " max-age=0 " , " Dnt " : " 1 " , " Upgrade-Insecure-Requests " : " 1 " , " User-Agent " : " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36 Edg/78.0.276.24 " , " Sec-Fetch-User " : " ?1 " , " Accept " : " text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 " , " Sec-Fetch-Site " : " none " , " Sec-Fetch-Mode " : " navigate " , " Accept-Encoding " : " gzip, deflate, br " , " Accept-Language " : " en-US,en;q=0.9 " , " X-Forwarded-Proto " : " http " , " X-Forwarded-Port " : " 3000 " }, " multiValueHeaders " : { " Host " : [ " 127.0.0.1:3000 " ], " Connection " : [ " keep-alive " ], " Cache-Control " : [ " max-age=0 " ], " Dnt " : [ " 1 " ], " Upgrade-Insecure-Requests " : [ " 1 " ], " User-Agent " : [ " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36 Edg/78.0.276.24 " ], " Sec-Fetch-User " : [ " ?1 " ], " Accept " : [ " text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 " ], " Sec-Fetch-Site " : [ " none " ], " Sec-Fetch-Mode " : [ " navigate " ], " Accept-Encoding " : [ " gzip, deflate, br " ], " Accept-Language " : [ " en-US,en;q=0.9 " ], " X-Forwarded-Proto " : [ " http " ], " X-Forwarded-Port " : [ " 3000 " ]}, " pathParameters " : null, " stageVariables " : null, " path " : " /test " , " isBase64Encoded " : false}
21
+ """
22
+ // This event is here to preserve backwards compatibility for events without the `domainName` field, see [discussion in #24](https://github.com/swift-server/swift-aws-lambda-events/pull/24#discussion_r969038350).
23
+ static let exampleGetEventBodyWithoutDomainName = """
20
24
{ " httpMethod " : " GET " , " body " : null, " resource " : " /test " , " requestContext " : { " resourceId " : " 123456 " , " apiId " : " 1234567890 " , " resourcePath " : " /test " , " httpMethod " : " GET " , " requestId " : " c6af9ac6-7b61-11e6-9a41-93e8deadbeef " , " accountId " : " 123456789012 " , " stage " : " Prod " , " identity " : { " apiKey " : null, " userArn " : null, " cognitoAuthenticationType " : null, " caller " : null, " userAgent " : " Custom User Agent String " , " user " : null, " cognitoIdentityPoolId " : null, " cognitoAuthenticationProvider " : null, " sourceIp " : " 127.0.0.1 " , " accountId " : null}, " extendedRequestId " : null, " path " : " /test " }, " queryStringParameters " : null, " multiValueQueryStringParameters " : null, " headers " : { " Host " : " 127.0.0.1:3000 " , " Connection " : " keep-alive " , " Cache-Control " : " max-age=0 " , " Dnt " : " 1 " , " Upgrade-Insecure-Requests " : " 1 " , " User-Agent " : " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36 Edg/78.0.276.24 " , " Sec-Fetch-User " : " ?1 " , " Accept " : " text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 " , " Sec-Fetch-Site " : " none " , " Sec-Fetch-Mode " : " navigate " , " Accept-Encoding " : " gzip, deflate, br " , " Accept-Language " : " en-US,en;q=0.9 " , " X-Forwarded-Proto " : " http " , " X-Forwarded-Port " : " 3000 " }, " multiValueHeaders " : { " Host " : [ " 127.0.0.1:3000 " ], " Connection " : [ " keep-alive " ], " Cache-Control " : [ " max-age=0 " ], " Dnt " : [ " 1 " ], " Upgrade-Insecure-Requests " : [ " 1 " ], " User-Agent " : [ " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36 Edg/78.0.276.24 " ], " Sec-Fetch-User " : [ " ?1 " ], " Accept " : [ " text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3 " ], " Sec-Fetch-Site " : [ " none " ], " Sec-Fetch-Mode " : [ " navigate " ], " Accept-Encoding " : [ " gzip, deflate, br " ], " Accept-Language " : [ " en-US,en;q=0.9 " ], " X-Forwarded-Proto " : [ " http " ], " X-Forwarded-Port " : [ " 3000 " ]}, " pathParameters " : null, " stageVariables " : null, " path " : " /test " , " isBase64Encoded " : false}
21
25
"""
22
26
23
27
static let todoPostEventBody = """
24
- { " httpMethod " : " POST " , " body " : " { \\ " title \\ " : \\ " a todo \\ " } " , " resource " : " /todos " , " requestContext " : { " resourceId " : " 123456 " , " apiId " : " 1234567890 " , " resourcePath " : " /todos " , " httpMethod " : " POST " , " requestId " : " c6af9ac6-7b61-11e6-9a41-93e8deadbeef " , " accountId " : " 123456789012 " , " stage " : " test " , " identity " : { " apiKey " : null, " userArn " : null, " cognitoAuthenticationType " : null, " caller " : null, " userAgent " : " Custom User Agent String " , " user " : null, " cognitoIdentityPoolId " : null, " cognitoAuthenticationProvider " : null, " sourceIp " : " 127.0.0.1 " , " accountId " : null}, " extendedRequestId " : null, " path " : " /todos " }, " queryStringParameters " : null, " multiValueQueryStringParameters " : null, " headers " : { " Host " : " 127.0.0.1:3000 " , " Connection " : " keep-alive " , " Content-Length " : " 18 " , " Pragma " : " no-cache " , " Cache-Control " : " no-cache " , " Accept " : " text/plain, */*; q=0.01 " , " Origin " : " http://todobackend.com " , " User-Agent " : " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.36 Safari/537.36 Edg/79.0.309.25 " , " Dnt " : " 1 " , " Content-Type " : " application/json " , " Sec-Fetch-Site " : " cross-site " , " Sec-Fetch-Mode " : " cors " , " Referer " : " http://todobackend.com/specs/index.html?http://127.0.0.1:3000/todos " , " Accept-Encoding " : " gzip, deflate, br " , " Accept-Language " : " en-US,en;q=0.9 " , " X-Forwarded-Proto " : " http " , " X-Forwarded-Port " : " 3000 " }, " multiValueHeaders " : { " Host " : [ " 127.0.0.1:3000 " ], " Connection " : [ " keep-alive " ], " Content-Length " : [ " 18 " ], " Pragma " : [ " no-cache " ], " Cache-Control " : [ " no-cache " ], " Accept " : [ " text/plain, */*; q=0.01 " ], " Origin " : [ " http://todobackend.com " ], " User-Agent " : [ " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.36 Safari/537.36 Edg/79.0.309.25 " ], " Dnt " : [ " 1 " ], " Content-Type " : [ " application/json " ], " Sec-Fetch-Site " : [ " cross-site " ], " Sec-Fetch-Mode " : [ " cors " ], " Referer " : [ " http://todobackend.com/specs/index.html?http://127.0.0.1:3000/todos " ], " Accept-Encoding " : [ " gzip, deflate, br " ], " Accept-Language " : [ " en-US,en;q=0.9 " ], " X-Forwarded-Proto " : [ " http " ], " X-Forwarded-Port " : [ " 3000 " ]}, " pathParameters " : null, " stageVariables " : null, " path " : " /todos " , " isBase64Encoded " : false}
28
+ { " httpMethod " : " POST " , " body " : " { \\ " title \\ " : \\ " a todo \\ " } " , " resource " : " /todos " , " requestContext " : { " resourceId " : " 123456 " , " apiId " : " 1234567890 " , " domainName " : " 1234567890.execute-api.us-east-1.amazonaws.com " , " resourcePath " : " /todos " , " httpMethod " : " POST " , " requestId " : " c6af9ac6-7b61-11e6-9a41-93e8deadbeef " , " accountId " : " 123456789012 " , " stage " : " test " , " identity " : { " apiKey " : null, " userArn " : null, " cognitoAuthenticationType " : null, " caller " : null, " userAgent " : " Custom User Agent String " , " user " : null, " cognitoIdentityPoolId " : null, " cognitoAuthenticationProvider " : null, " sourceIp " : " 127.0.0.1 " , " accountId " : null}, " extendedRequestId " : null, " path " : " /todos " }, " queryStringParameters " : null, " multiValueQueryStringParameters " : null, " headers " : { " Host " : " 127.0.0.1:3000 " , " Connection " : " keep-alive " , " Content-Length " : " 18 " , " Pragma " : " no-cache " , " Cache-Control " : " no-cache " , " Accept " : " text/plain, */*; q=0.01 " , " Origin " : " http://todobackend.com " , " User-Agent " : " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.36 Safari/537.36 Edg/79.0.309.25 " , " Dnt " : " 1 " , " Content-Type " : " application/json " , " Sec-Fetch-Site " : " cross-site " , " Sec-Fetch-Mode " : " cors " , " Referer " : " http://todobackend.com/specs/index.html?http://127.0.0.1:3000/todos " , " Accept-Encoding " : " gzip, deflate, br " , " Accept-Language " : " en-US,en;q=0.9 " , " X-Forwarded-Proto " : " http " , " X-Forwarded-Port " : " 3000 " }, " multiValueHeaders " : { " Host " : [ " 127.0.0.1:3000 " ], " Connection " : [ " keep-alive " ], " Content-Length " : [ " 18 " ], " Pragma " : [ " no-cache " ], " Cache-Control " : [ " no-cache " ], " Accept " : [ " text/plain, */*; q=0.01 " ], " Origin " : [ " http://todobackend.com " ], " User-Agent " : [ " Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.36 Safari/537.36 Edg/79.0.309.25 " ], " Dnt " : [ " 1 " ], " Content-Type " : [ " application/json " ], " Sec-Fetch-Site " : [ " cross-site " ], " Sec-Fetch-Mode " : [ " cors " ], " Referer " : [ " http://todobackend.com/specs/index.html?http://127.0.0.1:3000/todos " ], " Accept-Encoding " : [ " gzip, deflate, br " ], " Accept-Language " : [ " en-US,en;q=0.9 " ], " X-Forwarded-Proto " : [ " http " ], " X-Forwarded-Port " : [ " 3000 " ]}, " pathParameters " : null, " stageVariables " : null, " path " : " /todos " , " isBase64Encoded " : false}
25
29
"""
26
30
27
31
// MARK: - Request -
@@ -37,6 +41,16 @@ class APIGatewayTests: XCTestCase {
37
41
XCTAssertEqual ( req? . httpMethod, . GET)
38
42
}
39
43
44
+ func testRequestDecodingExampleGetRequestWithoutDomainName( ) {
45
+ let data = APIGatewayTests . exampleGetEventBodyWithoutDomainName. data ( using: . utf8) !
46
+ var req : APIGatewayRequest ?
47
+ XCTAssertNoThrow ( req = try JSONDecoder ( ) . decode ( APIGatewayRequest . self, from: data) )
48
+
49
+ XCTAssertEqual ( req? . path, " /test " )
50
+ XCTAssertEqual ( req? . httpMethod, . GET)
51
+ XCTAssertNil ( req? . requestContext. domainName)
52
+ }
53
+
40
54
func testRequestDecodingTodoPostRequest( ) {
41
55
let data = APIGatewayTests . todoPostEventBody. data ( using: . utf8) !
42
56
var req : APIGatewayRequest ?
0 commit comments