Skip to content

Commit b1cbf4c

Browse files
blockvotemduesterhoeft
authored andcommitted
Make response location optional (#11)
* Make response location optional * Format kotlin
1 parent d54113b commit b1cbf4c

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ data class ResponseEntity<T>(
1111
fun <T> ok(body: T? = null, headers: Map<String, String> = emptyMap()) =
1212
ResponseEntity<T>(200, body, headers)
1313

14-
fun <T> created(body: T? = null, location: URI, headers: Map<String, String> = emptyMap()) =
15-
ResponseEntity<T>(201, body, headers + ("location" to location.toString()))
14+
fun <T> created(body: T? = null, location: URI? = null, headers: Map<String, String> = emptyMap()) =
15+
ResponseEntity<T>(201, body, if (location == null) headers else headers + ("location" to location.toString()))
1616

1717
fun noContent(headers: Map<String, String> = emptyMap()) =
1818
ResponseEntity<Unit>(204, null, headers)

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

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package io.moia.router
22

33
import assertk.assert
44
import assertk.assertions.isEqualTo
5+
import assertk.assertions.isFalse
56
import assertk.assertions.isNullOrEmpty
7+
import assertk.assertions.isTrue
68
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
79
import io.mockk.mockk
810
import io.moia.router.Router.Companion.router
@@ -278,6 +280,33 @@ class RequestHandlerTest {
278280
assert(response.body).isNullOrEmpty()
279281
}
280282

283+
@Test
284+
fun `Create should not return a location header`() {
285+
val response = testRequestHandler.handleRequest(
286+
POST("/create-without-location")
287+
.withHeader("Accept", "application/json")
288+
.withHeader("Content-Type", "application/json")
289+
.withBody("""{ "greeting": "some" }"""),
290+
mockk()
291+
)
292+
assert(response.statusCode).isEqualTo(201)
293+
assert(response.headers.containsKey("location")).isFalse()
294+
}
295+
296+
@Test
297+
fun `Create should return a location header`() {
298+
val response = testRequestHandler.handleRequest(
299+
POST("/create-with-location")
300+
.withHeader("Accept", "application/json")
301+
.withHeader("Content-Type", "application/json")
302+
.withBody("""{ "greeting": "some" }"""),
303+
mockk()
304+
)
305+
assert(response.statusCode).isEqualTo(201)
306+
assert(response.headers.containsKey("location")).isTrue()
307+
assert(response.headers["location"]).isEqualTo("http://localhost/test")
308+
}
309+
281310
class TestRequestHandlerAuthorization : RequestHandler() {
282311
override val router = router {
283312
GET("/some") { _: Request<Unit> ->
@@ -372,6 +401,12 @@ class RequestHandlerTest {
372401
POST("/no-content") { _: Request<TestRequest> ->
373402
ResponseEntity.noContent()
374403
}
404+
POST("/create-without-location") { _: Request<TestRequest> ->
405+
ResponseEntity.created(null, null, emptyMap())
406+
}
407+
POST("/create-with-location") { r: Request<TestRequest> ->
408+
ResponseEntity.created(null, r.apiRequest.location("test"), emptyMap())
409+
}
375410
}
376411
}
377412
}

0 commit comments

Comments
 (0)