Skip to content

Commit 3b4fb14

Browse files
committed
Fix broken code
1 parent 783a9fb commit 3b4fb14

File tree

7 files changed

+51
-69
lines changed

7 files changed

+51
-69
lines changed

ktor-client/ktor-client-plugins/ktor-client-call-id/common/test/io/ktor/client/plugins/callid/CallIdTest.kt

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@ import io.ktor.server.plugins.callid.*
1212
import io.ktor.server.response.*
1313
import io.ktor.server.routing.*
1414
import io.ktor.server.testing.*
15-
import kotlin.coroutines.*
16-
import kotlin.test.*
15+
import kotlinx.coroutines.currentCoroutineContext
16+
import kotlin.test.Test
17+
import kotlin.test.assertEquals
1718
import io.ktor.server.plugins.callid.CallId as ServerCallId
1819

1920
class CallIdTest {
@@ -37,7 +38,7 @@ class CallIdTest {
3738
}
3839

3940
val response = client.get("/1").bodyAsText()
40-
assertEquals(response, "call-id-1:call-id-1:call-id-1")
41+
assertEquals("call-id-1:call-id-1:call-id-1", response)
4142
}
4243

4344
@Test
@@ -63,7 +64,7 @@ class CallIdTest {
6364
}
6465

6566
val response = client.get("/1").bodyAsText()
66-
assertEquals(response, "call-id-client-0:call-id-client-0:call-id-client-0")
67+
assertEquals("call-id-client-0:call-id-client-0:call-id-client-0", response)
6768
}
6869

6970
@Test
@@ -90,7 +91,7 @@ class CallIdTest {
9091
}
9192

9293
val response = client.get("/1").bodyAsText()
93-
assertEquals(response, "call-id-client-1:call-id-client-1:call-id-client-1")
94+
assertEquals("call-id-client-1:call-id-client-1:call-id-client-1", response)
9495
}
9596

9697
@Test
@@ -116,12 +117,13 @@ class CallIdTest {
116117
}
117118

118119
val response = client.get("/1").bodyAsText()
119-
assertEquals(response, "null:call-id-1")
120+
assertEquals("null:call-id-1", response)
120121
}
121122

122123
private suspend fun RoutingContext.respondWithCallId() {
123124
val callIdFromCall = call.callId ?: error("No call id in call")
124-
val callIdFromContext = coroutineContext[KtorCallIdContextElement]?.callId ?: error("No call id in context")
125+
val callIdFromContext = currentCoroutineContext()[KtorCallIdContextElement]?.callId
126+
?: error("No call id in context")
125127
val callIdFromHeader = call.request.headers[HttpHeaders.XRequestId] ?: error("No call id in header")
126128
call.respond("$callIdFromCall:$callIdFromContext:$callIdFromHeader")
127129
}

ktor-server/ktor-server-plugins/ktor-server-call-logging/jvm/test/io/ktor/server/plugins/calllogging/CallLoggingTest.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@ import io.ktor.server.request.*
1818
import io.ktor.server.response.*
1919
import io.ktor.server.routing.*
2020
import io.ktor.server.testing.*
21-
import io.ktor.util.logging.Logger
22-
import kotlinx.coroutines.*
23-
import kotlinx.coroutines.slf4j.*
24-
import kotlinx.coroutines.test.*
25-
import org.fusesource.jansi.*
26-
import org.slf4j.*
27-
import org.slf4j.event.*
28-
import java.io.*
29-
import java.util.concurrent.*
21+
import io.ktor.util.logging.*
22+
import kotlinx.coroutines.DelicateCoroutinesApi
23+
import kotlinx.coroutines.asCoroutineDispatcher
24+
import kotlinx.coroutines.newFixedThreadPoolContext
25+
import kotlinx.coroutines.slf4j.MDCContext
26+
import kotlinx.coroutines.test.runTest
27+
import kotlinx.coroutines.withContext
28+
import org.fusesource.jansi.Ansi
29+
import org.slf4j.LoggerFactory
30+
import org.slf4j.MDC
31+
import org.slf4j.event.Level
32+
import java.io.File
33+
import java.util.concurrent.Executors
3034
import kotlin.test.*
3135

3236
@Suppress("SameParameterValue")
@@ -189,7 +193,7 @@ class CallLoggingTest {
189193
}
190194
routing {
191195
get("/*") {
192-
environment.log.info("test message")
196+
application.environment.log.info("test message")
193197
call.respond("OK")
194198
}
195199
}
@@ -214,8 +218,8 @@ class CallLoggingTest {
214218
}
215219
routing {
216220
get("/*") {
217-
environment.log.info("test1")
218-
environment.log.info("test2")
221+
application.environment.log.info("test1")
222+
application.environment.log.info("test2")
219223
call.respond("OK")
220224
}
221225
}

ktor-server/ktor-server-plugins/ktor-server-di/common/test/io/ktor/server/plugins/di/DependencyInjectionTest.kt

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,19 @@
44

55
package io.ktor.server.plugins.di
66

7-
import io.ktor.client.request.get
8-
import io.ktor.client.statement.HttpResponse
9-
import io.ktor.client.statement.bodyAsText
7+
import io.ktor.client.request.*
8+
import io.ktor.client.statement.*
109
import io.ktor.server.application.*
11-
import io.ktor.server.response.respondText
10+
import io.ktor.server.response.*
1211
import io.ktor.server.routing.*
1312
import io.ktor.server.testing.*
14-
import io.ktor.test.dispatcher.runTestWithRealTime
15-
import io.ktor.util.logging.Logger
13+
import io.ktor.test.dispatcher.*
14+
import io.ktor.util.logging.*
1615
import io.ktor.utils.io.CancellationException
17-
import kotlinx.coroutines.CompletableDeferred
18-
import kotlinx.coroutines.Deferred
19-
import kotlinx.coroutines.Dispatchers
20-
import kotlinx.coroutines.awaitCancellation
21-
import kotlinx.coroutines.cancelAndJoin
16+
import kotlinx.coroutines.*
2217
import kotlinx.coroutines.channels.Channel
23-
import kotlinx.coroutines.coroutineScope
24-
import kotlinx.coroutines.delay
2518
import kotlinx.coroutines.flow.consumeAsFlow
2619
import kotlinx.coroutines.flow.toList
27-
import kotlinx.coroutines.joinAll
28-
import kotlinx.coroutines.launch
2920
import kotlinx.coroutines.test.TestResult
3021
import kotlinx.coroutines.test.runTest
3122
import kotlinx.serialization.Serializable
@@ -335,15 +326,15 @@ class DependencyInjectionTest {
335326
val bankService: Deferred<BankService> = dependencies.resolveDeferred()
336327
routing {
337328
get("/hello") {
338-
val service: GreetingService = dependencies.resolve()
329+
val service: GreetingService = application.dependencies.resolve()
339330
call.respondText(service.hello())
340331
}
341332
get("/balance") {
342333
val service: BankService = bankService.await()
343334
call.respondText(service.balance().toString())
344335
}
345336
get("/bank-teller") {
346-
val service: BankTeller = dependencies.resolve()
337+
val service: BankTeller = application.dependencies.resolve()
347338
call.respondText("${service.hello()}, your balance is ${service.balance()}")
348339
}
349340
}
@@ -390,15 +381,15 @@ class DependencyInjectionTest {
390381
val bankService: Deferred<BankService> = dependencies.resolveDeferred()
391382
routing {
392383
get("/hello") {
393-
val service: GreetingService = dependencies.resolve()
384+
val service: GreetingService = application.dependencies.resolve()
394385
call.respondText(service.hello())
395386
}
396387
get("/balance") {
397388
val service: BankService = bankService.await()
398389
call.respondText(service.balance().toString())
399390
}
400391
get("/bank-teller") {
401-
val service: BankTeller = dependencies.resolve()
392+
val service: BankTeller = application.dependencies.resolve()
402393
call.respondText("${service.hello()}, your balance is ${service.balance()}")
403394
}
404395
}
@@ -429,13 +420,14 @@ class DependencyInjectionTest {
429420
awaitCancellation()
430421
}
431422
}
423+
432424
routing {
433425
get("/hello") {
434426
call.launch {
435427
delay(50)
436-
registryDeferred.complete(dependencies)
428+
registryDeferred.complete(application.dependencies)
437429
}
438-
val service: GreetingService = dependencies.resolve()
430+
val service: GreetingService = application.dependencies.resolve()
439431
call.respondText(service.hello())
440432
}
441433
}

ktor-server/ktor-server-plugins/ktor-server-status-pages/common/test/io/ktor/server/plugins/statuspages/StatusPagesTest.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,11 @@ class StatusPagesTest {
293293

294294
routing {
295295
get("/fail") {
296-
async { throw AsyncFailedException() }.await()
296+
// TODO KTOR-8824: Check why we can't catch an exception thrown from call.async { ... } block
297+
application.async { throw AsyncFailedException() }.await()
297298
}
298299
get("/cancel") {
299-
val j = launch {
300+
val j = application.launch {
300301
delay(1000000L)
301302
}
302303
j.cancel()

ktor-server/ktor-server-test-host/common/test/TestApplicationTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class TestApplicationTest {
156156
}
157157
}
158158
install(TestPlugin) {
159-
pluginClient = client
159+
pluginClient = this@testApplication.client
160160
}
161161
externalServices {
162162
hosts("https://test.com") {

ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/plugins/CORSTest.kt

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import io.ktor.client.statement.*
99
import io.ktor.http.*
1010
import io.ktor.server.application.*
1111
import io.ktor.server.application.hooks.*
12-
import io.ktor.server.plugins.cors.routing.CORS
13-
import io.ktor.server.request.httpMethod
12+
import io.ktor.server.plugins.cors.routing.*
13+
import io.ktor.server.request.*
1414
import io.ktor.server.response.*
1515
import io.ktor.server.routing.*
1616
import io.ktor.server.testing.*
17-
import kotlinx.coroutines.test.*
17+
import kotlinx.coroutines.test.runTest
1818
import kotlin.test.*
1919

2020
class CORSTest {
@@ -1572,24 +1572,4 @@ class CORSTest {
15721572
assertNull(response.headers[HttpHeaders.AccessControlAllowOrigin])
15731573
}
15741574
}
1575-
1576-
@Test
1577-
fun routeInsideCorsPluginConfig() = testApplication {
1578-
routing {
1579-
install(CORS) {
1580-
route("/abc") {
1581-
get {
1582-
call.respond("OK")
1583-
}
1584-
}
1585-
}
1586-
}
1587-
1588-
client.options("/abc") {
1589-
header(HttpHeaders.Origin, "https://example.com")
1590-
header(HttpHeaders.AccessControlRequestMethod, "GET")
1591-
}.let { response ->
1592-
assertEquals(response.status, HttpStatusCode.Forbidden)
1593-
}
1594-
}
15951575
}

ktor-server/ktor-server-tests/common/test/io/ktor/tests/server/plugins/CallIdTest.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ import io.ktor.server.response.*
1515
import io.ktor.server.routing.*
1616
import io.ktor.server.testing.*
1717
import io.ktor.util.pipeline.*
18-
import kotlin.coroutines.*
19-
import kotlin.test.*
18+
import kotlinx.coroutines.currentCoroutineContext
19+
import kotlin.test.Test
20+
import kotlin.test.assertEquals
21+
import kotlin.test.assertNotEquals
22+
import kotlin.test.assertTrue
2023

2124
class CallIdTest {
2225
@Test
@@ -249,7 +252,7 @@ class CallIdTest {
249252
routing {
250253
route("1") {
251254
get {
252-
call.respond(coroutineContext[KtorCallIdContextElement]?.callId ?: "not found")
255+
call.respond(currentCoroutineContext()[KtorCallIdContextElement]?.callId ?: "not found")
253256
}
254257
}
255258
}

0 commit comments

Comments
 (0)