Skip to content

Commit f14ab40

Browse files
committed
Add Cache Test
1 parent 93faf85 commit f14ab40

File tree

1 file changed

+61
-7
lines changed

1 file changed

+61
-7
lines changed

okhttp/src/test/java/okhttp3/CacheTest.kt

+61-7
Original file line numberDiff line numberDiff line change
@@ -982,44 +982,51 @@ class CacheTest {
982982

983983
@Test
984984
fun requestMethodOptionsIsNotCached() {
985-
testRequestMethod("OPTIONS", false)
985+
testRequestMethod("OPTIONS", false, true)
986986
}
987987

988988
@Test
989989
fun requestMethodGetIsCached() {
990-
testRequestMethod("GET", true)
990+
testRequestMethod("GET", true, true)
991991
}
992992

993993
@Test
994994
fun requestMethodHeadIsNotCached() {
995995
// We could support this but choose not to for implementation simplicity
996-
testRequestMethod("HEAD", false)
996+
testRequestMethod("HEAD", false, true)
997997
}
998998

999999
@Test
10001000
fun requestMethodPostIsNotCached() {
10011001
// We could support this but choose not to for implementation simplicity
1002-
testRequestMethod("POST", false)
1002+
testRequestMethod("POST", false, true)
1003+
}
1004+
1005+
@Test
1006+
fun requestMethodPostIsNotCachedUnlessOverride() {
1007+
// We could support this but choose not to for implementation simplicity
1008+
testRequestMethod("POST", true, withOverride = true)
10031009
}
10041010

10051011
@Test
10061012
fun requestMethodPutIsNotCached() {
1007-
testRequestMethod("PUT", false)
1013+
testRequestMethod("PUT", false, true)
10081014
}
10091015

10101016
@Test
10111017
fun requestMethodDeleteIsNotCached() {
1012-
testRequestMethod("DELETE", false)
1018+
testRequestMethod("DELETE", false, true)
10131019
}
10141020

10151021
@Test
10161022
fun requestMethodTraceIsNotCached() {
1017-
testRequestMethod("TRACE", false)
1023+
testRequestMethod("TRACE", false, true)
10181024
}
10191025

10201026
private fun testRequestMethod(
10211027
requestMethod: String,
10221028
expectCached: Boolean,
1029+
withOverride: Boolean = false,
10231030
) {
10241031
// 1. Seed the cache (potentially).
10251032
// 2. Expect a cache hit or miss.
@@ -1038,6 +1045,11 @@ class CacheTest {
10381045
val request =
10391046
Request.Builder()
10401047
.url(url)
1048+
.apply {
1049+
if (withOverride) {
1050+
cacheUrlOverride(url)
1051+
}
1052+
}
10411053
.method(requestMethod, requestBodyOrNull(requestMethod))
10421054
.build()
10431055
val response1 = client.newCall(request).execute()
@@ -3250,6 +3262,48 @@ CLEAN $urlKey ${entryMetadata.length} ${entryBody.length}
32503262
)
32513263
}
32523264

3265+
@Test
3266+
fun getHasCorrectResponseIsCorrect() {
3267+
val request = Request(server.url("/abc"))
3268+
3269+
val response = testBasicCachingRules(request)
3270+
3271+
assertThat(response.request.url).isEqualTo(request.url)
3272+
assertThat(response.cacheResponse!!.request.url).isEqualTo(request.url)
3273+
}
3274+
3275+
@Test
3276+
fun postWithOverrideResponseIsCorrect() {
3277+
val url = server.url("/abc?token=123")
3278+
val cacheUrlOverride = url.newBuilder().removeAllQueryParameters("token").build()
3279+
3280+
val request =
3281+
Request.Builder()
3282+
.url(url)
3283+
.method("POST", "XYZ".toRequestBody())
3284+
.cacheUrlOverride(cacheUrlOverride)
3285+
.build()
3286+
3287+
val response = testBasicCachingRules(request)
3288+
3289+
assertThat(response.request.url).isEqualTo(request.url)
3290+
assertThat(response.cacheResponse!!.request.url).isEqualTo(cacheUrlOverride)
3291+
}
3292+
3293+
private fun testBasicCachingRules(request: Request): Response {
3294+
val mockResponse =
3295+
MockResponse.Builder()
3296+
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
3297+
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
3298+
.status("HTTP/1.1 200 Fantastic")
3299+
server.enqueue(mockResponse.build())
3300+
3301+
client.newCall(request).execute().use {
3302+
it.body.bytes()
3303+
}
3304+
return client.newCall(request).execute()
3305+
}
3306+
32533307
private operator fun get(url: HttpUrl): Response {
32543308
val request =
32553309
Request.Builder()

0 commit comments

Comments
 (0)