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.cts.util.FileUtils;
25 import android.net.http.HttpResponseCache;
26 
27 import java.io.File;
28 import java.io.InputStream;
29 import java.net.CacheRequest;
30 import java.net.CacheResponse;
31 import java.net.ResponseCache;
32 import java.net.URI;
33 import java.net.URLConnection;
34 import java.util.List;
35 import java.util.Map;
36 import java.util.UUID;
37 
38 public final class HttpResponseCacheTest extends TestCase {
39 
40     private File cacheDir;
41     private MockWebServer server = new MockWebServer();
42 
setUp()43     @Override public void setUp() throws Exception {
44         super.setUp();
45         String tmp = System.getProperty("java.io.tmpdir");
46         cacheDir = new File(tmp, "HttpCache-" + UUID.randomUUID());
47         cacheDir.mkdirs();
48         // Make the cache directory read / writable.
49         FileUtils.setPermissions(cacheDir.getPath(), 0777);
50     }
51 
tearDown()52     @Override protected void tearDown() throws Exception {
53         ResponseCache.setDefault(null);
54         server.shutdown();
55         super.tearDown();
56     }
57 
testInstall()58     public void testInstall() throws Exception {
59         HttpResponseCache installed = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
60         assertNotNull(installed);
61         assertSame(installed, ResponseCache.getDefault());
62         assertSame(installed, HttpResponseCache.getDefault());
63     }
64 
testSecondEquivalentInstallDoesNothing()65     public void testSecondEquivalentInstallDoesNothing() throws Exception {
66         HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
67         HttpResponseCache another = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
68         assertSame(first, another);
69     }
70 
testInstallClosesPreviouslyInstalled()71     public void testInstallClosesPreviouslyInstalled() throws Exception {
72         HttpResponseCache first = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
73         initializeCache(first);
74 
75         HttpResponseCache another = HttpResponseCache.install(cacheDir, 8 * 1024 * 1024);
76         initializeCache(first);
77 
78         assertNotSame(first, another);
79         try {
80             first.flush();
81             fail();
82         } catch (IllegalStateException expected) {
83         }
84     }
85 
testGetInstalledWithWrongTypeInstalled()86     public void testGetInstalledWithWrongTypeInstalled() {
87         ResponseCache.setDefault(new ResponseCache() {
88             @Override public CacheResponse get(URI uri, String requestMethod,
89                     Map<String, List<String>> requestHeaders) {
90                 return null;
91             }
92             @Override public CacheRequest put(URI uri, URLConnection connection) {
93                 return null;
94             }
95         });
96         assertNull(HttpResponseCache.getInstalled());
97     }
98 
testCloseCloses()99     public void testCloseCloses() throws Exception {
100         HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
101         initializeCache(cache);
102 
103         cache.close();
104         try {
105             cache.flush();
106             fail();
107         } catch (IllegalStateException expected) {
108         }
109     }
110 
testCloseUninstalls()111     public void testCloseUninstalls() throws Exception {
112         HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
113         cache.close();
114         assertNull(ResponseCache.getDefault());
115     }
116 
testDeleteUninstalls()117     public void testDeleteUninstalls() throws Exception {
118         HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
119         cache.delete();
120         assertNull(ResponseCache.getDefault());
121     }
122 
123     /**
124      * Make sure that statistics tracking are wired all the way through the
125      * wrapper class. http://code.google.com/p/android/issues/detail?id=25418
126      */
testStatisticsTracking()127     public void testStatisticsTracking() throws Exception {
128         HttpResponseCache cache = HttpResponseCache.install(cacheDir, 10 * 1024 * 1024);
129 
130         server.enqueue(new MockResponse()
131                 .addHeader("Cache-Control: max-age=60")
132                 .setBody("A"));
133         server.play();
134 
135         URLConnection c1 = server.getUrl("/").openConnection();
136         InputStream inputStream1 = c1.getInputStream();
137         assertEquals('A', inputStream1.read());
138         inputStream1.close();
139 
140         assertEquals(1, cache.getRequestCount());
141         assertEquals(1, cache.getNetworkCount());
142         assertEquals(0, cache.getHitCount());
143 
144         URLConnection c2 = server.getUrl("/").openConnection();
145         assertEquals('A', c2.getInputStream().read());
146 
147         URLConnection c3 = server.getUrl("/").openConnection();
148         assertEquals('A', c3.getInputStream().read());
149         assertEquals(3, cache.getRequestCount());
150         assertEquals(1, cache.getNetworkCount());
151         assertEquals(2, cache.getHitCount());
152     }
153 
initializeCache(HttpResponseCache cache)154     private void initializeCache(HttpResponseCache cache) {
155         // Ensure the cache is initialized, otherwise various methods are no-ops.
156         cache.size();
157     }
158 }
159