View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.hc.client5.http.impl.cache;
28  
29  import org.apache.hc.core5.http.message.BasicHttpRequest;
30  import org.junit.Assert;
31  import org.junit.Before;
32  import org.junit.Test;
33  
34  public class TestCacheableRequestPolicy {
35  
36      private CacheableRequestPolicy policy;
37  
38      @Before
39      public void setUp() throws Exception {
40          policy = new CacheableRequestPolicy();
41      }
42  
43      @Test
44      public void testIsGetServableFromCache() {
45          final BasicHttpRequest request = new BasicHttpRequest("GET", "someUri");
46  
47          Assert.assertTrue(policy.isServableFromCache(request));
48      }
49  
50      @Test
51      public void testIsGetWithCacheControlServableFromCache() {
52          BasicHttpRequest request = new BasicHttpRequest("GET", "someUri");
53          request.addHeader("Cache-Control", "no-cache");
54  
55          Assert.assertFalse(policy.isServableFromCache(request));
56  
57          request = new BasicHttpRequest("GET", "someUri");
58          request.addHeader("Cache-Control", "no-store");
59          request.addHeader("Cache-Control", "max-age=20");
60  
61          Assert.assertFalse(policy.isServableFromCache(request));
62  
63          request = new BasicHttpRequest("GET", "someUri");
64          request.addHeader("Cache-Control", "public");
65          request.addHeader("Cache-Control", "no-store, max-age=20");
66  
67          Assert.assertFalse(policy.isServableFromCache(request));
68      }
69  
70      @Test
71      public void testIsGetWithPragmaServableFromCache() {
72          BasicHttpRequest request = new BasicHttpRequest("GET", "someUri");
73          request.addHeader("Pragma", "no-cache");
74  
75          Assert.assertFalse(policy.isServableFromCache(request));
76  
77          request = new BasicHttpRequest("GET", "someUri");
78          request.addHeader("Pragma", "value1");
79          request.addHeader("Pragma", "value2");
80  
81          Assert.assertFalse(policy.isServableFromCache(request));
82      }
83  
84      @Test
85      public void testIsHeadServableFromCache() {
86          BasicHttpRequest request = new BasicHttpRequest("HEAD", "someUri");
87  
88          Assert.assertTrue(policy.isServableFromCache(request));
89  
90          request = new BasicHttpRequest("HEAD", "someUri");
91          request.addHeader("Cache-Control", "public");
92          request.addHeader("Cache-Control", "max-age=20");
93  
94          Assert.assertTrue(policy.isServableFromCache(request));
95      }
96  
97      @Test
98      public void testIsHeadWithCacheControlServableFromCache() {
99          BasicHttpRequest request = new BasicHttpRequest("HEAD", "someUri");
100         request.addHeader("Cache-Control", "no-cache");
101 
102         Assert.assertFalse(policy.isServableFromCache(request));
103 
104         request = new BasicHttpRequest("HEAD", "someUri");
105         request.addHeader("Cache-Control", "no-store");
106         request.addHeader("Cache-Control", "max-age=20");
107 
108         Assert.assertFalse(policy.isServableFromCache(request));
109 
110         request = new BasicHttpRequest("HEAD", "someUri");
111         request.addHeader("Cache-Control", "public");
112         request.addHeader("Cache-Control", "no-store, max-age=20");
113 
114         Assert.assertFalse(policy.isServableFromCache(request));
115     }
116 
117     @Test
118     public void testIsHeadWithPragmaServableFromCache() {
119         BasicHttpRequest request = new BasicHttpRequest("HEAD", "someUri");
120         request.addHeader("Pragma", "no-cache");
121 
122         Assert.assertFalse(policy.isServableFromCache(request));
123 
124         request = new BasicHttpRequest("HEAD", "someUri");
125         request.addHeader("Pragma", "value1");
126         request.addHeader("Pragma", "value2");
127 
128         Assert.assertFalse(policy.isServableFromCache(request));
129     }
130 
131     @Test
132     public void testIsArbitraryMethodServableFromCache() {
133         BasicHttpRequest request = new BasicHttpRequest("TRACE", "someUri");
134 
135         Assert.assertFalse(policy.isServableFromCache(request));
136 
137         request = new BasicHttpRequest("get", "someUri");
138 
139         Assert.assertFalse(policy.isServableFromCache(request));
140 
141     }
142 
143 }