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