Skip to content

Commit

Permalink
KTOR-6684 Fix duplicate Accept Header in ContentNegotiation (#3984)
Browse files Browse the repository at this point in the history
  • Loading branch information
marychatte authored and bjhham committed Mar 1, 2024
1 parent 4633438 commit 5fd337e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ public class ContentNegotiation internal constructor(
internal suspend fun convertRequest(request: HttpRequestBuilder, body: Any): Any? {
registrations.forEach {
LOGGER.trace("Adding Accept=${it.contentTypeToSend.contentType} header for ${request.url}")

if (request.headers.contains(HttpHeaders.Accept, it.contentTypeToSend.toString())) return@forEach
request.accept(it.contentTypeToSend)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,25 @@ abstract class JsonContentNegotiationTest(val converter: ContentConverter) {
}
}

@Test
fun testNoDuplicatedHeaders() = testApplication {
install(ContentNegotiation) {
register(ContentType.Application.Json, converter)
}

createClient {
install(io.ktor.client.plugins.contentnegotiation.ContentNegotiation) {
register(ContentType.Application.Json, converter)
}
}.get {
header(HttpHeaders.Accept, "application/json")
}.let { response ->
response.request.headers.forEach { _, values ->
assertEquals(1, values.size)
}
}
}

@Test
open fun testRespondNestedSealedWithTypeInfoAny() = testApplication {
install(ContentNegotiation) {
Expand Down

0 comments on commit 5fd337e

Please sign in to comment.