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.util.HashMap;
30  
31  import org.apache.http.HttpEntity;
32  import org.apache.http.HttpHost;
33  import org.apache.http.HttpRequest;
34  import org.apache.http.HttpResponse;
35  import org.apache.http.HttpVersion;
36  import org.apache.http.client.cache.HttpCacheContext;
37  import org.apache.http.client.methods.CloseableHttpResponse;
38  import org.apache.http.client.methods.HttpExecutionAware;
39  import org.apache.http.client.methods.HttpRequestWrapper;
40  import org.apache.http.client.protocol.HttpClientContext;
41  import org.apache.http.conn.routing.HttpRoute;
42  import org.apache.http.impl.execchain.ClientExecChain;
43  import org.apache.http.message.BasicHttpRequest;
44  import org.easymock.IExpectationSetters;
45  import org.easymock.EasyMock;
46  import org.junit.Before;
47  
48  public abstract class AbstractProtocolTest {
49  
50      protected static final int MAX_BYTES = 1024;
51      protected static final int MAX_ENTRIES = 100;
52      protected int entityLength = 128;
53      protected HttpHost host;
54      protected HttpRoute route;
55      protected HttpEntity body;
56      protected ClientExecChain mockBackend;
57      protected HttpCache mockCache;
58      protected HttpRequestWrapper request;
59      protected HttpCacheContext context;
60      protected CloseableHttpResponse originResponse;
61      protected CacheConfig config;
62      protected ClientExecChain impl;
63      protected HttpCache cache;
64  
65      public static HttpRequestWrapper eqRequest(final HttpRequestWrapper in) {
66          EasyMock.reportMatcher(new RequestEquivalent(in));
67          return null;
68      }
69  
70      public static HttpResponse eqResponse(final HttpResponse in) {
71          EasyMock.reportMatcher(new ResponseEquivalent(in));
72          return null;
73      }
74  
75      public static CloseableHttpResponse eqCloseableResponse(final CloseableHttpResponse in) {
76          EasyMock.reportMatcher(new ResponseEquivalent(in));
77          return null;
78      }
79  
80      @Before
81      public void setUp() {
82          host = new HttpHost("foo.example.com", 80);
83  
84          route = new HttpRoute(host);
85  
86          body = HttpTestUtils.makeBody(entityLength);
87  
88          request = HttpRequestWrapper.wrap(new BasicHttpRequest("GET", "/foo", HttpVersion.HTTP_1_1));
89  
90          context = HttpCacheContext.create();
91          context.setTargetHost(host);
92  
93          originResponse = Proxies.enhanceResponse(HttpTestUtils.make200Response());
94  
95          config = CacheConfig.custom()
96              .setMaxCacheEntries(MAX_ENTRIES)
97              .setMaxObjectSize(MAX_BYTES)
98              .build();
99  
100         cache = new BasicHttpCache(config);
101         mockBackend = EasyMock.createNiceMock(ClientExecChain.class);
102         mockCache = EasyMock.createNiceMock(HttpCache.class);
103         impl = createCachingExecChain(mockBackend, cache, config);
104     }
105 
106     protected ClientExecChain createCachingExecChain(final ClientExecChain backend,
107             final HttpCache cache, final CacheConfig config) {
108         return new CachingExec(backend, cache, config);
109     }
110 
111     protected boolean supportsRangeAndContentRangeHeaders(final ClientExecChain impl) {
112         return impl instanceof CachingExec && ((CachingExec) impl).supportsRangeAndContentRangeHeaders();
113     }
114 
115     protected void replayMocks() {
116         EasyMock.replay(mockBackend);
117         EasyMock.replay(mockCache);
118     }
119 
120     protected void verifyMocks() {
121         EasyMock.verify(mockBackend);
122         EasyMock.verify(mockCache);
123     }
124 
125     protected IExpectationSetters<CloseableHttpResponse> backendExpectsAnyRequest() throws Exception {
126         final CloseableHttpResponse resp = mockBackend.execute(
127                 EasyMock.isA(HttpRoute.class),
128                 EasyMock.isA(HttpRequestWrapper.class),
129                 EasyMock.isA(HttpClientContext.class),
130                 EasyMock.<HttpExecutionAware>isNull());
131         return EasyMock.expect(resp);
132     }
133 
134     protected IExpectationSetters<CloseableHttpResponse> backendExpectsAnyRequestAndReturn(
135             final HttpResponse reponse) throws Exception {
136         final CloseableHttpResponse resp = mockBackend.execute(
137                 EasyMock.isA(HttpRoute.class),
138                 EasyMock.isA(HttpRequestWrapper.class),
139                 EasyMock.isA(HttpClientContext.class),
140                 EasyMock.<HttpExecutionAware>isNull());
141         return EasyMock.expect(resp).andReturn(Proxies.enhanceResponse(reponse));
142     }
143 
144     protected void emptyMockCacheExpectsNoPuts() throws Exception {
145         mockBackend = EasyMock.createNiceMock(ClientExecChain.class);
146         mockCache = EasyMock.createNiceMock(HttpCache.class);
147 
148         impl = new CachingExec(mockBackend, mockCache, config);
149 
150         EasyMock.expect(mockCache.getCacheEntry(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class)))
151             .andReturn(null).anyTimes();
152         EasyMock.expect(mockCache.getVariantCacheEntriesWithEtags(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class)))
153             .andReturn(new HashMap<String,Variant>()).anyTimes();
154 
155         mockCache.flushCacheEntriesFor(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class));
156         EasyMock.expectLastCall().anyTimes();
157 
158         mockCache.flushCacheEntriesFor(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class));
159         EasyMock.expectLastCall().anyTimes();
160 
161         mockCache.flushInvalidatedCacheEntriesFor(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class));
162         EasyMock.expectLastCall().anyTimes();
163 
164         mockCache.flushInvalidatedCacheEntriesFor(EasyMock.isA(HttpHost.class), EasyMock.isA(HttpRequest.class), EasyMock.isA(HttpResponse.class));
165         EasyMock.expectLastCall().anyTimes();
166     }
167 
168     protected void behaveAsNonSharedCache() {
169         config = CacheConfig.custom()
170                 .setMaxCacheEntries(MAX_ENTRIES)
171                 .setMaxObjectSize(MAX_BYTES)
172                 .setSharedCache(false)
173                 .build();
174         impl = new CachingExec(mockBackend, cache, config);
175     }
176 
177     public AbstractProtocolTest() {
178         super();
179     }
180 
181 }