Home | History | Annotate | Download | only in okhttp
      1 /*
      2  * Copyright (C) 2014 Square, Inc.
      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 package com.squareup.okhttp;
     17 
     18 import java.util.concurrent.TimeUnit;
     19 import org.junit.Test;
     20 
     21 import static org.junit.Assert.assertEquals;
     22 import static org.junit.Assert.assertFalse;
     23 import static org.junit.Assert.assertNull;
     24 import static org.junit.Assert.assertSame;
     25 import static org.junit.Assert.assertTrue;
     26 import static org.junit.Assert.fail;
     27 
     28 public final class CacheControlTest {
     29   @Test public void emptyBuilderIsEmpty() throws Exception {
     30     CacheControl cacheControl = new CacheControl.Builder().build();
     31     assertEquals("", cacheControl.toString());
     32     assertFalse(cacheControl.noCache());
     33     assertFalse(cacheControl.noStore());
     34     assertEquals(-1, cacheControl.maxAgeSeconds());
     35     assertEquals(-1, cacheControl.sMaxAgeSeconds());
     36     assertFalse(cacheControl.isPrivate());
     37     assertFalse(cacheControl.isPublic());
     38     assertFalse(cacheControl.mustRevalidate());
     39     assertEquals(-1, cacheControl.maxStaleSeconds());
     40     assertEquals(-1, cacheControl.minFreshSeconds());
     41     assertFalse(cacheControl.onlyIfCached());
     42     assertFalse(cacheControl.mustRevalidate());
     43   }
     44 
     45   @Test public void completeBuilder() throws Exception {
     46     CacheControl cacheControl = new CacheControl.Builder()
     47         .noCache()
     48         .noStore()
     49         .maxAge(1, TimeUnit.SECONDS)
     50         .maxStale(2, TimeUnit.SECONDS)
     51         .minFresh(3, TimeUnit.SECONDS)
     52         .onlyIfCached()
     53         .noTransform()
     54         .build();
     55     assertEquals("no-cache, no-store, max-age=1, max-stale=2, min-fresh=3, only-if-cached, "
     56         + "no-transform", cacheControl.toString());
     57     assertTrue(cacheControl.noCache());
     58     assertTrue(cacheControl.noStore());
     59     assertEquals(1, cacheControl.maxAgeSeconds());
     60     assertEquals(2, cacheControl.maxStaleSeconds());
     61     assertEquals(3, cacheControl.minFreshSeconds());
     62     assertTrue(cacheControl.onlyIfCached());
     63 
     64     // These members are accessible to response headers only.
     65     assertEquals(-1, cacheControl.sMaxAgeSeconds());
     66     assertFalse(cacheControl.isPrivate());
     67     assertFalse(cacheControl.isPublic());
     68     assertFalse(cacheControl.mustRevalidate());
     69   }
     70 
     71   @Test public void parseEmpty() throws Exception {
     72     CacheControl cacheControl = CacheControl.parse(
     73         new Headers.Builder().set("Cache-Control", "").build());
     74     assertEquals("", cacheControl.toString());
     75     assertFalse(cacheControl.noCache());
     76     assertFalse(cacheControl.noStore());
     77     assertEquals(-1, cacheControl.maxAgeSeconds());
     78     assertEquals(-1, cacheControl.sMaxAgeSeconds());
     79     assertFalse(cacheControl.isPublic());
     80     assertFalse(cacheControl.mustRevalidate());
     81     assertEquals(-1, cacheControl.maxStaleSeconds());
     82     assertEquals(-1, cacheControl.minFreshSeconds());
     83     assertFalse(cacheControl.onlyIfCached());
     84     assertFalse(cacheControl.mustRevalidate());
     85   }
     86 
     87   @Test public void parse() throws Exception {
     88     String header = "no-cache, no-store, max-age=1, s-maxage=2, private, public, must-revalidate, "
     89         + "max-stale=3, min-fresh=4, only-if-cached, no-transform";
     90     CacheControl cacheControl = CacheControl.parse(new Headers.Builder()
     91         .set("Cache-Control", header)
     92         .build());
     93     assertTrue(cacheControl.noCache());
     94     assertTrue(cacheControl.noStore());
     95     assertEquals(1, cacheControl.maxAgeSeconds());
     96     assertEquals(2, cacheControl.sMaxAgeSeconds());
     97     assertTrue(cacheControl.isPrivate());
     98     assertTrue(cacheControl.isPublic());
     99     assertTrue(cacheControl.mustRevalidate());
    100     assertEquals(3, cacheControl.maxStaleSeconds());
    101     assertEquals(4, cacheControl.minFreshSeconds());
    102     assertTrue(cacheControl.onlyIfCached());
    103     assertTrue(cacheControl.noTransform());
    104     assertEquals(header, cacheControl.toString());
    105   }
    106 
    107   @Test public void parseIgnoreCacheControlExtensions() throws Exception {
    108     // Example from http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9.6
    109     String header = "private, community=\"UCI\"";
    110     CacheControl cacheControl = CacheControl.parse(new Headers.Builder()
    111         .set("Cache-Control", header)
    112         .build());
    113     assertFalse(cacheControl.noCache());
    114     assertFalse(cacheControl.noStore());
    115     assertEquals(-1, cacheControl.maxAgeSeconds());
    116     assertEquals(-1, cacheControl.sMaxAgeSeconds());
    117     assertTrue(cacheControl.isPrivate());
    118     assertFalse(cacheControl.isPublic());
    119     assertFalse(cacheControl.mustRevalidate());
    120     assertEquals(-1, cacheControl.maxStaleSeconds());
    121     assertEquals(-1, cacheControl.minFreshSeconds());
    122     assertFalse(cacheControl.onlyIfCached());
    123     assertFalse(cacheControl.noTransform());
    124     assertEquals(header, cacheControl.toString());
    125   }
    126 
    127   @Test public void parseCacheControlAndPragmaAreCombined() {
    128     Headers headers =
    129         Headers.of("Cache-Control", "max-age=12", "Pragma", "must-revalidate", "Pragma", "public");
    130     CacheControl cacheControl = CacheControl.parse(headers);
    131     assertEquals("max-age=12, public, must-revalidate", cacheControl.toString());
    132   }
    133 
    134   @SuppressWarnings("RedundantStringConstructorCall") // Testing instance equality.
    135   @Test public void parseCacheControlHeaderValueIsRetained() {
    136     String value = new String("max-age=12");
    137     Headers headers = Headers.of("Cache-Control", value);
    138     CacheControl cacheControl = CacheControl.parse(headers);
    139     assertSame(value, cacheControl.toString());
    140   }
    141 
    142   @Test public void parseCacheControlHeaderValueInvalidatedByPragma() {
    143     Headers headers = Headers.of("Cache-Control", "max-age=12", "Pragma", "must-revalidate");
    144     CacheControl cacheControl = CacheControl.parse(headers);
    145     assertNull(cacheControl.headerValue);
    146   }
    147 
    148   @Test public void parseCacheControlHeaderValueInvalidatedByTwoValues() {
    149     Headers headers = Headers.of("Cache-Control", "max-age=12", "Cache-Control", "must-revalidate");
    150     CacheControl cacheControl = CacheControl.parse(headers);
    151     assertNull(cacheControl.headerValue);
    152   }
    153 
    154   @Test public void parsePragmaHeaderValueIsNotRetained() {
    155     Headers headers = Headers.of("Pragma", "must-revalidate");
    156     CacheControl cacheControl = CacheControl.parse(headers);
    157     assertNull(cacheControl.headerValue);
    158   }
    159 
    160   @Test public void computedHeaderValueIsCached() {
    161     CacheControl cacheControl = new CacheControl.Builder()
    162         .maxAge(2, TimeUnit.DAYS)
    163         .build();
    164     assertNull(cacheControl.headerValue);
    165     assertEquals("max-age=172800", cacheControl.toString());
    166     assertEquals("max-age=172800", cacheControl.headerValue);
    167     cacheControl.headerValue = "Hi";
    168     assertEquals("Hi", cacheControl.toString());
    169   }
    170 
    171   @Test public void timeDurationTruncatedToMaxValue() throws Exception {
    172     CacheControl cacheControl = new CacheControl.Builder()
    173         .maxAge(365 * 100, TimeUnit.DAYS) // Longer than Integer.MAX_VALUE seconds.
    174         .build();
    175     assertEquals(Integer.MAX_VALUE, cacheControl.maxAgeSeconds());
    176   }
    177 
    178   @Test public void secondsMustBeNonNegative() throws Exception {
    179     CacheControl.Builder builder = new CacheControl.Builder();
    180     try {
    181       builder.maxAge(-1, TimeUnit.SECONDS);
    182       fail();
    183     } catch (IllegalArgumentException expected) {
    184     }
    185   }
    186 
    187   @Test public void timePrecisionIsTruncatedToSeconds() throws Exception {
    188     CacheControl cacheControl = new CacheControl.Builder()
    189         .maxAge(4999, TimeUnit.MILLISECONDS)
    190         .build();
    191     assertEquals(4, cacheControl.maxAgeSeconds());
    192   }
    193 }
    194