Skip to content

Commit 9aff946

Browse files
authored
KTOR-8232 Make OutgoingContent headers available in the StatusPages handlers (#4980)
1 parent 6f9d62f commit 9aff946

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

ktor-server/ktor-server-plugins/ktor-server-status-pages/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ kotlin {
1212
sourceSets {
1313
commonTest.dependencies {
1414
implementation(projects.ktorServerCallId)
15+
implementation(projects.ktorServerAuth)
1516
}
1617
}
1718
}

ktor-server/ktor-server-plugins/ktor-server-status-pages/common/src/io/ktor/server/plugins/statuspages/StatusPages.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ public val StatusPages: ApplicationPlugin<StatusPagesConfig> = createApplication
6868
return@on
6969
}
7070

71+
content.headers.entries().forEach { (key, values) ->
72+
values.forEach { value ->
73+
call.response.headers.append(key, value)
74+
}
75+
}
76+
7177
call.attributes.put(statusPageMarker, Unit)
7278
try {
7379
LOGGER.trace("Executing $handler for status code $status for call: ${call.request.uri}")

ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import io.ktor.client.statement.*
99
import io.ktor.http.*
1010
import io.ktor.http.content.*
1111
import io.ktor.server.application.*
12+
import io.ktor.server.auth.*
1213
import io.ktor.server.plugins.*
1314
import io.ktor.server.plugins.callid.*
1415
import io.ktor.server.request.*
@@ -567,4 +568,60 @@ class StatusPagesTest {
567568
assertEquals("Custom-Value", responseWithRoute.headers["Custom-Header"])
568569
assertEquals("body", responseWithRoute.bodyAsText())
569570
}
571+
572+
@Test
573+
fun testHeadersOfOutgoingContent() = testApplication {
574+
install(StatusPages) {
575+
status(HttpStatusCode.Unauthorized) { call, _ ->
576+
if (call.response.headers[HttpHeaders.WWWAuthenticate] == "Basic realm=myRealm, charset=UTF-8") {
577+
call.respond(HttpStatusCode.OK)
578+
} else {
579+
call.respond(HttpStatusCode.InternalServerError)
580+
}
581+
}
582+
status(HttpStatusCode.OK) { call, _ ->
583+
assertEquals("Custom-Value-Response", call.response.headers["Custom-Header-Response"])
584+
assertEquals("Custom-Value-Content", call.response.headers["Custom-Header-Content"])
585+
}
586+
}
587+
588+
install(Authentication) {
589+
basic {
590+
realm = "myRealm"
591+
validate {
592+
null
593+
}
594+
}
595+
}
596+
597+
routing {
598+
authenticate {
599+
get("/auth") {
600+
throw IllegalStateException("This should not be called")
601+
}
602+
}
603+
604+
get("/custom-headers") {
605+
call.response.headers.append("Custom-Header-Response", "Custom-Value-Response")
606+
call.respond(object : OutgoingContent.ReadChannelContent() {
607+
override val status: HttpStatusCode
608+
get() = HttpStatusCode.OK
609+
override fun readFrom() = ByteReadChannel("hello world")
610+
override val headers: Headers
611+
get() = Headers.build {
612+
append("Custom-Header-Content", "Custom-Value-Content")
613+
}
614+
})
615+
}
616+
}
617+
618+
assertEquals(HttpStatusCode.OK, client.get("/auth").status)
619+
620+
client.get("/custom-headers").apply {
621+
assertEquals(HttpStatusCode.OK, status)
622+
assertEquals("hello world", bodyAsText())
623+
assertEquals("Custom-Value-Content", headers["Custom-Header-Content"])
624+
assertEquals("Custom-Value-Response", headers["Custom-Header-Response"])
625+
}
626+
}
570627
}

0 commit comments

Comments
 (0)