-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KTOR-4770 Fix ignoreBody for Server ContentNegotiation Request (#3134)
- Loading branch information
Showing
8 changed files
with
115 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...tor-server-plugins/ktor-server-content-negotiation/jvmAndNix/test/RequestConverterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.ktor.server.plugins.contentnegotiation | ||
|
||
import io.ktor.client.request.* | ||
import io.ktor.client.statement.* | ||
import io.ktor.http.* | ||
import io.ktor.serialization.* | ||
import io.ktor.server.application.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.testing.* | ||
import io.ktor.util.reflect.* | ||
import io.ktor.utils.io.* | ||
import io.ktor.utils.io.charsets.* | ||
import kotlin.test.* | ||
|
||
/* | ||
* Copyright 2014-2022 JetBrains s.r.o and contributors. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
class RequestConverterTest { | ||
|
||
@Test | ||
fun testIgnoreType() = testApplication { | ||
var used = false | ||
|
||
install(ContentNegotiation) { | ||
ignoreType<NonSerializableClass>() | ||
register( | ||
ContentType.Application.Json, | ||
object : ContentConverter { | ||
override suspend fun deserialize( | ||
charset: Charset, | ||
typeInfo: TypeInfo, | ||
content: ByteReadChannel | ||
): Any? { | ||
used = true | ||
return null | ||
} | ||
} | ||
) | ||
} | ||
|
||
routing { | ||
post("/foo") { | ||
val result: String = try { | ||
call.receive<NonSerializableClass>() | ||
"OK" | ||
} catch (cause: Throwable) { | ||
cause.message ?: cause.toString() | ||
} | ||
call.respondText(result) | ||
} | ||
post("/bar") { | ||
val result: String = try { | ||
call.receive<SerializableClass>() | ||
"OK" | ||
} catch (cause: Throwable) { | ||
cause.message ?: cause.toString() | ||
} | ||
call.respondText(result) | ||
} | ||
} | ||
|
||
val responseFoo = client.post("/foo") { | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
assertEquals( | ||
"Cannot transform this request's content to io.ktor.server.plugins.contentnegotiation.NonSerializableClass", | ||
responseFoo.bodyAsText() | ||
) | ||
assertFalse(used) | ||
|
||
val responseBar = client.post("/bar") { | ||
contentType(ContentType.Application.Json) | ||
} | ||
|
||
assertEquals( | ||
"No suitable converter found for TypeInfo(" + | ||
"type=class io.ktor.server.plugins.contentnegotiation.SerializableClass, " + | ||
"reifiedType=class io.ktor.server.plugins.contentnegotiation.SerializableClass, " + | ||
"kotlinType=io.ktor.server.plugins.contentnegotiation.SerializableClass" + | ||
")", | ||
responseBar.bodyAsText() | ||
) | ||
assertTrue(used) | ||
} | ||
} | ||
|
||
internal class NonSerializableClass | ||
|
||
internal class SerializableClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters