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.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.cache.HttpCacheContext;
33 import org.apache.hc.client5.http.classic.ExecChain;
34 import org.apache.hc.client5.http.classic.ExecRuntime;
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
52
53
54 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 HttpCacheContext 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 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 = HttpCacheContext.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 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 }