diff --git a/ktor-http/common/src/io/ktor/http/ContentTypes.kt b/ktor-http/common/src/io/ktor/http/ContentTypes.kt index 92d72ed8122..c5944e9e16e 100644 --- a/ktor-http/common/src/io/ktor/http/ContentTypes.kt +++ b/ktor-http/common/src/io/ktor/http/ContentTypes.kt @@ -56,7 +56,8 @@ public class ContentType private constructor( * Checks if `this` type matches a [pattern] type taking into account placeholder symbols `*` and parameters. */ public fun match(pattern: ContentType): Boolean { - if (pattern.contentType != "*" && !pattern.contentType.equals(contentType, ignoreCase = true)) { + if (pattern.contentType != "*" && contentType != "*" + && !pattern.contentType.equals(contentType, ignoreCase = true)) { return false } diff --git a/ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/routing/RoutingProcessingTest.kt b/ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/routing/RoutingProcessingTest.kt index 46d3168a78a..26bded979bf 100644 --- a/ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/routing/RoutingProcessingTest.kt +++ b/ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/routing/RoutingProcessingTest.kt @@ -193,24 +193,63 @@ class RoutingProcessingTest { @Test fun testRoutingAcceptContentSubtypeWildcard() = testApplication { - routing { - route("getImage") { + route("image") { accept(ContentType.Image.Any) { get { call.respondText { "Image" } } } } + route("any") { + accept(ContentType.Any) { + get { call.respondText { "Any" } } + } + } } - on("making request to /getImage with Accept image/png") { - client.get("/getImage") { - header(HttpHeaders.Accept, "image/png") + on("making request to /image with Accept image/png") { + client.get("/image") { + header(HttpHeaders.Accept, ContentType.Image.PNG) + }.let { + assertEquals(HttpStatusCode.OK, it.status) + assertEquals("Image", it.bodyAsText()) } - .let { + } + + on("making request to /image with Accept */*") { + client.get("/image") { + header(HttpHeaders.Accept, ContentType.Any) + }.let { assertEquals(HttpStatusCode.OK, it.status) assertEquals("Image", it.bodyAsText()) } } + + on("making request to /any with Accept image/png") { + client.get("/any") { + header(HttpHeaders.Accept, ContentType.Image.PNG) + }.let { + assertEquals(HttpStatusCode.OK, it.status) + assertEquals("Any", it.bodyAsText()) + } + } + + on("making request to /any with Accept image/*") { + client.get("/any") { + header(HttpHeaders.Accept, ContentType.Image.Any) + }.let { + assertEquals(HttpStatusCode.OK, it.status) + assertEquals("Any", it.bodyAsText()) + } + } + + on("making request to /any with Accept */*") { + client.get("/any") { + header(HttpHeaders.Accept, ContentType.Any) + }.let { + assertEquals(HttpStatusCode.OK, it.status) + assertEquals("Any", it.bodyAsText()) + } + } } @Test