1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.http.cts; 18 19 import com.google.mockwebserver.MockResponse; 20 import com.google.mockwebserver.MockWebServer; 21 22 import junit.framework.TestCase; 23 24 import android.net.http.HttpResponseCache; 25 import android.platform.test.annotations.AppModeFull; 26 27 import com.android.compatibility.common.util.FileUtils; 28 29 import java.io.File; 30 import java.io.InputStream; 31 import java.net.CacheRequest; 32 import java.net.CacheResponse; 33 import java.net.ResponseCache; 34 import java.net.URI; 35 import java.net.URLConnection; 36 import java.util.List; 37 import java.util.Map; 38 import java.util.UUID; 39 40 public final class HttpResponseCacheTest extends TestCase { 41 42 private File cacheDir; 43 private MockWebServer server; 44 setUp()45 @Override public void setUp() throws Exception { 46 super.setUp(); 47 server = new MockWebServer(); 48 String tmp = System.getProperty("java.io.tmpdir"); 49 cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); 50 cacheDir.mkdirs(); 51 // Make the cache directory read / writable. 52 FileUtils.setPermissions(cacheDir.getPath(), 0777); 53 } 54 tearDown()55 @Override protected void tearDown() throws Exception { 56 ResponseCache.setDefault(null); 57 server.shutdown(); 58 super.tearDown(); 59 } 60 testInstall()61 public void testInstall() throws Exception { 62 HttpResponseCache installed = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 63 assertNotNull(installed); 64 assertSame(installed, ResponseCache.getDefault()); 65 assertSame(installed, HttpResponseCache.getDefault()); 66 } 67 testSecondEquivalentInstallDoesNothing()68 public void testSecondEquivalentInstallDoesNothing() throws Exception { 69 HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 70 HttpResponseCache another = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 71 assertSame(first, another); 72 } 73 testInstallClosesPreviouslyInstalled()74 public void testInstallClosesPreviouslyInstalled() throws Exception { 75 HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 76 initializeCache(first); 77 78 HttpResponseCache another = HttpResponseCache.install(cacheDir, 8 * 1024 * 1024); 79 initializeCache(first); 80 81 assertNotSame(first, another); 82 try { 83 first.flush(); 84 fail(); 85 } catch (IllegalStateException expected) { 86 } 87 } 88 testGetInstalledWithWrongTypeInstalled()89 public void testGetInstalledWithWrongTypeInstalled() { 90 ResponseCache.setDefault(new ResponseCache() { 91 @Override public CacheResponse get(URI uri, String requestMethod, 92 Map<String, List<String>> requestHeaders) { 93 return null; 94 } 95 @Override public CacheRequest put(URI uri, URLConnection connection) { 96 return null; 97 } 98 }); 99 assertNull(HttpResponseCache.getInstalled()); 100 } 101 testCloseCloses()102 public void testCloseCloses() throws Exception { 103 HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 104 initializeCache(cache); 105 106 cache.close(); 107 try { 108 cache.flush(); 109 fail(); 110 } catch (IllegalStateException expected) { 111 } 112 } 113 testCloseUninstalls()114 public void testCloseUninstalls() throws Exception { 115 HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 116 cache.close(); 117 assertNull(ResponseCache.getDefault()); 118 } 119 testDeleteUninstalls()120 public void testDeleteUninstalls() throws Exception { 121 HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 122 cache.delete(); 123 assertNull(ResponseCache.getDefault()); 124 } 125 126 /** 127 * Make sure that statistics tracking are wired all the way through the 128 * wrapper class. http://code.google.com/p/android/issues/detail?id=25418 129 */ 130 @AppModeFull(reason = "Socket cannot bind in instant app mode") testStatisticsTracking()131 public void testStatisticsTracking() throws Exception { 132 HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 133 134 server.enqueue(new MockResponse() 135 .addHeader("Cache-Control: max-age=60") 136 .setBody("A")); 137 server.play(); 138 139 URLConnection c1 = server.getUrl("/").openConnection(); 140 InputStream inputStream1 = c1.getInputStream(); 141 assertEquals('A', inputStream1.read()); 142 inputStream1.close(); 143 144 assertEquals(1, cache.getRequestCount()); 145 assertEquals(1, cache.getNetworkCount()); 146 assertEquals(0, cache.getHitCount()); 147 148 URLConnection c2 = server.getUrl("/").openConnection(); 149 assertEquals('A', c2.getInputStream().read()); 150 151 URLConnection c3 = server.getUrl("/").openConnection(); 152 assertEquals('A', c3.getInputStream().read()); 153 assertEquals(3, cache.getRequestCount()); 154 assertEquals(1, cache.getNetworkCount()); 155 assertEquals(2, cache.getHitCount()); 156 } 157 initializeCache(HttpResponseCache cache)158 private void initializeCache(HttpResponseCache cache) { 159 // Ensure the cache is initialized, otherwise various methods are no-ops. 160 cache.size(); 161 } 162 } 163