1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 }