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.io.IOException;
30  
31  import org.apache.hc.client5.http.HttpRoute;
32  import org.apache.hc.client5.http.classic.ExecChain;
33  import org.apache.hc.client5.http.classic.ExecRuntime;
34  import org.apache.hc.client5.http.protocol.HttpClientContext;
35  import org.apache.hc.core5.http.ClassicHttpRequest;
36  import org.apache.hc.core5.http.ClassicHttpResponse;
37  import org.apache.hc.core5.http.HttpException;
38  import org.apache.hc.core5.http.HttpHost;
39  import org.apache.hc.core5.http.HttpResponse;
40  import org.apache.hc.core5.http.HttpStatus;
41  import org.apache.hc.core5.http.io.support.ClassicRequestBuilder;
42  import org.apache.hc.core5.http.message.BasicClassicHttpRequest;
43  import org.junit.jupiter.api.Assertions;
44  import org.junit.jupiter.api.BeforeEach;
45  import org.junit.jupiter.api.Test;
46  import org.mockito.Mock;
47  import org.mockito.Mockito;
48  import org.mockito.MockitoAnnotations;
49  
50  /**
51   * This class tests behavior that is allowed (MAY) by the HTTP/1.1 protocol
52   * specification and for which we have implemented the behavior in HTTP cache.
53   */
54  public class TestProtocolAllowedBehavior {
55  
56      static final int MAX_BYTES = 1024;
57      static final int MAX_ENTRIES = 100;
58      static final int ENTITY_LENGTH = 128;
59  
60      HttpHost host;
61      HttpRoute route;
62      HttpClientContext context;
63      @Mock
64      ExecChain mockExecChain;
65      @Mock
66      ExecRuntime mockExecRuntime;
67      @Mock
68      HttpCache mockCache;
69      ClassicHttpRequest request;
70      ClassicHttpResponse originResponse;
71      CacheConfig config;
72      CachingExec impl;
73      HttpCache cache;
74  
75      @BeforeEach
76      public void setUp() throws Exception {
77          MockitoAnnotations.openMocks(this);
78          host = new HttpHost("foo.example.com", 80);
79  
80          route = new HttpRoute(host);
81  
82          request = new BasicClassicHttpRequest("GET", "/foo");
83  
84          context = HttpClientContext.create();
85  
86          originResponse = HttpTestUtils.make200Response();
87  
88          config = CacheConfig.custom()
89                  .setMaxCacheEntries(MAX_ENTRIES)
90                  .setMaxObjectSize(MAX_BYTES)
91                  .setSharedCache(false)
92                  .build();
93  
94          cache = new BasicHttpCache(config);
95          impl = new CachingExec(cache, null, config);
96  
97          Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(originResponse);
98      }
99  
100     public ClassicHttpResponse execute(final ClassicHttpRequest request) throws IOException, HttpException {
101         return impl.execute(
102                 ClassicRequestBuilder.copy(request).build(),
103                 new ExecChain.Scope("test", route, request, mockExecRuntime, context),
104                 mockExecChain);
105     }
106 
107     @Test
108     public void testNonSharedCacheMayCacheResponsesWithCacheControlPrivate() throws Exception {
109         final ClassicHttpRequest req1 = new BasicClassicHttpRequest("GET","/");
110         originResponse.setHeader("Cache-Control","private,max-age=3600");
111 
112         final ClassicHttpRequest req2 = new BasicClassicHttpRequest("GET","/");
113 
114         Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(originResponse);
115 
116         execute(req1);
117         final HttpResponse result = execute(req2);
118         Assertions.assertEquals(HttpStatus.SC_OK, result.getCode());
119 
120         Mockito.verifyNoInteractions(mockCache);
121     }
122 }