From 4f2b8cbf6580a394ea0dad81c427cc37ed9fa513 Mon Sep 17 00:00:00 2001 From: rsinukov Date: Thu, 15 Sep 2022 15:12:52 +0200 Subject: [PATCH] KTOR-4009 Add flag to respond with 500 on exceptions in tests --- .../io/ktor/server/testing/TestApplication.kt | 2 +- .../server/testing/TestApplicationEngine.kt | 12 ++++++- .../jvmAndNix/test/TestApplicationTest.kt | 31 +++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplication.kt b/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplication.kt index d2a639e2ec..beab0e61f9 100644 --- a/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplication.kt +++ b/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplication.kt @@ -133,7 +133,7 @@ public open class TestApplicationBuilder { developmentMode = true val oldConfig = config this@TestApplicationBuilder.environmentBuilder(this) - if (config == oldConfig) { // config was not set by user. load the default one + if (config == oldConfig) { // the user did not set config. load the default one config = DefaultTestConfig() } parentCoroutineContext += this@TestApplicationBuilder.job diff --git a/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplicationEngine.kt b/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplicationEngine.kt index 0587b47799..f4c43994d1 100644 --- a/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplicationEngine.kt +++ b/ktor-server/ktor-server-test-host/jvmAndNix/src/io/ktor/server/testing/TestApplicationEngine.kt @@ -19,6 +19,10 @@ import kotlinx.atomicfu.* import kotlinx.coroutines.* import kotlin.coroutines.* +@OptIn(InternalAPI::class) +@PublicAPICandidate("2.2.0") +internal const val CONFIG_KEY_THROW_ON_EXCEPTION = "ktor.test.throwOnException" + /** * A test engine that provides a way to simulate application calls to the existing application module(s) * without actual HTTP connection. @@ -96,7 +100,13 @@ class TestApplicationEngine( } private suspend fun PipelineContext.handleTestFailure(cause: Throwable) { - tryRespondError(defaultExceptionStatusCode(cause) ?: throw cause) + val throwOnException = environment.config + .propertyOrNull(CONFIG_KEY_THROW_ON_EXCEPTION) + ?.getString()?.toBoolean() ?: true + tryRespondError( + defaultExceptionStatusCode(cause) + ?: if (throwOnException) throw cause else HttpStatusCode.InternalServerError + ) } private suspend fun PipelineContext.tryRespondError(statusCode: HttpStatusCode) { diff --git a/ktor-server/ktor-server-test-host/jvmAndNix/test/TestApplicationTest.kt b/ktor-server/ktor-server-test-host/jvmAndNix/test/TestApplicationTest.kt index a79c145e97..e2809f19ad 100644 --- a/ktor-server/ktor-server-test-host/jvmAndNix/test/TestApplicationTest.kt +++ b/ktor-server/ktor-server-test-host/jvmAndNix/test/TestApplicationTest.kt @@ -8,8 +8,10 @@ import io.ktor.client.* import io.ktor.client.plugins.* import io.ktor.client.request.* import io.ktor.client.statement.* +import io.ktor.http.* import io.ktor.http.content.* import io.ktor.server.application.* +import io.ktor.server.config.* import io.ktor.server.plugins.* import io.ktor.server.request.* import io.ktor.server.response.* @@ -248,4 +250,33 @@ class TestApplicationTest { request2.await() } } + + @Test + fun testExceptionThrowsByDefault() = testApplication { + routing { + get("/boom") { + throw IllegalStateException("error") + } + } + + val error = assertFailsWith { + client.get("/boom") + } + assertEquals("error", error.message) + } + + @Test + fun testExceptionRespondsWith500IfFlagSet() = testApplication { + environment { + config = MapApplicationConfig(CONFIG_KEY_THROW_ON_EXCEPTION to "false") + } + routing { + get("/boom") { + throw IllegalStateException("error") + } + } + + val response = client.get("/boom") + assertEquals(HttpStatusCode.InternalServerError, response.status) + } }