Skip to content

Commit 225ddd8

Browse files
committed
APIGatewayCustomAuthorizer event test
1 parent 35b7dda commit 225ddd8

File tree

5 files changed

+118
-22
lines changed

5 files changed

+118
-22
lines changed

gradle/libs.versions.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ allopen = "2.0.20"
1212
mokkery = "2.3.0"
1313
kotlinx-resources = "0.9.0"
1414
gradle-publish = "1.2.0"
15+
trueangle-lambda = "0.0.1"
1516

1617
[libraries]
1718
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
@@ -39,4 +40,5 @@ ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
3940
allopen = { id = "org.jetbrains.kotlin.plugin.allopen", version.ref = "allopen" }
4041
mokkery = { id = "dev.mokkery", version.ref = "mokkery" }
4142
kotlinx-resources = { id = "com.goncalossilva.resources", version.ref = "kotlinx-resources" }
42-
gradle-publish = { id = "com.gradle.plugin-publish", version.ref = "gradle-publish" }
43+
gradle-publish = { id = "com.gradle.plugin-publish", version.ref = "gradle-publish" }
44+
trueangle-lambda = { id = "io.github.trueangle.plugin.lambda", version.ref = "trueangle-lambda" }

lambda-events/src/commonMain/kotlin/io/github/trueangle/knative/lambda/runtime/events/apigateway/APIGateway.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,10 @@ data class APIGatewayRequest(
6464
}
6565

6666
@Serializable
67-
data class APIGatewayResponse<T>(
67+
data class APIGatewayResponse(
6868
@SerialName("statusCode") val statusCode: Int,
6969
@SerialName("headers") val headers: Map<String, String>? = null,
7070
@SerialName("multiValueHeaders") val multiValueHeaders: Map<String, String>? = null,
71-
@SerialName("body") val body: T? = null,
71+
@SerialName("body") val body: String? = null,
7272
@SerialName("isBase64Encoded") val isBase64Encoded: Boolean? = null
7373
)

lambda-events/src/commonMain/kotlin/io/github/trueangle/knative/lambda/runtime/events/apigateway/APIGatewayCustomAuthorizer.kt

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,53 @@ data class APIGatewayCustomAuthorizer(
1717
@SerialName("queryStringParameters") val queryStringParameters: Map<String, String>?,
1818
@SerialName("pathParameters") val pathParameters: Map<String, String>?,
1919
@SerialName("stageVariables") val stageVariables: Map<String, String>?,
20-
@SerialName("requestContext") val requestContext: RequestContext?
20+
@SerialName("requestContext") val requestContext: Context?
2121
) {
2222
@Serializable
23-
data class RequestContext(
24-
@SerialName("path") val path: String?,
25-
@SerialName("accountId") val accountId: String?,
26-
@SerialName("resourceId") val resourceId: String?,
27-
@SerialName("stage") val stage: String?,
28-
@SerialName("requestId") val requestId: String?,
29-
@SerialName("identity") val identity: Identity?,
30-
@SerialName("resourcePath") val resourcePath: String?,
31-
@SerialName("httpMethod") val httpMethod: String?,
32-
@SerialName("apiId") val apiId: String?
33-
)
23+
data class Context(
24+
@SerialName("path") val path: String,
25+
@SerialName("accountId") val accountId: String,
26+
@SerialName("resourceId") val resourceId: String,
27+
@SerialName("stage") val stage: String,
28+
@SerialName("requestId") val requestId: String,
29+
@SerialName("identity") val identity: Identity,
30+
@SerialName("resourcePath") val resourcePath: String,
31+
@SerialName("httpMethod") val httpMethod: String,
32+
@SerialName("apiId") val apiId: String,
33+
@SerialName("http") val http: HTTP,
34+
@SerialName("routeKey") val routeKey: String,
35+
@SerialName("time") val time: String,
36+
@SerialName("timeEpoch") val timeEpoch: Long,
37+
) {
38+
@Serializable
39+
data class HTTP(
40+
@SerialName("method") val method: String,
41+
@SerialName("path") val path: String,
42+
@SerialName("protocol") val protocol: String,
43+
@SerialName("sourceIp") val sourceIp: String,
44+
@SerialName("userAgent") val userAgent: String
45+
)
46+
}
3447

3548
@Serializable
3649
data class Identity(
37-
@SerialName("apiKey") val apiKey: String?,
38-
@SerialName("sourceIp") val sourceIp: String?
39-
)
50+
@SerialName("apiKey") val apiKey: String,
51+
@SerialName("sourceIp") val sourceIp: String,
52+
@SerialName("clientCert") val clientCert: ClientCert
53+
) {
54+
@Serializable
55+
data class ClientCert(
56+
@SerialName("clientCertPem") val clientCertPem: String,
57+
@SerialName("issuerDN") val issuerDN: String,
58+
@SerialName("serialNumber") val serialNumber: String,
59+
@SerialName("subjectDN") val subjectDN: String,
60+
@SerialName("validity") val validity: Validity
61+
)
62+
63+
@Serializable
64+
data class Validity(
65+
@SerialName("notAfter") val notAfter: String,
66+
@SerialName("notBefore") val notBefore: String
67+
)
68+
}
4069
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
package io.github.trueangle.knative.lambda.runtime.events.apigateway
22

33
import com.goncalossilva.resources.Resource
4-
import kotlinx.serialization.Serializable
54
import kotlinx.serialization.json.Json
65
import kotlin.test.Test
7-
import kotlin.test.assertEquals
86

97
const val RESOURCES_PATH = "src/nativeTest/resources"
108

11-
class APIGatewayRequestTest {
9+
class APIGatewayTest {
1210

1311
@Test
14-
fun `GIVEN APIGatewayRequest json AND object body THEN parse`() {
12+
fun `WHEN APIGatewayRequest json THEN parse`() {
1513
val jsonString = Resource("$RESOURCES_PATH/example-apigw-request.json").readText()
1614
Json.decodeFromString<APIGatewayRequest>(jsonString)
1715
}
16+
17+
@Test
18+
fun `WHEN APIGatewayCustomAuthorizer json THEN parse`() {
19+
val jsonString = Resource("$RESOURCES_PATH/example-apigw-v2-custom-authorizer-v1-request.json").readText()
20+
Json.decodeFromString<APIGatewayCustomAuthorizer>(jsonString)
21+
}
1822
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
{
2+
"version": "1.0",
3+
"type": "REQUEST",
4+
"methodArn": "arn:aws:execute-api:us-east-1:123456789012:abcdef123/test/GET/request",
5+
"identitySource": "user1,123",
6+
"authorizationToken": "user1,123",
7+
"resource": "/request",
8+
"path": "/request",
9+
"httpMethod": "GET",
10+
"headers": {
11+
"X-AMZ-Date": "20170718T062915Z",
12+
"Accept": "*/*",
13+
"HeaderAuth1": "headerValue1",
14+
"CloudFront-Viewer-Country": "US",
15+
"CloudFront-Forwarded-Proto": "https",
16+
"CloudFront-Is-Tablet-Viewer": "false",
17+
"CloudFront-Is-Mobile-Viewer": "false",
18+
"User-Agent": "..."
19+
},
20+
"queryStringParameters": {
21+
"QueryString1": "queryValue1"
22+
},
23+
"pathParameters": {},
24+
"stageVariables": {
25+
"StageVar1": "stageValue1"
26+
},
27+
"requestContext": {
28+
"path": "/request",
29+
"accountId": "123456789012",
30+
"resourceId": "05c7jb",
31+
"stage": "test",
32+
"requestId": "...",
33+
"identity": {
34+
"apiKey": "...",
35+
"sourceIp": "...",
36+
"clientCert": {
37+
"clientCertPem": "CERT_CONTENT",
38+
"subjectDN": "www.example.com",
39+
"issuerDN": "Example issuer",
40+
"serialNumber": "a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1:a1",
41+
"validity": {
42+
"notBefore": "May 28 12:30:02 2019 GMT",
43+
"notAfter": "Aug 5 09:36:04 2021 GMT"
44+
}
45+
}
46+
},
47+
"http": {
48+
"method": "POST",
49+
"path": "/my/path",
50+
"protocol": "HTTP/1.1",
51+
"sourceIp": "IP",
52+
"userAgent": "agent"
53+
},
54+
"resourcePath": "/request",
55+
"httpMethod": "GET",
56+
"apiId": "abcdef123",
57+
"routeKey": "$default",
58+
"time": "12/Mar/2020:19:03:58 +0000",
59+
"timeEpoch": 1583348638390
60+
}
61+
}

0 commit comments

Comments
 (0)