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.mockito.Matchers.eq;
30  import static org.mockito.Matchers.isA;
31  import static org.mockito.Mockito.mock;
32  import static org.mockito.Mockito.reset;
33  import static org.mockito.Mockito.verify;
34  import static org.mockito.Mockito.when;
35  
36  import java.io.File;
37  import java.util.Date;
38  
39  import org.apache.http.HttpHost;
40  import org.apache.http.HttpResponse;
41  import org.apache.http.HttpVersion;
42  import org.apache.http.client.cache.HttpCacheStorage;
43  import org.apache.http.client.cache.ResourceFactory;
44  import org.apache.http.client.methods.HttpExecutionAware;
45  import org.apache.http.client.methods.HttpGet;
46  import org.apache.http.client.methods.HttpRequestWrapper;
47  import org.apache.http.client.protocol.HttpClientContext;
48  import org.apache.http.client.utils.DateUtils;
49  import org.apache.http.conn.routing.HttpRoute;
50  import org.apache.http.impl.execchain.ClientExecChain;
51  import org.apache.http.message.BasicHttpResponse;
52  import org.junit.After;
53  import org.junit.Assert;
54  import org.junit.Before;
55  import org.junit.Test;
56  import org.mockito.Matchers;
57  
58  public class TestHttpCacheJiraNumber1147 {
59  
60      private File cacheDir;
61  
62      private void removeCache() {
63          if (this.cacheDir != null) {
64              final File[] files = this.cacheDir.listFiles();
65              for (final File cacheFile : files) {
66                  cacheFile.delete();
67              }
68              this.cacheDir.delete();
69              this.cacheDir = null;
70          }
71      }
72  
73      @Before
74      public void setUp() throws Exception {
75          cacheDir = File.createTempFile("cachedir", "");
76          if (cacheDir.exists()) {
77              cacheDir.delete();
78          }
79          cacheDir.mkdir();
80      }
81  
82      @After
83      public void cleanUp() {
84          removeCache();
85      }
86  
87      @Test
88      public void testIssue1147() throws Exception {
89          final CacheConfig cacheConfig = CacheConfig.custom()
90              .setSharedCache(true)
91              .setMaxObjectSize(262144) //256kb
92              .build();
93  
94          final ResourceFactory resourceFactory = new FileResourceFactory(cacheDir);
95          final HttpCacheStorage httpCacheStorage = new ManagedHttpCacheStorage(cacheConfig);
96  
97          final ClientExecChain backend = mock(ClientExecChain.class);
98          final HttpRequestWrapper get = HttpRequestWrapper.wrap(new HttpGet("http://somehost/"));
99          final HttpClientContext context = HttpClientContext.create();
100         final HttpHost target = new HttpHost("somehost", 80);
101         final HttpRoute route = new HttpRoute(target);
102 
103         context.setTargetHost(target);
104 
105         final Date now = new Date();
106         final Date tenSecondsAgo = new Date(now.getTime() - 10 * 1000L);
107 
108         final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
109         response.setEntity(HttpTestUtils.makeBody(128));
110         response.setHeader("Content-Length", "128");
111         response.setHeader("ETag", "\"etag\"");
112         response.setHeader("Cache-Control", "public, max-age=3600");
113         response.setHeader("Last-Modified", DateUtils.formatDate(tenSecondsAgo));
114 
115         when(backend.execute(
116                 eq(route),
117                 isA(HttpRequestWrapper.class),
118                 isA(HttpClientContext.class),
119                 (HttpExecutionAware) Matchers.isNull())).thenReturn(Proxies.enhanceResponse(response));
120 
121         final BasicHttpCache cache = new BasicHttpCache(resourceFactory, httpCacheStorage, cacheConfig);
122         final ClientExecChain t = createCachingExecChain(backend, cache, cacheConfig);
123 
124         final HttpResponse response1 = t.execute(route, get, context, null);
125         Assert.assertEquals(200, response1.getStatusLine().getStatusCode());
126         IOUtils.consume(response1.getEntity());
127 
128         verify(backend).execute(
129                 eq(route),
130                 isA(HttpRequestWrapper.class),
131                 isA(HttpClientContext.class),
132                 (HttpExecutionAware) Matchers.isNull());
133 
134         removeCache();
135 
136         reset(backend);
137         when(backend.execute(
138                 eq(route),
139                 isA(HttpRequestWrapper.class),
140                 isA(HttpClientContext.class),
141                 (HttpExecutionAware) Matchers.isNull())).thenReturn(Proxies.enhanceResponse(response));
142 
143         final HttpResponse response2 = t.execute(route, get, context, null);
144         Assert.assertEquals(200, response2.getStatusLine().getStatusCode());
145         IOUtils.consume(response2.getEntity());
146 
147         verify(backend).execute(
148                 eq(route),
149                 isA(HttpRequestWrapper.class),
150                 isA(HttpClientContext.class),
151                 (HttpExecutionAware) Matchers.isNull());
152     }
153 
154     protected ClientExecChain createCachingExecChain(final ClientExecChain backend,
155             final BasicHttpCache cache, final CacheConfig config) {
156         return new CachingExec(backend, cache, config);
157     }
158 
159 }