Skip to content

Commit 2587c36

Browse files
committed
Add a cacheUrlOverride
1 parent 73a5bef commit 2587c36

File tree

3 files changed

+11
-36
lines changed

3 files changed

+11
-36
lines changed

okhttp-dnsoverhttps/src/main/kotlin/okhttp3/dnsoverhttps/DnsOverHttps.kt

+8-10
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,7 @@ class DnsOverHttps internal constructor(
186186
throw unknownHostException
187187
}
188188

189-
private fun getCacheOnlyResponse(
190-
request: Request,
191-
): Response? {
189+
private fun getCacheOnlyResponse(request: Request): Response? {
192190
if (client.cache != null) {
193191
try {
194192
// Use the cache without hitting the network first
@@ -200,10 +198,11 @@ class DnsOverHttps internal constructor(
200198

201199
var cacheUrl = request.url
202200

203-
val cacheRequest = request.newBuilder()
204-
.cacheControl(onlyIfCached)
205-
.cacheUrlOverride(cacheUrl)
206-
.build()
201+
val cacheRequest =
202+
request.newBuilder()
203+
.cacheControl(onlyIfCached)
204+
.cacheUrlOverride(cacheUrl)
205+
.build()
207206

208207
val cacheResponse = client.newCall(cacheRequest).execute()
209208

@@ -258,7 +257,7 @@ class DnsOverHttps internal constructor(
258257
url(url)
259258
.cacheUrlOverride(
260259
url.newBuilder()
261-
.addQueryParameter("hostname", hostname).build()
260+
.addQueryParameter("hostname", hostname).build(),
262261
)
263262
.post(query.toRequestBody(DNS_MESSAGE))
264263
} else {
@@ -326,8 +325,7 @@ class DnsOverHttps internal constructor(
326325
this.bootstrapDnsHosts = bootstrapDnsHosts
327326
}
328327

329-
fun bootstrapDnsHosts(vararg bootstrapDnsHosts: InetAddress): Builder =
330-
bootstrapDnsHosts(bootstrapDnsHosts.toList())
328+
fun bootstrapDnsHosts(vararg bootstrapDnsHosts: InetAddress): Builder = bootstrapDnsHosts(bootstrapDnsHosts.toList())
331329

332330
fun systemDns(systemDns: Dns) =
333331
apply {

okhttp-dnsoverhttps/src/test/java/okhttp3/dnsoverhttps/DnsOverHttpsTest.kt

+1-25
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,9 @@ import java.util.concurrent.TimeUnit
3232
import mockwebserver3.MockResponse
3333
import mockwebserver3.MockWebServer
3434
import okhttp3.Cache
35-
import okhttp3.Call
3635
import okhttp3.Dns
37-
import okhttp3.EventListener
3836
import okhttp3.OkHttpClient
3937
import okhttp3.Protocol
40-
import okhttp3.Response
4138
import okhttp3.testing.PlatformRule
4239
import okio.Buffer
4340
import okio.ByteString.Companion.decodeHex
@@ -59,27 +56,6 @@ class DnsOverHttpsTest {
5956
private val bootstrapClient =
6057
OkHttpClient.Builder()
6158
.protocols(listOf(Protocol.HTTP_2, Protocol.HTTP_1_1))
62-
.eventListener(object : EventListener() {
63-
override fun callStart(call: Call) {
64-
println("callStart " + call.request().url + " " + call.request().cacheUrlOverride)
65-
}
66-
67-
override fun satisfactionFailure(call: Call, response: Response) {
68-
println("satisfactionFailure " + call.request().url)
69-
}
70-
71-
override fun cacheHit(call: Call, response: Response) {
72-
println("cacheHit " + call.request().url)
73-
}
74-
75-
override fun cacheMiss(call: Call) {
76-
println("cacheMiss " + call.request().url)
77-
}
78-
79-
override fun cacheConditionalHit(call: Call, cachedResponse: Response) {
80-
println("cacheConditionalHit " + call.request().url)
81-
}
82-
})
8359
.build()
8460

8561
@BeforeEach
@@ -343,7 +319,7 @@ class DnsOverHttpsTest {
343319
private fun buildLocalhost(
344320
bootstrapClient: OkHttpClient,
345321
includeIPv6: Boolean,
346-
post: Boolean = false
322+
post: Boolean = false,
347323
): DnsOverHttps {
348324
val url = server.url("/lookup?ct")
349325
return DnsOverHttps.Builder().client(bootstrapClient)

okhttp/src/main/kotlin/okhttp3/Request.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,8 @@ class Request internal constructor(builder: Builder) {
327327
*
328328
* If set, this will also allow caching for POST requests.
329329
*/
330-
fun cacheUrlOverride(cacheUrlOverride: HttpUrl?) = apply {
330+
fun cacheUrlOverride(cacheUrlOverride: HttpUrl?) =
331+
apply {
331332
this.cacheUrlOverride = cacheUrlOverride
332333
}
333334

0 commit comments

Comments
 (0)