Created
April 29, 2020 05:53
-
-
Save lynnfield/eb4b285fb0d55a2896beda13c15d6449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import okhttp3.* | |
import okhttp3.internal.closeQuietly | |
import org.junit.Test | |
import java.io.File | |
import java.io.IOException | |
import java.util.concurrent.TimeUnit | |
class CacheTest { | |
@Test | |
fun test() { | |
val directory = File("./cache") | |
val client = OkHttpClient.Builder() | |
.addNetworkInterceptor { chain -> | |
println("----------- intercept netw ------------") | |
val request = chain.request() | |
println(request.headers) | |
chain.proceed(request).also { | |
println(it.request.headers) | |
println("-----------------------") | |
} | |
} | |
.addInterceptor { chain -> | |
println("----------- intercept ------------") | |
val request = chain.request() | |
println(request.headers) | |
chain.proceed(request).also { | |
println(it.request.headers) | |
println("-----------------------") | |
} | |
} | |
.cache(Cache(directory, 1024 * 1024)) | |
.build() | |
val request = | |
Request.Builder().url("https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js").build() | |
client.newCall(request).enqueue(object : Callback { | |
override fun onFailure(call: Call, e: IOException) { | |
TODO("Not yet implemented") | |
} | |
override fun onResponse(call: Call, it: Response) { | |
println(it.request.headers) | |
println("-----------------") | |
println(it.networkResponse?.request?.headers) | |
println("-----------------") | |
println(it.networkResponse?.headers) | |
println("-----------------") | |
println(it.networkResponse) | |
println("=====================") | |
it.closeQuietly() | |
} | |
}) | |
val request1 = | |
Request.Builder().url("https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js").build() | |
client.newCall(request1).enqueue(object : Callback { | |
override fun onFailure(call: Call, e: IOException) { | |
TODO("Not yet implemented") | |
} | |
override fun onResponse(call: Call, it: Response) { | |
println(it.request.headers) | |
println("-----------------") | |
println(it.networkResponse?.request?.headers) | |
println("-----------------") | |
println(it.networkResponse?.headers) | |
println("-----------------") | |
println(it.networkResponse) | |
println("=====================") | |
it.closeQuietly() | |
} | |
}) | |
client.dispatcher.executorService.shutdown() | |
client.dispatcher.executorService.awaitTermination(10, TimeUnit.SECONDS) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment