Skip to content

Commit

Permalink
setter kun error body with det ikke er body fra før
Browse files Browse the repository at this point in the history
  • Loading branch information
davidsteinsland committed Nov 27, 2024
1 parent 2764397 commit cba36a8
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
import io.ktor.events.*
import io.ktor.http.*
import io.ktor.http.HttpStatusCode.Companion.allStatusCodes
import io.ktor.http.content.OutgoingContent
import io.ktor.serialization.jackson.*
import io.ktor.server.application.*
import io.ktor.server.cio.*
Expand Down Expand Up @@ -314,16 +315,26 @@ fun StatusPagesConfig.defaultStatusPagesConfig() {
))
}
status(*allStatusCodes.filterNot { code -> code.isSuccess() }.toTypedArray()) { statusCode ->
if (content.contentLength == null) {
call.response.header("Content-Type", ContentType.Application.ProblemJson.toString())
call.respond(statusCode, FeilResponse(
status = statusCode,
type = statusCode.toURI(call),
detail = statusCode.description,
instance = URI(call.request.uri),
callId = call.callId,
stacktrace = null
))
/* exhaustive when-block so it will be compiler error if new types are added */
when (content) {
is OutgoingContent.NoContent -> {
call.response.header("Content-Type", ContentType.Application.ProblemJson.toString())
call.respond(statusCode, FeilResponse(
status = statusCode,
type = statusCode.toURI(call),
detail = statusCode.description,
instance = URI(call.request.uri),
callId = call.callId,
stacktrace = null
))
}
is OutgoingContent.ByteArrayContent,
is OutgoingContent.ContentWrapper,
is OutgoingContent.ProtocolUpgrade,
is OutgoingContent.ReadChannelContent,
is OutgoingContent.WriteChannelContent -> {
/* do nothing */
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,47 @@ class NaisfulAppTest {
}

@Test
fun `method not allowed`() {
fun `default error body`() {
testApp(
applicationModule = {
routing {
get("/test") {
get("/test1") {
call.respond(HttpStatusCode.OK)
}
get("/test2") {
call.respond(HttpStatusCode.Conflict, "This is a body")
}
get("/test3") {
call.respond(HttpStatusCode.Forbidden, CustomResponse("Fault"))
}
}
}
) {
val response = post("/test")
val body = response.body<FeilResponse>()
assertEquals(ContentType.Application.ProblemJson, response.contentType())
assertEquals(URI("urn:error:method_not_allowed"), body.type)
assertEquals(HttpStatusCode.MethodNotAllowed.description, body.title)
assertEquals(HttpStatusCode.MethodNotAllowed.value, body.status)
assertEquals(URI("/test"), body.instance)
assertEquals("Method Not Allowed", body.detail)
post("/test1").also { response ->
val body = response.body<FeilResponse>()
assertEquals(HttpStatusCode.MethodNotAllowed, response.status)
assertEquals(ContentType.Application.ProblemJson, response.contentType())
assertEquals(URI("urn:error:method_not_allowed"), body.type)
assertEquals(HttpStatusCode.MethodNotAllowed.description, body.title)
assertEquals(HttpStatusCode.MethodNotAllowed.value, body.status)
assertEquals(URI("/test1"), body.instance)
assertEquals("Method Not Allowed", body.detail)
}
get("/test2").also { response ->
assertEquals(HttpStatusCode.Conflict, response.status)
assertEquals(ContentType.Text.Plain.withCharset(Charsets.UTF_8), response.contentType())
assertEquals("This is a body", response.bodyAsText())
}
get("/test3").also { response ->
assertEquals(HttpStatusCode.Forbidden, response.status)
assertEquals(ContentType.Application.Json.withCharset(Charsets.UTF_8), response.contentType())
assertEquals("Fault", response.body<CustomResponse>().text)
}
}
}

private data class CustomResponse(val text: String)

private suspend fun assertUntil(maxWait: Duration, assertion: suspend () -> Unit) {
val startTime = System.currentTimeMillis()
lateinit var lastAssertionError: AssertionError
Expand Down

0 comments on commit cba36a8

Please sign in to comment.