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.jupiter.api.Assertions;
31  import org.junit.jupiter.api.BeforeEach;
32  import org.junit.jupiter.api.Test;
33  
34  public class TestCacheableRequestPolicy {
35  
36      private CacheableRequestPolicy policy;
37  
38      @BeforeEach
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          final RequestCacheControl cacheControl = RequestCacheControl.builder().build();
47  
48          Assertions.assertTrue(policy.canBeServedFromCache(cacheControl, request));
49      }
50  
51      @Test
52      public void testIsGetWithCacheControlServableFromCache() {
53          final BasicHttpRequest request = new BasicHttpRequest("GET", "someUri");
54          final RequestCacheControl cacheControl = RequestCacheControl.builder()
55                  .setNoCache(true)
56                  .build();
57  
58          Assertions.assertFalse(policy.canBeServedFromCache(cacheControl, request));
59  
60          final RequestCacheControl cacheControl2 = RequestCacheControl.builder()
61                  .setNoStore(true)
62                  .setMaxAge(20)
63                  .build();
64  
65          Assertions.assertFalse(policy.canBeServedFromCache(cacheControl2, request));
66      }
67  
68      @Test
69      public void testIsHeadServableFromCache() {
70          final BasicHttpRequest request = new BasicHttpRequest("HEAD", "someUri");
71          final RequestCacheControl cacheControl = RequestCacheControl.builder().build();
72  
73          Assertions.assertTrue(policy.canBeServedFromCache(cacheControl, request));
74  
75          final RequestCacheControl cacheControl2 = RequestCacheControl.builder()
76                  .setMaxAge(20)
77                  .build();
78  
79          Assertions.assertTrue(policy.canBeServedFromCache(cacheControl2, request));
80      }
81  
82      @Test
83      public void testIsHeadWithCacheControlServableFromCache() {
84          final BasicHttpRequest request = new BasicHttpRequest("HEAD", "someUri");
85          final RequestCacheControl cacheControl = RequestCacheControl.builder()
86                  .setNoCache(true)
87                  .build();
88  
89          Assertions.assertFalse(policy.canBeServedFromCache(cacheControl, request));
90  
91          request.addHeader("Cache-Control", "no-store");
92          request.addHeader("Cache-Control", "max-age=20");
93          final RequestCacheControl cacheControl2 = RequestCacheControl.builder()
94                  .setNoStore(true)
95                  .setMaxAge(20)
96                  .build();
97  
98          Assertions.assertFalse(policy.canBeServedFromCache(cacheControl2, request));
99      }
100 
101     @Test
102     public void testIsArbitraryMethodServableFromCache() {
103         final BasicHttpRequest request = new BasicHttpRequest("TRACE", "someUri");
104         final RequestCacheControl cacheControl = RequestCacheControl.builder()
105                 .build();
106 
107         Assertions.assertFalse(policy.canBeServedFromCache(cacheControl, request));
108 
109         final BasicHttpRequest request2 = new BasicHttpRequest("huh", "someUri");
110 
111         Assertions.assertFalse(policy.canBeServedFromCache(cacheControl, request2));
112 
113     }
114 
115 }