From 1a57f7c82552af9f9d2f375bf547c4b2b6c2551f Mon Sep 17 00:00:00 2001 From: Yannick Block Date: Mon, 29 Apr 2019 16:31:08 +0200 Subject: [PATCH 1/3] Add null safety check for protobuf deserializer --- .../kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt index 73af0469..83f05eb0 100644 --- a/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt +++ b/router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt @@ -14,7 +14,7 @@ class ProtoDeserializationHandler : DeserializationHandler { private val proto = MediaType.parse("application/x-protobuf") override fun supports(input: APIGatewayProxyRequestEvent): Boolean = - MediaType.parse(input.contentType()).`is`(proto) + input.contentType() != null && MediaType.parse(input.contentType()).`is`(proto) override fun deserialize(input: APIGatewayProxyRequestEvent, target: KType?): Any { val bytes = Base64.getDecoder().decode(input.body) From 90b27582f5c1e6be6272a0d697081ae996c89476 Mon Sep 17 00:00:00 2001 From: Yannick Block Date: Mon, 29 Apr 2019 18:08:31 +0200 Subject: [PATCH 2/3] Add ProtoDeserializationHandlerTest --- .../proto/ProtoDeserializationHandlerTest.kt | 20 +++++++++++++++++++ .../moia/router/proto/RequestHandlerTest.kt | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt diff --git a/router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt b/router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt new file mode 100644 index 00000000..e6c7a421 --- /dev/null +++ b/router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt @@ -0,0 +1,20 @@ +package io.moia.router.proto + +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent +import io.moia.router.withHeader +import org.junit.jupiter.api.Assertions.assertFalse +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test + +internal class ProtoDeserializationHandlerTest { + + @Test + fun `Deserializer should not support if the content type of the input is null`() { + assertFalse(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent())) + } + + @Test + fun `Deserializer should support if the content type of the input is protobuf`() { + assertTrue(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/x-protobuf"))) + } +} \ No newline at end of file diff --git a/router-protobuf/src/test/kotlin/io/moia/router/proto/RequestHandlerTest.kt b/router-protobuf/src/test/kotlin/io/moia/router/proto/RequestHandlerTest.kt index ea8713f5..45cf7d80 100644 --- a/router-protobuf/src/test/kotlin/io/moia/router/proto/RequestHandlerTest.kt +++ b/router-protobuf/src/test/kotlin/io/moia/router/proto/RequestHandlerTest.kt @@ -14,7 +14,7 @@ import java.util.Base64 class RequestHandlerTest { - val testRequestHandler = TestRequestHandler() + private val testRequestHandler = TestRequestHandler() @Test fun `should match request to proto handler and return json`() { From a9e309ab34c760bfe9be58087c0a6f48bb00c2ac Mon Sep 17 00:00:00 2001 From: Yannick Block Date: Mon, 29 Apr 2019 18:12:42 +0200 Subject: [PATCH 3/3] Improve ProtoDeserializationHandlerTest --- .../io/moia/router/proto/ProtoDeserializationHandlerTest.kt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt b/router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt index e6c7a421..d90bc7fa 100644 --- a/router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt +++ b/router-protobuf/src/test/kotlin/io/moia/router/proto/ProtoDeserializationHandlerTest.kt @@ -13,6 +13,11 @@ internal class ProtoDeserializationHandlerTest { assertFalse(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent())) } + @Test + fun `Deserializer should not support if the content type of the input is json`() { + assertFalse(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/json"))) + } + @Test fun `Deserializer should support if the content type of the input is protobuf`() { assertTrue(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/x-protobuf")))