Skip to content

Commit 974a1a2

Browse files
committed
make default consumes and produces configurable
1 parent 10344ad commit 974a1a2

File tree

4 files changed

+51
-27
lines changed

4 files changed

+51
-27
lines changed

src/main/kotlin/com/github/mduesterhoeft/router/RequestHandler.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@ import com.amazonaws.services.lambda.runtime.Context
44
import com.amazonaws.services.lambda.runtime.RequestHandler
55
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
66
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
7-
import com.fasterxml.jackson.databind.ObjectMapper
87
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
98
import com.github.mduesterhoeft.router.ProtoBufUtils.toJsonWithoutWrappers
109
import com.google.common.net.MediaType
1110
import com.google.protobuf.GeneratedMessageV3
1211
import org.slf4j.Logger
1312
import org.slf4j.LoggerFactory
1413
import java.util.Base64
15-
import java.util.logging.LogManager
1614
import kotlin.reflect.KClass
1715
import kotlin.reflect.jvm.reflect
1816

src/main/kotlin/com/github/mduesterhoeft/router/RequestPredicate.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import com.google.common.net.MediaType
66
data class RequestPredicate(
77
val method: String,
88
val pathPattern: String,
9-
var produces: Set<String> = setOf("application/json", "application/x-protobuf"),
10-
var consumes: Set<String> = setOf("application/json", "application/x-protobuf")
9+
var produces: Set<String>,
10+
var consumes: Set<String>
1111
) {
1212

1313
fun consuming(vararg mediaTypes: String): RequestPredicate {

src/main/kotlin/com/github/mduesterhoeft/router/Router.kt

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,63 @@ class Router {
66

77
val routes = mutableListOf<RouterFunction<*, *>>()
88

9+
var defaultConsuming = setOf("application/json", "application/x-protobuf")
10+
var defaultProducing = setOf("application/json", "application/x-protobuf")
11+
912
fun <I, T> GET(pattern: String, handlerFunction: HandlerFunction<I, T>) =
1013
RequestPredicate(
1114
method = "GET",
1215
pathPattern = pattern,
13-
consumes = emptySet()
16+
consumes = emptySet(),
17+
produces = defaultProducing
1418
).also { routes += RouterFunction(it, handlerFunction) }
1519

1620
fun <I, T> POST(pattern: String, handlerFunction: HandlerFunction<I, T>) =
17-
RequestPredicate("POST", pattern).also {
21+
RequestPredicate(
22+
method = "POST",
23+
pathPattern = pattern,
24+
consumes = defaultConsuming,
25+
produces = defaultProducing
26+
).also {
1827
routes += RouterFunction(it, handlerFunction)
1928
}
2029

2130
fun <I, T> PUT(pattern: String, handlerFunction: HandlerFunction<I, T>) =
22-
RequestPredicate("PUT", pattern).also {
31+
RequestPredicate(
32+
method = "PUT",
33+
pathPattern = pattern,
34+
consumes = defaultConsuming,
35+
produces = defaultProducing
36+
).also {
2337
routes += RouterFunction(it, handlerFunction)
2438
}
2539

2640
fun <I, T> DELETE(pattern: String, handlerFunction: HandlerFunction<I, T>) =
27-
RequestPredicate("DELETE", pattern).also {
41+
RequestPredicate(
42+
method = "DELETE",
43+
pathPattern = pattern,
44+
consumes = defaultConsuming,
45+
produces = defaultProducing
46+
).also {
2847
routes += RouterFunction(it, handlerFunction)
2948
}
3049

50+
fun <I, T> PATCH(pattern: String, handlerFunction: HandlerFunction<I, T>) =
51+
RequestPredicate(
52+
method = "PATCH",
53+
pathPattern = pattern,
54+
consumes = defaultConsuming,
55+
produces = defaultProducing
56+
).also {
57+
routes += RouterFunction(it, handlerFunction)
58+
}
59+
60+
// the default content types the HandlerFunctions of this router can produce
61+
fun defaultProducing(contentTypes: Set<String>): Router = this.also { defaultProducing = contentTypes }
62+
63+
// the default content types the HandlerFunctions of this router can handle
64+
fun defaultConsuming(contentTypes: Set<String>): Router = this.also { defaultConsuming = contentTypes }
65+
3166
companion object {
3267
fun router(routes: Router.() -> Unit) = Router().apply(routes)
3368
}

src/test/kotlin/com/github/mduesterhoeft/router/RequestHandlerTest.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import com.github.mduesterhoeft.router.Router.Companion.router
77
import com.github.mduesterhoeft.router.sample.proto.SampleOuterClass.Sample
88
import io.mockk.mockk
99
import org.junit.jupiter.api.Test
10-
import java.util.Base64
1110

1211
class RequestHandlerTest {
1312

@@ -20,8 +19,7 @@ class RequestHandlerTest {
2019
APIGatewayProxyRequestEvent()
2120
.withPath("/some")
2221
.withHttpMethod("GET")
23-
.withHeaders(mapOf("Accept" to "application/json"))
24-
, mockk()
22+
.withHeaders(mapOf("Accept" to "application/json")), mockk()
2523
)!!
2624

2725
assert(response.statusCode).isEqualTo(200)
@@ -35,8 +33,7 @@ class RequestHandlerTest {
3533
APIGatewayProxyRequestEvent()
3634
.withPath("/some-proto")
3735
.withHttpMethod("GET")
38-
.withHeaders(mapOf("Accept" to "application/json"))
39-
, mockk()
36+
.withHeaders(mapOf("Accept" to "application/json")), mockk()
4037
)!!
4138

4239
assert(response.statusCode).isEqualTo(200)
@@ -50,8 +47,7 @@ class RequestHandlerTest {
5047
APIGatewayProxyRequestEvent()
5148
.withPath("/some-proto")
5249
.withHttpMethod("GET")
53-
.withHeaders(mapOf("Accept" to "application/x-protobuf"))
54-
, mockk()
50+
.withHeaders(mapOf("Accept" to "application/x-protobuf")), mockk()
5551
)!!
5652

5753
assert(response.statusCode).isEqualTo(200)
@@ -65,8 +61,7 @@ class RequestHandlerTest {
6561
APIGatewayProxyRequestEvent()
6662
.withPath("/some")
6763
.withHttpMethod("GET")
68-
.withHeaders(mapOf("Accept" to "img/jpg"))
69-
, mockk()
64+
.withHeaders(mapOf("Accept" to "img/jpg")), mockk()
7065
)!!
7166

7267
assert(response.statusCode).isEqualTo(406)
@@ -82,8 +77,7 @@ class RequestHandlerTest {
8277
.withHeaders(mapOf(
8378
"Accept" to "application/json",
8479
"Content-Type" to "image/jpg"
85-
))
86-
, mockk()
80+
)), mockk()
8781
)!!
8882

8983
assert(response.statusCode).isEqualTo(415)
@@ -100,8 +94,7 @@ class RequestHandlerTest {
10094
"Accept" to "application/json",
10195
"Content-Type" to "application/json"
10296
))
103-
.withBody("""{ "greeting": "some" }""")
104-
, mockk()
97+
.withBody("""{ "greeting": "some" }"""), mockk()
10598
)!!
10699

107100
assert(response.statusCode).isEqualTo(200)
@@ -118,8 +111,7 @@ class RequestHandlerTest {
118111
.withHeaders(mapOf(
119112
"Accept" to "application/json",
120113
"Content-Type" to "image/jpg"
121-
))
122-
, mockk()
114+
)), mockk()
123115
)!!
124116

125117
assert(response.statusCode).isEqualTo(405)
@@ -132,8 +124,7 @@ class RequestHandlerTest {
132124
APIGatewayProxyRequestEvent()
133125
.withPath("/some-other")
134126
.withHttpMethod("GET")
135-
.withHeaders(mapOf("Accept" to "application/json"))
136-
, mockk()
127+
.withHeaders(mapOf("Accept" to "application/json")), mockk()
137128
)!!
138129

139130
assert(response.statusCode).isEqualTo(404)
@@ -145,10 +136,10 @@ class RequestHandlerTest {
145136
data class TestRequest(val greeting: String)
146137

147138
override val router = router {
148-
GET("/some") { request: Request<Unit> ->
139+
GET("/some") { _: Request<Unit> ->
149140
ResponseEntity.ok(TestResponse("Hello"))
150141
}
151-
GET("/some-proto") { request: Request<Unit> ->
142+
GET("/some-proto") { _: Request<Unit> ->
152143
ResponseEntity.ok(Sample.newBuilder().setHello("Hello").build())
153144
}
154145
POST("/some") { r: Request<TestRequest> ->

0 commit comments

Comments
 (0)