1a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom/* 2a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * Copyright (C) 2011 The Android Open Source Project 3a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * 4a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License"); 5a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * you may not use this file except in compliance with the License. 6a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * You may obtain a copy of the License at 7a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * 8a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * http://www.apache.org/licenses/LICENSE-2.0 9a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * 10a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * Unless required by applicable law or agreed to in writing, software 11a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS, 12a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * See the License for the specific language governing permissions and 14a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom * limitations under the License. 15a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom */ 16a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 17a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrompackage android.net.http; 18a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 1918c227ba72406c0b69ce9925a80113060d870256Jesse Wilsonimport com.google.mockwebserver.MockResponse; 2018c227ba72406c0b69ce9925a80113060d870256Jesse Wilsonimport com.google.mockwebserver.MockWebServer; 21a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.io.File; 22a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.net.CacheRequest; 23a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.net.CacheResponse; 24a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.net.ResponseCache; 25a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.net.URI; 26a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.net.URLConnection; 27a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.util.List; 28a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.util.Map; 29a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport java.util.UUID; 30a7284f0e72745d66155e1e282fc07113332790faBrian Carlstromimport junit.framework.TestCase; 31a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 32a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrompublic final class HttpResponseCacheTest extends TestCase { 33a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 34a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom private File cacheDir; 3518c227ba72406c0b69ce9925a80113060d870256Jesse Wilson private MockWebServer server = new MockWebServer(); 36a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 37a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom @Override public void setUp() throws Exception { 38a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom super.setUp(); 39a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom String tmp = System.getProperty("java.io.tmpdir"); 40a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID()); 41a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 42a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 43a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom @Override protected void tearDown() throws Exception { 44a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom ResponseCache.setDefault(null); 4518c227ba72406c0b69ce9925a80113060d870256Jesse Wilson server.shutdown(); 46a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom super.tearDown(); 47a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 48a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 49a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom public void testInstall() throws Exception { 50a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache installed = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 51a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertNotNull(installed); 52a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertSame(installed, ResponseCache.getDefault()); 53a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertSame(installed, HttpResponseCache.getDefault()); 54a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 55a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 56a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom public void testSecondEquivalentInstallDoesNothing() throws Exception { 57a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 58a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache another = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 59a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertSame(first, another); 60a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 61a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 62a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom public void testInstallClosesPreviouslyInstalled() throws Exception { 63a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 64a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache another = HttpResponseCache.install(cacheDir, 8 * 1024 * 1024); 65a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertNotSame(first, another); 66a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom try { 67a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom first.flush(); 68a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom fail(); 69a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } catch (IllegalStateException expected) { 70a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 71a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 72a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 73a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom public void testGetInstalledWithWrongTypeInstalled() { 74a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom ResponseCache.setDefault(new ResponseCache() { 75a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom @Override public CacheResponse get(URI uri, String requestMethod, 76a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom Map<String, List<String>> requestHeaders) { 77a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom return null; 78a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 79a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom @Override public CacheRequest put(URI uri, URLConnection connection) { 80a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom return null; 81a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 82a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom }); 83a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertNull(HttpResponseCache.getInstalled()); 84a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 85a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 86a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom public void testCloseCloses() throws Exception { 87a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 88a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom cache.close(); 89a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom try { 90a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom cache.flush(); 91a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom fail(); 92a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } catch (IllegalStateException expected) { 93a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 94a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 95a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 96a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom public void testCloseUninstalls() throws Exception { 97a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 98a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom cache.close(); 99a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertNull(ResponseCache.getDefault()); 100a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 101a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom 102a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom public void testDeleteUninstalls() throws Exception { 103a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 104a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom cache.delete(); 105a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom assertNull(ResponseCache.getDefault()); 106a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom } 10718c227ba72406c0b69ce9925a80113060d870256Jesse Wilson 10818c227ba72406c0b69ce9925a80113060d870256Jesse Wilson /** 10918c227ba72406c0b69ce9925a80113060d870256Jesse Wilson * Make sure that statistics tracking are wired all the way through the 11018c227ba72406c0b69ce9925a80113060d870256Jesse Wilson * wrapper class. http://code.google.com/p/android/issues/detail?id=25418 11118c227ba72406c0b69ce9925a80113060d870256Jesse Wilson */ 11218c227ba72406c0b69ce9925a80113060d870256Jesse Wilson public void testStatisticsTracking() throws Exception { 11318c227ba72406c0b69ce9925a80113060d870256Jesse Wilson HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024); 11418c227ba72406c0b69ce9925a80113060d870256Jesse Wilson 11518c227ba72406c0b69ce9925a80113060d870256Jesse Wilson server.enqueue(new MockResponse() 11618c227ba72406c0b69ce9925a80113060d870256Jesse Wilson .addHeader("Cache-Control: max-age=60") 11718c227ba72406c0b69ce9925a80113060d870256Jesse Wilson .setBody("A")); 11818c227ba72406c0b69ce9925a80113060d870256Jesse Wilson server.play(); 11918c227ba72406c0b69ce9925a80113060d870256Jesse Wilson 12018c227ba72406c0b69ce9925a80113060d870256Jesse Wilson URLConnection c1 = server.getUrl("/").openConnection(); 12118c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals('A', c1.getInputStream().read()); 12218c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals(1, cache.getRequestCount()); 12318c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals(1, cache.getNetworkCount()); 12418c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals(0, cache.getHitCount()); 12518c227ba72406c0b69ce9925a80113060d870256Jesse Wilson 12618c227ba72406c0b69ce9925a80113060d870256Jesse Wilson URLConnection c2 = server.getUrl("/").openConnection(); 12718c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals('A', c2.getInputStream().read()); 12818c227ba72406c0b69ce9925a80113060d870256Jesse Wilson 12918c227ba72406c0b69ce9925a80113060d870256Jesse Wilson URLConnection c3 = server.getUrl("/").openConnection(); 13018c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals('A', c3.getInputStream().read()); 13118c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals(3, cache.getRequestCount()); 13218c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals(1, cache.getNetworkCount()); 13318c227ba72406c0b69ce9925a80113060d870256Jesse Wilson assertEquals(2, cache.getHitCount()); 13418c227ba72406c0b69ce9925a80113060d870256Jesse Wilson } 135a7284f0e72745d66155e1e282fc07113332790faBrian Carlstrom} 136