Skip to content

Commit

Permalink
KTOR-6671 Fix Resources: a / route isn't resolved when there is a sib…
Browse files Browse the repository at this point in the history
…ling
  • Loading branch information
e5l committed Feb 12, 2025
1 parent ba3aaa7 commit 99c22c1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -607,16 +607,21 @@ private fun Route.staticContentRoute(
remotePath: String,
autoHead: Boolean,
handler: suspend (ApplicationCall).() -> Unit
) = route(remotePath) {
route("{$pathParameterName...}") {
get {
call.handler()
}
if (autoHead) {
method(HttpMethod.Head) {
install(StaticContentAutoHead)
handle {
call.handler()
) = createChild(object : RouteSelector() {
override suspend fun evaluate(context: RoutingResolveContext, segmentIndex: Int): RouteSelectorEvaluation =
RouteSelectorEvaluation.Success(quality = RouteSelectorEvaluation.qualityTailcard)
}).apply {
route(remotePath) {
route("{$pathParameterName...}") {
get {
call.handler()
}
if (autoHead) {
method(HttpMethod.Head) {
install(StaticContentAutoHead)
handle {
call.handler()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,25 @@

package io.ktor.tests.resources

import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import io.ktor.resources.*
import io.ktor.server.http.content.*
import io.ktor.server.resources.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import java.math.*
import kotlin.test.*
import kotlinx.serialization.KSerializer
import kotlinx.serialization.Serializable
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.descriptors.SerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.math.BigDecimal
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class ResourcesTestJvm {

Expand Down Expand Up @@ -57,4 +67,25 @@ class ResourcesTestJvm {
"/?bd=123456789012345678901234567890&bi=123456789012345678901234567890"
)
}

@Resource("/")
class Home

@Test
fun testHomeResourceWithStaticResource() = testResourcesApplication {
var executed = false
routing {
get<Home> {
executed = true
call.respondText("OK")
}
staticResources("/", "static")
}

client.get("/").let { response ->
assertTrue(executed)
assertEquals(HttpStatusCode.OK, response.status)
assertEquals("OK", response.bodyAsText())
}
}
}

0 comments on commit 99c22c1

Please sign in to comment.