Skip to content

Commit ed04c31

Browse files
blockvotemduesterhoeft
authored andcommitted
Add null safety check for missing request path (#4)
1 parent 2cab791 commit ed04c31

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ data class RequestPredicate(
3838
matchContentType = contentTypeMatches(request.contentType(), consumes)
3939
)
4040

41-
private fun pathMatches(request: APIGatewayProxyRequestEvent) = UriTemplate.from(
42-
pathPattern
43-
).matches(request.path)
41+
private fun pathMatches(request: APIGatewayProxyRequestEvent) =
42+
request.path?.let { UriTemplate.from(pathPattern).matches(it) } ?: false
4443
private fun methodMatches(request: APIGatewayProxyRequestEvent) = method.equals(request.httpMethod, true)
4544
private fun contentTypeMatches(contentType: String?, accepted: Set<String>) =
4645
if (accepted.isEmpty() && contentType == null) true

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,24 @@ class RequestHandlerTest {
244244
assert(response.statusCode).isEqualTo(401)
245245
}
246246

247+
@Test
248+
fun `Request without headers should return status code 406`() {
249+
val response = testRequestHandler.handleRequest(
250+
GET("/some"),
251+
mockk()
252+
)
253+
assert(response.statusCode).isEqualTo(406)
254+
}
255+
256+
@Test
257+
fun `Request without request path should return status code 404`() {
258+
val response = testRequestHandler.handleRequest(
259+
GET(),
260+
mockk()
261+
)
262+
assert(response.statusCode).isEqualTo(404)
263+
}
264+
247265
class TestRequestHandlerAuthorization : RequestHandler() {
248266
override val router = router {
249267
GET("/some") { _: Request<Unit> ->

0 commit comments

Comments
 (0)