@@ -24,6 +24,11 @@ class APIGatewayTests: XCTestCase {
24
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}
25
25
"""
26
26
27
+ // This event contains an authorizer which would be attached if a resource/method combo uses Cognito for authentication/authorization of API clients.
28
+ static let exampleGetEventBodyWithAuthorizer = """
29
+ { " 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}, " authorizer " : { " claims " : { " sub " : " 2592124a-27bf-4e30-b95f-2f21c862fc82 " , " event_id " : " fbf7193a-e3b4-462b-95b4-4df60a9fe410 " , " token_use " : " access " , " scope " : " aws.cognito.signin.user.admin phone openid profile email " , " auth_time " : " 1683085806 " , " iss " : " https://cognito-idp.us-east-1.amazonaws.com/us-east-1_pxAHZcSSX " , " exp " : " Thu May 04 03:50:06 UTC 2023 " , " version " : " 2 " , " iat " : " Wed May 03 03:50:06 UTC 2023 " , " client_id " : " 6rupb256qn12tju82occ9eppqr " , " jti " : " 5d92ca29-677e-40d9-a074-2d86fbcb4023 " , " username " : " richwolf " }}, " 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}
30
+ """
31
+
27
32
static let todoPostEventBody = """
28
33
{ " 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}
29
34
"""
@@ -51,6 +56,17 @@ class APIGatewayTests: XCTestCase {
51
56
XCTAssertNil ( req? . requestContext. domainName)
52
57
}
53
58
59
+ func testRequestDecodingExampleGetRequestWithAuthorizer( ) {
60
+ let data = APIGatewayTests . exampleGetEventBodyWithAuthorizer. data ( using: . utf8) !
61
+ var req : APIGatewayRequest ?
62
+ XCTAssertNoThrow ( req = try JSONDecoder ( ) . decode ( APIGatewayRequest . self, from: data) )
63
+
64
+ XCTAssertEqual ( req? . path, " /test " )
65
+ XCTAssertEqual ( req? . httpMethod, . GET)
66
+ XCTAssertEqual ( req? . requestContext. authorizer? . claims ? [ " scope " ] , " aws.cognito.signin.user.admin phone openid profile email " )
67
+ XCTAssertEqual ( req? . requestContext. authorizer? . claims ? [ " username " ] , " richwolf " )
68
+ }
69
+
54
70
func testRequestDecodingTodoPostRequest( ) {
55
71
let data = APIGatewayTests . todoPostEventBody. data ( using: . utf8) !
56
72
var req : APIGatewayRequest ?
0 commit comments