Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fun APIGatewayProxyRequestEvent.acceptHeader() = getHeaderCaseInsensitive("accep
fun APIGatewayProxyRequestEvent.acceptedMediaTypes() = acceptHeader()
?.split(",")
?.map { it.trim() }
?.map { MediaType.parse(it) }
?.mapNotNull { parseMediaTypeSafe(it) }
.orEmpty()
fun APIGatewayProxyRequestEvent.contentType() = getHeaderCaseInsensitive("content-type")

Expand Down Expand Up @@ -68,3 +68,10 @@ private fun getCaseInsensitive(key: String, map: Map<String, String>?): String?
?.value

fun APIGatewayProxyResponseEvent.bodyAsBytes() = Base64.getDecoder().decode(body)

private fun parseMediaTypeSafe(input: String): MediaType? =
try {
MediaType.parse(input)
} catch (e: IllegalArgumentException) {
null
}
15 changes: 15 additions & 0 deletions router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,21 @@ class RequestHandlerTest {
assert(response.body).isEqualTo("""{"greeting":"some"}""")
}

@Test
fun `should fail with 406 Not Acceptable on an unparsable media type`() {

val response = testRequestHandler.handleRequest(
POST("/some")
.withHeaders(mapOf(
"Accept" to "*",
"Content-Type" to "application/json"
))
.withBody("""{ "greeting": "some" }"""), mockk()
)

assert(response.statusCode).isEqualTo(406)
}

@Test
fun `should match request requiring permission`() {

Expand Down