@@ -31,9 +31,12 @@ import java.util.concurrent.TimeUnit
31
31
import mockwebserver3.MockResponse
32
32
import mockwebserver3.MockWebServer
33
33
import okhttp3.Cache
34
+ import okhttp3.Call
34
35
import okhttp3.Dns
36
+ import okhttp3.EventListener
35
37
import okhttp3.OkHttpClient
36
38
import okhttp3.Protocol
39
+ import okhttp3.Response
37
40
import okhttp3.testing.PlatformRule
38
41
import okio.Buffer
39
42
import okio.ByteString.Companion.decodeHex
@@ -55,6 +58,27 @@ class DnsOverHttpsTest {
55
58
private val bootstrapClient =
56
59
OkHttpClient .Builder ()
57
60
.protocols(listOf (Protocol .HTTP_2 , Protocol .HTTP_1_1 ))
61
+ .eventListener(object : EventListener () {
62
+ override fun callStart (call : Call ) {
63
+ println (" callStart " + call.request().url + " " + call.request().cacheUrlOverride)
64
+ }
65
+
66
+ override fun satisfactionFailure (call : Call , response : Response ) {
67
+ println (" satisfactionFailure " + call.request().url)
68
+ }
69
+
70
+ override fun cacheHit (call : Call , response : Response ) {
71
+ println (" cacheHit " + call.request().url)
72
+ }
73
+
74
+ override fun cacheMiss (call : Call ) {
75
+ println (" cacheMiss " + call.request().url)
76
+ }
77
+
78
+ override fun cacheConditionalHit (call : Call , cachedResponse : Response ) {
79
+ println (" cacheConditionalHit " + call.request().url)
80
+ }
81
+ })
58
82
.build()
59
83
60
84
@BeforeEach
@@ -193,6 +217,31 @@ class DnsOverHttpsTest {
193
217
assertThat(result).isEqualTo(listOf (address(" 157.240.1.18" )))
194
218
}
195
219
220
+ @Test
221
+ fun usesCacheEvenForPost () {
222
+ val cache = Cache (" cache" .toPath(), (100 * 1024 ).toLong(), cacheFs)
223
+ val cachedClient = bootstrapClient.newBuilder().cache(cache).build()
224
+ val cachedDns = buildLocalhost(cachedClient, false , post = true )
225
+ server.enqueue(
226
+ dnsResponse(
227
+ " 0000818000010003000000000567726170680866616365626f6f6b03636f6d0000010001c00c000500010" +
228
+ " 0000a6d000603617069c012c0300005000100000cde000c04737461720463313072c012c04200010001000" +
229
+ " 0003b00049df00112" ,
230
+ )
231
+ .newBuilder()
232
+ .setHeader(" cache-control" , " private, max-age=298" )
233
+ .build(),
234
+ )
235
+ var result = cachedDns.lookup(" google.com" )
236
+ assertThat(result).containsExactly(address(" 157.240.1.18" ))
237
+ val recordedRequest = server.takeRequest()
238
+ assertThat(recordedRequest.method).isEqualTo(" POST" )
239
+ assertThat(recordedRequest.path)
240
+ .isEqualTo(" /lookup?ct" )
241
+ result = cachedDns.lookup(" google.com" )
242
+ assertThat(result).isEqualTo(listOf (address(" 157.240.1.18" )))
243
+ }
244
+
196
245
@Test
197
246
fun usesCacheOnlyIfFresh () {
198
247
val cache = Cache (File (" ./target/DnsOverHttpsTest.cache" ), 100 * 1024L )
@@ -245,12 +294,14 @@ class DnsOverHttpsTest {
245
294
private fun buildLocalhost (
246
295
bootstrapClient : OkHttpClient ,
247
296
includeIPv6 : Boolean ,
297
+ post : Boolean = false
248
298
): DnsOverHttps {
249
299
val url = server.url(" /lookup?ct" )
250
300
return DnsOverHttps .Builder ().client(bootstrapClient)
251
301
.includeIPv6(includeIPv6)
252
302
.resolvePrivateAddresses(true )
253
303
.url(url)
304
+ .post(post)
254
305
.build()
255
306
}
256
307
0 commit comments