From fff96010636d99ddb14658ea12fb4c13428e2c71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20D=C3=BCsterh=C3=B6ft?= Date: Thu, 13 Jun 2019 14:35:46 +0200 Subject: [PATCH 1/2] Return not acceptable when failing to parse media type --- .../moia/router/APIGatewayProxyEventExtensions.kt | 9 ++++++++- .../kotlin/io/moia/router/RequestHandlerTest.kt | 15 +++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/router/src/main/kotlin/io/moia/router/APIGatewayProxyEventExtensions.kt b/router/src/main/kotlin/io/moia/router/APIGatewayProxyEventExtensions.kt index ed3da188..3b907c3b 100644 --- a/router/src/main/kotlin/io/moia/router/APIGatewayProxyEventExtensions.kt +++ b/router/src/main/kotlin/io/moia/router/APIGatewayProxyEventExtensions.kt @@ -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") @@ -68,3 +68,10 @@ private fun getCaseInsensitive(key: String, map: Map?): String? ?.value fun APIGatewayProxyResponseEvent.bodyAsBytes() = Base64.getDecoder().decode(body) + +private fun parseMediaTypeSafe(input: String): MediaType? = + try { + MediaType.parse(input) + } catch (e: IllegalArgumentException) { + null + } diff --git a/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt b/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt index 9a8a39c0..197205fc 100644 --- a/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt +++ b/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt @@ -336,6 +336,21 @@ class RequestHandlerTest { assert(response.body).isEqualTo("""{"greeting":"some"}""") } + @Test + fun `should fail with not-acceptable on non-parseable 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`() { From 8f0d460b884592b03fe947a452ed2140159e169d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mathias=20D=C3=BCsterh=C3=B6ft?= Date: Thu, 13 Jun 2019 15:22:56 +0200 Subject: [PATCH 2/2] Update router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt Co-Authored-By: Nihal Gonsalves --- router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt b/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt index 197205fc..694100e3 100644 --- a/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt +++ b/router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt @@ -337,7 +337,7 @@ class RequestHandlerTest { } @Test - fun `should fail with not-acceptable on non-parseable media type`() { + fun `should fail with 406 Not Acceptable on an unparsable media type`() { val response = testRequestHandler.handleRequest( POST("/some")