Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions router/src/main/kotlin/io/moia/router/ResponseEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ data class ResponseEntity<T>(
fun <T> ok(body: T? = null, headers: Map<String, String> = emptyMap()) =
ResponseEntity<T>(200, body, headers)

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

fun noContent(headers: Map<String, String> = emptyMap()) =
ResponseEntity<Unit>(204, null, headers)
Expand Down
35 changes: 35 additions & 0 deletions router/src/test/kotlin/io/moia/router/RequestHandlerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package io.moia.router

import assertk.assert
import assertk.assertions.isEqualTo
import assertk.assertions.isFalse
import assertk.assertions.isNullOrEmpty
import assertk.assertions.isTrue
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent
import io.mockk.mockk
import io.moia.router.Router.Companion.router
Expand Down Expand Up @@ -278,6 +280,33 @@ class RequestHandlerTest {
assert(response.body).isNullOrEmpty()
}

@Test
fun `Create should not return a location header`() {
val response = testRequestHandler.handleRequest(
POST("/create-without-location")
.withHeader("Accept", "application/json")
.withHeader("Content-Type", "application/json")
.withBody("""{ "greeting": "some" }"""),
mockk()
)
assert(response.statusCode).isEqualTo(201)
assert(response.headers.containsKey("location")).isFalse()
}

@Test
fun `Create should return a location header`() {
val response = testRequestHandler.handleRequest(
POST("/create-with-location")
.withHeader("Accept", "application/json")
.withHeader("Content-Type", "application/json")
.withBody("""{ "greeting": "some" }"""),
mockk()
)
assert(response.statusCode).isEqualTo(201)
assert(response.headers.containsKey("location")).isTrue()
assert(response.headers["location"]).isEqualTo("http://localhost/test")
}

class TestRequestHandlerAuthorization : RequestHandler() {
override val router = router {
GET("/some") { _: Request<Unit> ->
Expand Down Expand Up @@ -372,6 +401,12 @@ class RequestHandlerTest {
POST("/no-content") { _: Request<TestRequest> ->
ResponseEntity.noContent()
}
POST("/create-without-location") { _: Request<TestRequest> ->
ResponseEntity.created(null, null, emptyMap())
}
POST("/create-with-location") { r: Request<TestRequest> ->
ResponseEntity.created(null, r.apiRequest.location("test"), emptyMap())
}
}
}
}