Skip to content

Commit 1a60bcb

Browse files
raoulkjmoennich
authored andcommitted
add ApiRequestContext to Request (#28)
* add ApiRequestContext to Request * requestContext with explicit type
1 parent 5bf2198 commit 1a60bcb

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

router/src/main/kotlin/io/moia/router/Router.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.moia.router
22

33
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
4+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent.ProxyRequestContext
45
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
56

67
class Router {
@@ -78,6 +79,7 @@ data class Request<I>(val apiRequest: APIGatewayProxyRequestEvent, val body: I,
7879
val pathParameters by lazy { UriTemplate.from(pathPattern).extract(apiRequest.path) }
7980
val queryParameters: Map<String, String>? by lazy { apiRequest.queryStringParameters }
8081
val multiValueQueryStringParameters: Map<String, List<String>>? by lazy { apiRequest.multiValueQueryStringParameters }
82+
val requestContext: ProxyRequestContext by lazy { apiRequest.requestContext }
8183
fun getPathParameter(name: String): String = pathParameters[name] ?: error("Could not find path parameter '$name")
8284
fun getQueryParameter(name: String): String? = queryParameters?.get(name)
8385
fun getMultiValueQueryStringParameter(name: String): List<String>? = multiValueQueryStringParameters?.get(name)

router/src/test/kotlin/io/moia/router/RouterTest.kt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import assertk.assert
44
import assertk.assertions.hasSize
55
import assertk.assertions.isEmpty
66
import assertk.assertions.isEqualTo
7+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
78
import io.moia.router.Router.Companion.router
89
import org.assertj.core.api.BDDAssertions.then
910
import org.junit.jupiter.api.Assertions.assertTrue
@@ -104,4 +105,24 @@ class RouterTest {
104105
assert(consumes).isEqualTo(setOf<String>())
105106
}
106107
}
108+
109+
@Test
110+
fun `request should contain ProxyRequestContext`() {
111+
val claims = mapOf(
112+
"foobar" to "foo"
113+
)
114+
val context = APIGatewayProxyRequestEvent.ProxyRequestContext().apply {
115+
authorizer = mapOf("claims" to claims)
116+
}
117+
118+
val request = Request<Unit>(
119+
APIGatewayProxyRequestEvent()
120+
.withPath("/some-other")
121+
.withHttpMethod("GET")
122+
.withHeaders(mapOf("Accept" to "application/json"))
123+
.withRequestContext(context),
124+
Unit
125+
)
126+
assert(request.requestContext.authorizer!!["claims"]).isEqualTo(claims)
127+
}
107128
}

0 commit comments

Comments
 (0)