Не подтверждена Коммит e5bc12d6 создал по автору Rustam's avatar Rustam Зафиксировано автором GitHub
Просмотр файлов

KTOR-4160 KTOR-4230 Fixed original exception is swallowed by "No request...

KTOR-4160 KTOR-4230 Fixed original exception is swallowed by "No request transformation found" (#2992)
владелец 4e79e8cc
......@@ -6,11 +6,12 @@ package io.ktor.client.plugins
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.call.HttpClientCall
import io.ktor.client.plugins.HttpCallValidator.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.utils.*
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.util.*
import io.ktor.util.pipeline.*
......@@ -125,10 +126,7 @@ public class HttpCallValidator internal constructor(
proceedWith(it)
} catch (cause: Throwable) {
val unwrappedCause = cause.unwrapCancellationException()
plugin.processException(
unwrappedCause,
DefaultHttpRequest(HttpClientCall(scope), context.build())
)
plugin.processException(unwrappedCause, HttpRequest(context))
throw unwrappedCause
}
}
......@@ -154,6 +152,17 @@ public class HttpCallValidator internal constructor(
}
}
private fun HttpRequest(builder: HttpRequestBuilder) = object : HttpRequest {
override val call: HttpClientCall get() = error("Call is not initialized")
override val method: HttpMethod = builder.method
override val url: Url = builder.url.build()
override val attributes: Attributes = builder.attributes
override val headers: Headers = builder.headers.build()
override val content: OutgoingContent
get() = builder.body as? OutgoingContent
?: error("Content was not transformed to OutgoingContent yet. Current body is ${builder.body}")
}
/**
* Install [HttpCallValidator] with [block] configuration.
*/
......
......@@ -11,7 +11,6 @@ import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.client.tests.utils.*
import io.ktor.http.*
import io.ktor.utils.io.concurrent.*
import kotlin.test.*
@Suppress("DEPRECATION")
......@@ -414,6 +413,58 @@ class CallValidatorTest {
}
}
}
@Test
fun testThrowsOriginalExceptionWhenBodyIsNotSerialized() = testWithEngine(MockEngine) {
class TestException : RuntimeException()
config {
engine {
addHandler { respond("OK") }
}
}
test { client ->
client.requestPipeline.intercept(HttpRequestPipeline.Render) {
throw TestException()
}
assertFailsWith<TestException> {
client.get("/") {
setBody(listOf("a", "b", "c"))
}
}
}
}
@Test
fun testCanNotAccessBodyAndCallWhenNotSerialized() = testWithEngine(MockEngine) {
class TestException : RuntimeException()
config {
engine {
addHandler { respond("OK") }
}
HttpResponseValidator {
handleResponseExceptionWithRequest { _, request ->
assertFailsWith<IllegalStateException> { request.content }
assertFailsWith<IllegalStateException> { request.call }
}
}
}
test { client ->
client.requestPipeline.intercept(HttpRequestPipeline.Render) {
throw TestException()
}
assertFailsWith<TestException> {
client.get("/") {
setBody(listOf("a", "b", "c"))
}
}
}
}
}
internal class CallValidatorTestException : Throwable()
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать