Skip to content

Commit 81240dc

Browse files
blockvotemduesterhoeft
authored andcommitted
Ignore content-type for deletion requests (#12)
* Ignore content-type for deletion requests Co-authored-by: Nihal Gonsalves <nihal@nihalgonsalves.com> * Format kotlin
1 parent 5a7729b commit 81240dc

File tree

3 files changed

+36
-9
lines changed

3 files changed

+36
-9
lines changed

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

+9-9
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ class Router {
1414
var filter: Filter = Filter.NoOp
1515

1616
fun <I, T> GET(pattern: String, handlerFunction: HandlerFunction<I, T>) =
17-
RequestPredicate(
18-
method = "GET",
19-
pathPattern = pattern,
20-
consumes = emptySet(),
21-
produces = defaultProducing
22-
).also { routes += RouterFunction(it, handlerFunction) }
17+
defaultRequestPredicate(pattern, "GET", handlerFunction, emptySet())
2318

2419
fun <I, T> POST(pattern: String, handlerFunction: HandlerFunction<I, T>) =
2520
defaultRequestPredicate(pattern, "POST", handlerFunction)
@@ -28,16 +23,21 @@ class Router {
2823
defaultRequestPredicate(pattern, "PUT", handlerFunction)
2924

3025
fun <I, T> DELETE(pattern: String, handlerFunction: HandlerFunction<I, T>) =
31-
defaultRequestPredicate(pattern, "DELETE", handlerFunction)
26+
defaultRequestPredicate(pattern, "DELETE", handlerFunction, emptySet())
3227

3328
fun <I, T> PATCH(pattern: String, handlerFunction: HandlerFunction<I, T>) =
3429
defaultRequestPredicate(pattern, "PATCH", handlerFunction)
3530

36-
private fun <I, T> defaultRequestPredicate(pattern: String, method: String, handlerFunction: HandlerFunction<I, T>) =
31+
private fun <I, T> defaultRequestPredicate(
32+
pattern: String,
33+
method: String,
34+
handlerFunction: HandlerFunction<I, T>,
35+
consuming: Set<String> = defaultConsuming
36+
) =
3737
RequestPredicate(
3838
method = method,
3939
pathPattern = pattern,
40-
consumes = defaultConsuming,
40+
consumes = consuming,
4141
produces = defaultProducing
4242
).also { routes += RouterFunction(it, handlerFunction) }
4343

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

+15
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,18 @@ class RequestHandlerTest {
324324
assert(response.headers["location"]).isEqualTo("http://localhost/test")
325325
}
326326

327+
@Test
328+
fun `Deletion should ignore the body and content-type`() {
329+
val response = testRequestHandler.handleRequest(
330+
DELETE("/delete-me")
331+
.withHeader("Accept", "application/json")
332+
.withHeader("Content-Type", "text/csv")
333+
.withBody("this may be faulty"),
334+
mockk()
335+
)
336+
assert(response.statusCode).isEqualTo(204)
337+
}
338+
327339
class TestRequestHandlerAuthorization : RequestHandler() {
328340
override val router = router {
329341
GET("/some") { _: Request<Unit> ->
@@ -424,6 +436,9 @@ class RequestHandlerTest {
424436
POST("/create-with-location") { r: Request<TestRequest> ->
425437
ResponseEntity.created(null, r.apiRequest.location("test"), emptyMap())
426438
}
439+
DELETE("/delete-me") { _: Request<Unit> ->
440+
ResponseEntity.noContent()
441+
}
427442
}
428443
}
429444
}

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

+12
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,16 @@ class RouterTest {
9292
assertTrue(UriTemplate.from(pathPattern).matches("/some/sub/sub/sub/path"))
9393
}
9494
}
95+
96+
@Test
97+
fun `should not consume for a deletion route`() {
98+
val router = router {
99+
DELETE("/delete-me") { r: Request<Unit> ->
100+
ResponseEntity.ok(null)
101+
}
102+
}
103+
with(router.routes.first().requestPredicate) {
104+
assert(consumes).isEqualTo(setOf<String>())
105+
}
106+
}
95107
}

0 commit comments

Comments
 (0)