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