Skip to content

Commit b765248

Browse files
should match accept all header (#23)
* should match accept all header * Improve readme
1 parent d5eb79b commit b765248

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

README.md

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ repositories {
3030
}
3131
3232
dependencies {
33-
implementation 'com.github.moia-dev.lambda-kotlin-request-router:router:0.5.0'
33+
implementation 'com.github.moia-dev.lambda-kotlin-request-router:router:0.8.2'
3434
}
3535
3636
```
@@ -99,13 +99,13 @@ override val router = router {
9999

100100
private fun loggingFilter() = Filter { next -> {
101101
request ->
102-
log.info("Handling request ${request.apiRequest.httpMethod} ${request.apiRequest.path}")
102+
log.info("Handling request ${request.httpMethod} ${request.path}")
103103
next(request) }
104104
}
105105

106106
private fun mdcFilter() = Filter { next -> {
107107
request ->
108-
MDC.put("requestId", request.apiRequest.requestContext?.requestId)
108+
MDC.put("requestId", request.requestContext?.requestId)
109109
next(request) }
110110
}
111111
}
@@ -161,6 +161,48 @@ So we do no validation of the JWT token.
161161

162162
### Protobuf support
163163

164+
The module `router-protobuf` helps to ease implementation of handlers that receive and return protobuf messages.
165+
166+
```
167+
implementation 'com.github.moia-dev.lambda-kotlin-request-router:router-protobuf:0.8.2'
168+
```
169+
170+
A handler implementation that wants to take advantage of the protobuf support should inherit from `ProtoEnabledRequestHandler`.
171+
172+
```kotlin
173+
class TestRequestHandler : ProtoEnabledRequestHandler() {
174+
175+
override val router = router {
176+
defaultProducing = setOf("application/x-protobuf")
177+
defaultConsuming = setOf("application/x-protobuf")
178+
179+
defaultContentType = "application/x-protobuf"
180+
181+
GET("/some-proto") { _: Request<Unit> -> ResponseEntity.ok(Sample.newBuilder().setHello("Hello").build()) }
182+
.producing("application/x-protobuf", "application/json")
183+
POST("/some-proto") { r: Request<Sample> -> ResponseEntity.ok(r.body) }
184+
GET<Unit, Unit>("/some-error") { _: Request<Unit> -> throw ApiException("boom", "BOOM", 400) }
185+
}
186+
187+
override fun createErrorBody(error: ApiError): Any =
188+
io.moia.router.proto.sample.SampleOuterClass.ApiError.newBuilder()
189+
.setMessage(error.message)
190+
.setCode(error.code)
191+
.build()
192+
193+
override fun createUnprocessableEntityErrorBody(errors: List<UnprocessableEntityError>): Any =
194+
errors.map { error ->
195+
io.moia.router.proto.sample.SampleOuterClass.UnprocessableEntityError.newBuilder()
196+
.setMessage(error.message)
197+
.setCode(error.code)
198+
.setPath(error.path)
199+
.build()
200+
}
201+
}
202+
```
203+
204+
Make sure you override `createErrorBody` and `createUnprocessableEntityErrorBody` to map error type to your proto error messages.
205+
164206

165207
### Open API validation support
166208

@@ -173,7 +215,7 @@ This library validates:
173215
- ...
174216

175217
```
176-
testImplementation 'com.github.moia-dev.lambda-kotlin-request-router:router-openapi-request-validator:0.5.0'
218+
testImplementation 'com.github.moia-dev.lambda-kotlin-request-router:router-openapi-request-validator:0.8.2'
177219
```
178220

179221
```kotlin

router/src/main/kotlin/io/moia/router/RequestPredicate.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ data class RequestPredicate(
4949
fun matchedAcceptType(acceptedMediaTypes: List<MediaType>) =
5050
produces
5151
.map { MediaType.parse(it) }
52-
.firstOrNull { acceptedMediaTypes.any { acceptedType -> acceptedType.`is`(it) } }
52+
.firstOrNull { acceptedMediaTypes.any { acceptedType -> it.`is`(acceptedType) } }
5353

5454
private fun acceptMatches(acceptedMediaTypes: List<MediaType>) =
5555
matchedAcceptType(acceptedMediaTypes) != null

router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class RequestHandlerTest {
5555
APIGatewayProxyRequestEvent()
5656
.withPath("/some")
5757
.withHttpMethod("GET")
58-
.withHeaders(mapOf("Accept" to "img/jpg")), mockk()
58+
.withHeaders(mapOf("Accept" to "image/jpg")), mockk()
5959
)
6060

6161
assert(response.statusCode).isEqualTo(406)
@@ -318,6 +318,24 @@ class RequestHandlerTest {
318318
assert(response.body).isEqualTo("""{"greeting":"some"}""")
319319
}
320320

321+
@Test
322+
fun `should handle request with accept all header`() {
323+
324+
val response = testRequestHandler.handleRequest(
325+
POST("/some")
326+
.withHeaders(mapOf(
327+
"Accept" to "*/*",
328+
"Content-Type" to "application/json"
329+
))
330+
.withBody("""{ "greeting": "some" }"""), mockk()
331+
)
332+
333+
assert(response.statusCode).isEqualTo(200)
334+
assert(response.getHeaderCaseInsensitive("content-type")).isEqualTo("application/json")
335+
336+
assert(response.body).isEqualTo("""{"greeting":"some"}""")
337+
}
338+
321339
@Test
322340
fun `should match request requiring permission`() {
323341

0 commit comments

Comments
 (0)