|
| 1 | +package com.howtoprogram.okhttp.cache; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.concurrent.TimeUnit; |
| 6 | + |
| 7 | +import org.junit.Before; |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +import okhttp3.Cache; |
| 11 | +import okhttp3.CacheControl; |
| 12 | +import okhttp3.OkHttpClient; |
| 13 | +import okhttp3.Request; |
| 14 | +import okhttp3.Response; |
| 15 | +import static junit.framework.Assert.*; |
| 16 | + |
| 17 | +public class CacheOkHttpExampleTest { |
| 18 | + |
| 19 | + private OkHttpClient client; |
| 20 | + |
| 21 | + @Before |
| 22 | + public void initCache() throws IOException { |
| 23 | + int cacheSize = 10 * 1024 * 1024; // 10 MiB |
| 24 | + File cacheLoc = new File("/var/tmp/okhttp"); |
| 25 | + Cache cache = new Cache(cacheLoc, cacheSize); |
| 26 | + // Clear all previous caches on the given directory to make sure |
| 27 | + // all our tests run correctly |
| 28 | + cache.evictAll(); |
| 29 | + client = new OkHttpClient.Builder().cache(cache).build(); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void cacheWithOkHttpTest() throws IOException { |
| 34 | + |
| 35 | + // The below URL will produce the response with cache in 20 seconds |
| 36 | + Request request = new Request.Builder().url("http://httpbin.org/cache/20").build(); |
| 37 | + Response response1 = client.newCall(request).execute(); |
| 38 | + if (!response1.isSuccessful()) { |
| 39 | + throw new IOException("Unexpected code " + response1); |
| 40 | + } |
| 41 | + |
| 42 | + response1.close(); |
| 43 | + assertNull(response1.cacheResponse()); |
| 44 | + assertNotNull(response1.networkResponse()); |
| 45 | + |
| 46 | + Response response2 = client.newCall(request).execute(); |
| 47 | + if (!response2.isSuccessful()) { |
| 48 | + throw new IOException("Unexpected code " + response2); |
| 49 | + } |
| 50 | + response2.close(); |
| 51 | + assertNotNull(response2.cacheResponse()); |
| 52 | + assertNull(response2.networkResponse()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void forceNetworkResponseTest() throws IOException { |
| 57 | + // The below URL will produce the response with cache in 20 seconds |
| 58 | + Request request = new Request.Builder().url("http://httpbin.org/cache/20").build(); |
| 59 | + |
| 60 | + Response response4 = client.newCall(request).execute(); |
| 61 | + if (!response4.isSuccessful()) { |
| 62 | + throw new IOException("Unexpected code " + response4); |
| 63 | + } |
| 64 | + response4.close(); |
| 65 | + assertNull(response4.cacheResponse()); |
| 66 | + assertNotNull(response4.networkResponse()); |
| 67 | + |
| 68 | + request = new Request.Builder().cacheControl(new CacheControl.Builder().noCache().build()) |
| 69 | + .url("http://httpbin.org/cache/20").build(); |
| 70 | + |
| 71 | + Response response5 = client.newCall(request).execute(); |
| 72 | + if (!response5.isSuccessful()) { |
| 73 | + throw new IOException("Unexpected code " + response5); |
| 74 | + } |
| 75 | + response5.close(); |
| 76 | + assertNull(response5.cacheResponse()); |
| 77 | + assertNotNull(response5.networkResponse()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + public void forceCacheResponseTest() throws Exception { |
| 82 | + // The below URL will produce the response with cache in 5 seconds |
| 83 | + Request request = new Request.Builder().url("http://httpbin.org/cache/20").build(); |
| 84 | + |
| 85 | + Response response6 = client.newCall(request).execute(); |
| 86 | + if (!response6.isSuccessful()) { |
| 87 | + throw new IOException("Unexpected code " + response6); |
| 88 | + } |
| 89 | + response6.close(); |
| 90 | + assertNull(response6.cacheResponse()); |
| 91 | + assertNotNull(response6.networkResponse()); |
| 92 | + |
| 93 | + // Only accept the response if it is in the cache. If the response isn't |
| 94 | + // cached, a 504 Unsatisfiable Request response will be returned. |
| 95 | + |
| 96 | + request = new Request.Builder() |
| 97 | + .cacheControl(new CacheControl.Builder().onlyIfCached().build()) |
| 98 | + .url("http://httpbin.org/cache/20").build(); |
| 99 | + |
| 100 | + Response response7 = client.newCall(request).execute(); |
| 101 | + |
| 102 | + response7.close(); |
| 103 | + assertNotNull(response7.cacheResponse()); |
| 104 | + assertNull(response7.networkResponse()); |
| 105 | + |
| 106 | + // Sleep 20 seconds to make sure the cache will be stale. Then the 504 |
| 107 | + // response error will be return. |
| 108 | + Thread.sleep(20000); |
| 109 | + |
| 110 | + Response response8 = client.newCall(request).execute(); |
| 111 | + |
| 112 | + response8.close(); |
| 113 | + assertEquals(504, response8.code()); |
| 114 | + assertEquals("Unsatisfiable Request (only-if-cached)", response8.message()); |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void forceCacheResponseAllowStaleTest() throws Exception { |
| 120 | + // The below URL will produce the response with cache in 20 seconds |
| 121 | + Request request = new Request.Builder().url("http://httpbin.org/cache/20").build(); |
| 122 | + |
| 123 | + Response response9 = client.newCall(request).execute(); |
| 124 | + if (!response9.isSuccessful()) { |
| 125 | + throw new IOException("Unexpected code " + response9); |
| 126 | + } |
| 127 | + response9.close(); |
| 128 | + assertNull(response9.cacheResponse()); |
| 129 | + assertNotNull(response9.networkResponse()); |
| 130 | + |
| 131 | + // Sleep 20 seconds to make sure the cache will be stale. |
| 132 | + Thread.sleep(20000); |
| 133 | + request = new Request.Builder() |
| 134 | + .cacheControl(new CacheControl.Builder().onlyIfCached().build()) |
| 135 | + .url("http://httpbin.org/cache/20").build(); |
| 136 | + |
| 137 | + Response response10 = client.newCall(request).execute(); |
| 138 | + assertEquals(504, response10.code()); |
| 139 | + assertEquals("Unsatisfiable Request (only-if-cached)", response10.message()); |
| 140 | + |
| 141 | + request = new Request.Builder() |
| 142 | + .cacheControl(new CacheControl.Builder().maxStale(7, TimeUnit.DAYS).build()) |
| 143 | + .url("http://httpbin.org/cache/20").build(); |
| 144 | + |
| 145 | + Response response11 = client.newCall(request).execute(); |
| 146 | + response11.close(); |
| 147 | + assertNotNull(response11.cacheResponse()); |
| 148 | + assertNull(response11.networkResponse()); |
| 149 | + |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments