File tree Expand file tree Collapse file tree
ktor-client/ktor-client-okhttp
src/io/ktor/client/engine/okhttp
test/io/ktor/client/engine/okhttp Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -12,10 +12,12 @@ public final class io/ktor/client/engine/okhttp/OkHttpConfig : io/ktor/client/en
1212 public final fun addNetworkInterceptor (Lokhttp3/Interceptor;)V
1313 public final fun config (Lkotlin/jvm/functions/Function1;)V
1414 public final fun getClientCacheSize ()I
15+ public final fun getDns ()Lokhttp3/Dns;
1516 public final fun getDuplexStreamingEnabled ()Z
1617 public final fun getPreconfigured ()Lokhttp3/OkHttpClient;
1718 public final fun getWebSocketFactory ()Lokhttp3/WebSocket$Factory;
1819 public final fun setClientCacheSize (I)V
20+ public final fun setDns (Lokhttp3/Dns;)V
1921 public final fun setDuplexStreamingEnabled (Z)V
2022 public final fun setPreconfigured (Lokhttp3/OkHttpClient;)V
2123 public final fun setWebSocketFactory (Lokhttp3/WebSocket$Factory;)V
Original file line number Diff line number Diff line change @@ -55,6 +55,24 @@ public class OkHttpConfig : HttpClientEngineConfig() {
5555 */
5656 public var duplexStreamingEnabled: Boolean = false
5757
58+ /* *
59+ * Specifies the [Dns] resolver used by the [OkHttpClient] to look up IP addresses for hostnames.
60+ * When `null`, OkHttp's default [Dns.SYSTEM] resolver is used.
61+ *
62+ * Set this to inject a custom resolver, for example to enable DNS-over-HTTPS or
63+ * to override host resolution in tests:
64+ * ```kotlin
65+ * install(OkHttp) {
66+ * engine {
67+ * dns = Dns { hostname -> listOf(InetAddress.getByName("127.0.0.1")) }
68+ * }
69+ * }
70+ * ```
71+ *
72+ * [Report a problem](https://ktor.io/feedback/?fqname=io.ktor.client.engine.okhttp.OkHttpConfig.dns)
73+ */
74+ public var dns: Dns ? = null
75+
5876 /* *
5977 * Configures [OkHttpClient] using [OkHttpClient.Builder].
6078 *
Original file line number Diff line number Diff line change @@ -152,6 +152,7 @@ public class OkHttpEngine(override val config: OkHttpConfig) : HttpClientEngineB
152152 }
153153 builder.apply (config.config)
154154 config.proxy?.let { builder.proxy(it) }
155+ config.dns?.let { builder.dns(it) }
155156 timeoutExtension?.let {
156157 builder.setupTimeoutAttributes(it)
157158 }
Original file line number Diff line number Diff line change 55package io.ktor.client.engine.okhttp
66
77import okhttp3.Dispatcher
8+ import okhttp3.Dns
89import okhttp3.OkHttpClient
10+ import java.net.InetAddress
911import kotlin.test.Test
1012import kotlin.test.assertSame
1113
@@ -26,4 +28,36 @@ class OkHttpEngineTest {
2628
2729 assertSame(dispatcher, client.dispatcher)
2830 }
31+
32+ @Test
33+ fun `dns config is applied to OkHttpClient` () {
34+ val customDns = Dns { _ -> listOf (InetAddress .getByName(" 127.0.0.1" )) }
35+
36+ val engine = OkHttpEngine (OkHttpConfig ().apply { dns = customDns })
37+ try {
38+ val cacheField = engine.javaClass.getDeclaredField(" clientCache" ).apply { isAccessible = true }
39+ val clientCache = cacheField.get(engine) as Map <* , * >
40+
41+ val client = clientCache[null ] as OkHttpClient
42+
43+ assertSame(customDns, client.dns)
44+ } finally {
45+ engine.close()
46+ }
47+ }
48+
49+ @Test
50+ fun `default dns is preserved when dns config is not set` () {
51+ val engine = OkHttpEngine (OkHttpConfig ())
52+ try {
53+ val cacheField = engine.javaClass.getDeclaredField(" clientCache" ).apply { isAccessible = true }
54+ val clientCache = cacheField.get(engine) as Map <* , * >
55+
56+ val client = clientCache[null ] as OkHttpClient
57+
58+ assertSame(Dns .SYSTEM , client.dns)
59+ } finally {
60+ engine.close()
61+ }
62+ }
2963}
You can’t perform that action at this time.
0 commit comments