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  
30  import static org.junit.jupiter.api.Assertions.assertEquals;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  import java.util.Collections;
34  
35  import org.apache.hc.core5.http.HttpRequest;
36  import org.apache.hc.core5.http.HttpVersion;
37  import org.apache.hc.core5.http.ProtocolVersion;
38  import org.apache.hc.core5.http.message.BasicHttpRequest;
39  import org.apache.hc.core5.http.support.BasicRequestBuilder;
40  import org.junit.jupiter.api.BeforeEach;
41  import org.junit.jupiter.api.Test;
42  
43  public class TestRequestProtocolCompliance {
44  
45      private RequestProtocolCompliance impl;
46  
47      @BeforeEach
48      public void setUp() {
49          impl = new RequestProtocolCompliance(false);
50      }
51  
52      @Test
53      public void testRequestWithWeakETagAndRange() throws Exception {
54          final HttpRequest req = new BasicHttpRequest("GET", "/");
55          req.setHeader("Range", "bytes=0-499");
56          req.setHeader("If-Range", "W/\"weak\"");
57          assertEquals(1, impl.requestIsFatallyNonCompliant(req).size());
58      }
59  
60      @Test
61      public void testRequestWithWeekETagForPUTOrDELETEIfMatch() throws Exception {
62          final HttpRequest req = new BasicHttpRequest("PUT", "http://example.com/");
63          req.setHeader("If-Match", "W/\"weak\"");
64          assertEquals(1, impl.requestIsFatallyNonCompliant(req).size());
65      }
66  
67      @Test
68      public void testRequestWithWeekETagForPUTOrDELETEIfMatchAllowed() throws Exception {
69          final HttpRequest req = new BasicHttpRequest("PUT", "http://example.com/");
70          req.setHeader("If-Match", "W/\"weak\"");
71          impl = new RequestProtocolCompliance(true);
72          assertEquals(Collections.emptyList(), impl.requestIsFatallyNonCompliant(req));
73      }
74  
75      @Test
76      public void testRequestContainsNoCacheDirectiveWithFieldName() throws Exception {
77          final HttpRequest req = new BasicHttpRequest("GET", "/");
78          req.setHeader("Cache-Control", "no-cache=false");
79          assertEquals(1, impl.requestIsFatallyNonCompliant(req).size());
80      }
81  
82      @Test
83      public void doesNotModifyACompliantRequest() throws Exception {
84          final HttpRequest req = new BasicHttpRequest("GET", "/");
85          final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
86          impl.makeRequestCompliant(wrapper);
87          assertTrue(HttpTestUtils.equivalent(req, wrapper));
88      }
89  
90      @Test
91      public void upgrades1_0RequestTo1_1() throws Exception {
92          final HttpRequest req = new BasicHttpRequest("GET", "/");
93          req.setVersion(HttpVersion.HTTP_1_0);
94          final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
95          impl.makeRequestCompliant(wrapper);
96          assertEquals(HttpVersion.HTTP_1_1, wrapper.getVersion());
97      }
98  
99      @Test
100     public void downgrades1_2RequestTo1_1() throws Exception {
101         final HttpRequest req = new BasicHttpRequest("GET", "/");
102         req.setVersion(new ProtocolVersion("HTTP", 1, 2));
103         final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
104         impl.makeRequestCompliant(wrapper);
105         assertEquals(HttpVersion.HTTP_1_1, wrapper.getVersion());
106     }
107 
108     @Test
109     public void stripsMinFreshFromRequestIfNoCachePresent()
110         throws Exception {
111         final HttpRequest req = new BasicHttpRequest("GET", "/");
112         req.setHeader("Cache-Control", "no-cache, min-fresh=10");
113         final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
114         impl.makeRequestCompliant(wrapper);
115         assertEquals("no-cache",
116                 wrapper.getFirstHeader("Cache-Control").getValue());
117     }
118 
119     @Test
120     public void stripsMaxFreshFromRequestIfNoCachePresent()
121         throws Exception {
122         final HttpRequest req = new BasicHttpRequest("GET", "/");
123         req.setHeader("Cache-Control", "no-cache, max-stale=10");
124         final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
125         impl.makeRequestCompliant(wrapper);
126         assertEquals("no-cache",
127                 wrapper.getFirstHeader("Cache-Control").getValue());
128     }
129 
130     @Test
131     public void stripsMaxAgeFromRequestIfNoCachePresent()
132         throws Exception {
133         final HttpRequest req = new BasicHttpRequest("GET", "/");
134         req.setHeader("Cache-Control", "no-cache, max-age=10");
135         final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
136         impl.makeRequestCompliant(wrapper);
137         assertEquals("no-cache",
138                 wrapper.getFirstHeader("Cache-Control").getValue());
139     }
140 
141     @Test
142     public void doesNotStripMinFreshFromRequestWithoutNoCache()
143         throws Exception {
144         final HttpRequest req = new BasicHttpRequest("GET", "/");
145         req.setHeader("Cache-Control", "min-fresh=10");
146         final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
147         impl.makeRequestCompliant(wrapper);
148         assertEquals("min-fresh=10",
149                 wrapper.getFirstHeader("Cache-Control").getValue());
150     }
151 
152     @Test
153     public void correctlyStripsMinFreshFromMiddleIfNoCache()
154         throws Exception {
155         final HttpRequest req = new BasicHttpRequest("GET", "/");
156         req.setHeader("Cache-Control", "no-cache,min-fresh=10,no-store");
157         final HttpRequest wrapper = BasicRequestBuilder.copy(req).build();
158         impl.makeRequestCompliant(wrapper);
159         assertEquals("no-cache,no-store",
160                 wrapper.getFirstHeader("Cache-Control").getValue());
161     }
162 
163 }