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