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