Skip to content

Commit c26a8dd

Browse files
blockvotemduesterhoeft
authored andcommitted
Add null safety check for protobuf deserializer (#8)
* Add null safety check for protobuf deserializer * Add ProtoDeserializationHandlerTest * Improve ProtoDeserializationHandlerTest
1 parent 4698808 commit c26a8dd

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

router-protobuf/src/main/kotlin/io/moia/router/proto/ProtoDeserializationHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ProtoDeserializationHandler : DeserializationHandler {
1414
private val proto = MediaType.parse("application/x-protobuf")
1515

1616
override fun supports(input: APIGatewayProxyRequestEvent): Boolean =
17-
MediaType.parse(input.contentType()).`is`(proto)
17+
input.contentType() != null && MediaType.parse(input.contentType()).`is`(proto)
1818

1919
override fun deserialize(input: APIGatewayProxyRequestEvent, target: KType?): Any {
2020
val bytes = Base64.getDecoder().decode(input.body)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.moia.router.proto
2+
3+
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
4+
import io.moia.router.withHeader
5+
import org.junit.jupiter.api.Assertions.assertFalse
6+
import org.junit.jupiter.api.Assertions.assertTrue
7+
import org.junit.jupiter.api.Test
8+
9+
internal class ProtoDeserializationHandlerTest {
10+
11+
@Test
12+
fun `Deserializer should not support if the content type of the input is null`() {
13+
assertFalse(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent()))
14+
}
15+
16+
@Test
17+
fun `Deserializer should not support if the content type of the input is json`() {
18+
assertFalse(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/json")))
19+
}
20+
21+
@Test
22+
fun `Deserializer should support if the content type of the input is protobuf`() {
23+
assertTrue(ProtoDeserializationHandler().supports(APIGatewayProxyRequestEvent().withHeader("content-type", "application/x-protobuf")))
24+
}
25+
}

router-protobuf/src/test/kotlin/io/moia/router/proto/RequestHandlerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import java.util.Base64
1818

1919
class RequestHandlerTest {
2020

21-
val testRequestHandler = TestRequestHandler()
21+
private val testRequestHandler = TestRequestHandler()
2222

2323
@Test
2424
fun `should match request to proto handler and return json`() {

0 commit comments

Comments
 (0)