@@ -982,44 +982,51 @@ class CacheTest {
982
982
983
983
@Test
984
984
fun requestMethodOptionsIsNotCached () {
985
- testRequestMethod(" OPTIONS" , false )
985
+ testRequestMethod(" OPTIONS" , false , true )
986
986
}
987
987
988
988
@Test
989
989
fun requestMethodGetIsCached () {
990
- testRequestMethod(" GET" , true )
990
+ testRequestMethod(" GET" , true , true )
991
991
}
992
992
993
993
@Test
994
994
fun requestMethodHeadIsNotCached () {
995
995
// We could support this but choose not to for implementation simplicity
996
- testRequestMethod(" HEAD" , false )
996
+ testRequestMethod(" HEAD" , false , true )
997
997
}
998
998
999
999
@Test
1000
1000
fun requestMethodPostIsNotCached () {
1001
1001
// 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 )
1003
1009
}
1004
1010
1005
1011
@Test
1006
1012
fun requestMethodPutIsNotCached () {
1007
- testRequestMethod(" PUT" , false )
1013
+ testRequestMethod(" PUT" , false , true )
1008
1014
}
1009
1015
1010
1016
@Test
1011
1017
fun requestMethodDeleteIsNotCached () {
1012
- testRequestMethod(" DELETE" , false )
1018
+ testRequestMethod(" DELETE" , false , true )
1013
1019
}
1014
1020
1015
1021
@Test
1016
1022
fun requestMethodTraceIsNotCached () {
1017
- testRequestMethod(" TRACE" , false )
1023
+ testRequestMethod(" TRACE" , false , true )
1018
1024
}
1019
1025
1020
1026
private fun testRequestMethod (
1021
1027
requestMethod : String ,
1022
1028
expectCached : Boolean ,
1029
+ withOverride : Boolean = false,
1023
1030
) {
1024
1031
// 1. Seed the cache (potentially).
1025
1032
// 2. Expect a cache hit or miss.
@@ -1038,6 +1045,11 @@ class CacheTest {
1038
1045
val request =
1039
1046
Request .Builder ()
1040
1047
.url(url)
1048
+ .apply {
1049
+ if (withOverride) {
1050
+ cacheUrlOverride(url)
1051
+ }
1052
+ }
1041
1053
.method(requestMethod, requestBodyOrNull(requestMethod))
1042
1054
.build()
1043
1055
val response1 = client.newCall(request).execute()
@@ -3250,6 +3262,48 @@ CLEAN $urlKey ${entryMetadata.length} ${entryBody.length}
3250
3262
)
3251
3263
}
3252
3264
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
+
3253
3307
private operator fun get (url : HttpUrl ): Response {
3254
3308
val request =
3255
3309
Request .Builder ()
0 commit comments