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 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
package info.magnolia.module.cache.filter; |
35 |
|
|
36 |
|
import static org.junit.Assert.*; |
37 |
|
|
38 |
|
import info.magnolia.cms.cache.CacheConstants; |
39 |
|
|
40 |
|
import java.util.Date; |
41 |
|
|
42 |
|
import org.apache.http.client.utils.DateUtils; |
43 |
|
import org.junit.Before; |
44 |
|
import org.junit.Test; |
45 |
|
|
46 |
|
|
47 |
|
@link |
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (57) |
Complexity: 16 |
Complexity Density: 0.39 |
|
49 |
|
public class ResponseExpirationCalculatorTest { |
50 |
|
|
51 |
|
private ResponseExpirationCalculator negotiator; |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
53 |
15 |
@Before... |
54 |
|
public void setUp() { |
55 |
15 |
negotiator = new ResponseExpirationCalculator(); |
56 |
|
} |
57 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1PASS
|
|
58 |
1 |
@Test... |
59 |
|
public void testConsumesCacheHeaders() throws Exception { |
60 |
|
|
61 |
1 |
assertTrue(negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, "")); |
62 |
1 |
assertTrue(negotiator.addHeader(CacheConstants.HEADER_EXPIRES, "")); |
63 |
1 |
assertTrue(negotiator.addHeader(CacheConstants.HEADER_PRAGMA, CacheConstants.HEADER_VALUE_NO_CACHE)); |
64 |
|
|
65 |
1 |
assertFalse(negotiator.addHeader("Content-Type", "")); |
66 |
1 |
assertFalse(negotiator.addHeader("Accept", "")); |
67 |
1 |
assertFalse(negotiator.addHeader("Vary", "")); |
68 |
|
} |
69 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
70 |
1 |
@Test... |
71 |
|
public void testWontCacheWhenPragmaNoCache() throws Exception { |
72 |
|
|
73 |
1 |
negotiator.addHeader(CacheConstants.HEADER_PRAGMA, CacheConstants.HEADER_VALUE_NO_CACHE); |
74 |
1 |
assertEquals(0, negotiator.getMaxAgeInSeconds()); |
75 |
|
} |
76 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
77 |
1 |
@Test... |
78 |
|
public void testWontCacheWhenCacheControlPrivate() throws Exception { |
79 |
|
|
80 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_PRIVATE); |
81 |
1 |
assertEquals(0, negotiator.getMaxAgeInSeconds()); |
82 |
|
} |
83 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
84 |
1 |
@Test... |
85 |
|
public void testWontCacheWhenCacheControlNoCache() throws Exception { |
86 |
|
|
87 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_NO_CACHE); |
88 |
1 |
assertEquals(0, negotiator.getMaxAgeInSeconds()); |
89 |
|
} |
90 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
91 |
1 |
@Test... |
92 |
|
public void testDetectsSharedCacheMaxAge() throws Exception { |
93 |
|
|
94 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_S_MAXAGE + "=15"); |
95 |
1 |
assertEquals(15, negotiator.getMaxAgeInSeconds()); |
96 |
|
} |
97 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
98 |
1 |
@Test... |
99 |
|
public void testDetectsCacheControlMaxAge() throws Exception { |
100 |
|
|
101 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=15"); |
102 |
1 |
assertEquals(15, negotiator.getMaxAgeInSeconds()); |
103 |
|
} |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
105 |
1 |
@Test... |
106 |
|
public void testDetectsExpiresAsLong() throws Exception { |
107 |
|
|
108 |
1 |
negotiator.addHeader(CacheConstants.HEADER_EXPIRES, System.currentTimeMillis() + 10000L); |
109 |
1 |
assertTrue(negotiator.getMaxAgeInSeconds() > 1); |
110 |
|
} |
111 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
112 |
1 |
@Test... |
113 |
|
public void testDetectsExpiresAsInt() throws Exception { |
114 |
|
|
115 |
1 |
negotiator.addHeader(CacheConstants.HEADER_EXPIRES, (int) (System.currentTimeMillis() / 1000) + 10); |
116 |
1 |
assertTrue(negotiator.getMaxAgeInSeconds() > 1); |
117 |
|
} |
118 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
119 |
1 |
@Test... |
120 |
|
public void testDetectsExpiresAsString() throws Exception { |
121 |
|
|
122 |
1 |
String expiresString = DateUtils.formatDate(new Date(System.currentTimeMillis() + 10000), DateUtils.PATTERN_RFC1123); |
123 |
|
|
124 |
1 |
negotiator.addHeader(CacheConstants.HEADER_EXPIRES, expiresString); |
125 |
1 |
assertTrue(negotiator.getMaxAgeInSeconds() > 1); |
126 |
|
} |
127 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
1PASS
|
|
128 |
1 |
@Test... |
129 |
|
public void testSharedCacheMaxAgeOverridesCacheControlMaxAge() throws Exception { |
130 |
|
|
131 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=5," + CacheConstants.HEADER_VALUE_S_MAXAGE + "=15"); |
132 |
1 |
assertEquals(15, negotiator.getMaxAgeInSeconds()); |
133 |
|
} |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
135 |
1 |
@Test... |
136 |
|
public void testSharedCacheMaxAgeOverridesExpires() throws Exception { |
137 |
|
|
138 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=5," + CacheConstants.HEADER_VALUE_S_MAXAGE + "=15"); |
139 |
1 |
negotiator.addHeader(CacheConstants.HEADER_EXPIRES, "0"); |
140 |
1 |
assertEquals(15, negotiator.getMaxAgeInSeconds()); |
141 |
|
} |
142 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
143 |
1 |
@Test... |
144 |
|
public void testCacheControlMaxAgeOverridesExpires() throws Exception { |
145 |
|
|
146 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=5"); |
147 |
1 |
negotiator.addHeader(CacheConstants.HEADER_EXPIRES, "0"); |
148 |
1 |
assertEquals(5, negotiator.getMaxAgeInSeconds()); |
149 |
|
} |
150 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
151 |
1 |
@Test... |
152 |
|
public void testChoosesMostRestrictiveSharedCache() throws Exception { |
153 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_S_MAXAGE + "=5"); |
154 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_S_MAXAGE + "=15"); |
155 |
1 |
assertEquals(5, negotiator.getMaxAgeInSeconds()); |
156 |
|
} |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
158 |
1 |
@Test... |
159 |
|
public void testChoosesMostRestrictiveCacheControlMaxAge() throws Exception { |
160 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=5"); |
161 |
1 |
negotiator.addHeader(CacheConstants.HEADER_CACHE_CONTROL, CacheConstants.HEADER_VALUE_MAX_AGE + "=15"); |
162 |
1 |
assertEquals(5, negotiator.getMaxAgeInSeconds()); |
163 |
|
} |
164 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
1PASS
|
|
165 |
1 |
@Test... |
166 |
|
public void testChoosesMostRestrictiveExpires() throws Exception { |
167 |
1 |
negotiator.addHeader(CacheConstants.HEADER_EXPIRES, System.currentTimeMillis() + 1000 * 10); |
168 |
1 |
negotiator.addHeader(CacheConstants.HEADER_EXPIRES, System.currentTimeMillis() + 1000 * 20); |
169 |
1 |
assertTrue(negotiator.getMaxAgeInSeconds() <= 10); |
170 |
|
} |
171 |
|
|
172 |
|
} |