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 java.net.SocketTimeoutException;
30  import java.util.Date;
31  
32  import org.apache.http.HttpResponse;
33  import org.apache.http.HttpStatus;
34  import org.apache.http.HttpVersion;
35  import org.apache.http.client.methods.HttpRequestWrapper;
36  import org.apache.http.client.utils.DateUtils;
37  import org.apache.http.message.BasicHttpRequest;
38  import org.junit.Assert;
39  import org.junit.Test;
40  
41  /**
42   * This class tests behavior that is allowed (MAY) by the HTTP/1.1 protocol
43   * specification and for which we have implemented the behavior in the
44   * {@link CachingHttpClient}.
45   */
46  public class TestProtocolAllowedBehavior extends AbstractProtocolTest {
47  
48      @Test
49      public void testNonSharedCacheReturnsStaleResponseWhenRevalidationFailsForProxyRevalidate()
50          throws Exception {
51          final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(
52                  new BasicHttpRequest("GET","/", HttpVersion.HTTP_1_1));
53          final Date now = new Date();
54          final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
55          originResponse.setHeader("Date", DateUtils.formatDate(tenSecondsAgo));
56          originResponse.setHeader("Cache-Control","max-age=5,proxy-revalidate");
57          originResponse.setHeader("Etag","\"etag\"");
58  
59          backendExpectsAnyRequest().andReturn(originResponse);
60  
61          final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
62                  new BasicHttpRequest("GET","/", HttpVersion.HTTP_1_1));
63  
64          backendExpectsAnyRequest().andThrow(new SocketTimeoutException());
65  
66          replayMocks();
67          behaveAsNonSharedCache();
68          impl.execute(route, req1, context, null);
69          final HttpResponse result = impl.execute(route, req2, context, null);
70          verifyMocks();
71  
72          Assert.assertEquals(HttpStatus.SC_OK, result.getStatusLine().getStatusCode());
73      }
74  
75      @Test
76      public void testNonSharedCacheMayCacheResponsesWithCacheControlPrivate()
77          throws Exception {
78          final HttpRequestWrapper req1 = HttpRequestWrapper.wrap(
79                  new BasicHttpRequest("GET","/", HttpVersion.HTTP_1_1));
80          originResponse.setHeader("Cache-Control","private,max-age=3600");
81  
82          backendExpectsAnyRequest().andReturn(originResponse);
83  
84          final HttpRequestWrapper req2 = HttpRequestWrapper.wrap(
85                  new BasicHttpRequest("GET","/", HttpVersion.HTTP_1_1));
86  
87          replayMocks();
88          behaveAsNonSharedCache();
89          impl.execute(route, req1, context, null);
90          final HttpResponse result = impl.execute(route, req2, context, null);
91          verifyMocks();
92  
93          Assert.assertEquals(HttpStatus.SC_OK, result.getStatusLine().getStatusCode());
94      }
95  }