Clover icon

Magnolia Module Cache 5.5.9

  1. Project Clover database Mon Nov 25 2019 16:46:50 CET
  2. Package info.magnolia.module.cache.filter

File ResponseExpirationCalculatorTest.java

 

Code metrics

0
41
16
1
172
99
16
0.39
2.56
16
1

Classes

Class Line # Actions
ResponseExpirationCalculatorTest 49 41 0% 16 0
1.0100%
 

Contributing tests

This file is covered by 15 tests. .

Source view

1    /**
2    * This file Copyright (c) 2011-2018 Magnolia International
3    * Ltd. (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10    * This file is distributed in the hope that it will be
11    * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12    * implied warranty of MERCHANTABILITY or FITNESS FOR A
13    * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14    * Redistribution, except as permitted by whichever of the GPL
15    * or MNA you select, is prohibited.
16    *
17    * 1. For the GPL license (GPL), you can redistribute and/or
18    * modify this file under the terms of the GNU General
19    * Public License, Version 3, as published by the Free Software
20    * Foundation. You should have received a copy of the GNU
21    * General Public License, Version 3 along with this program;
22    * if not, write to the Free Software Foundation, Inc., 51
23    * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24    *
25    * 2. For the Magnolia Network Agreement (MNA), this file
26    * and the accompanying materials are made available under the
27    * terms of the MNA which accompanies this distribution, and
28    * is available at http://www.magnolia-cms.com/mna.html
29    *
30    * Any modifications to this file must keep this entire header
31    * intact.
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    * Test case for {@link ResponseExpirationCalculator}.
48    */
 
49    public class ResponseExpirationCalculatorTest {
50   
51    private ResponseExpirationCalculator negotiator;
52   
 
53  15 toggle @Before
54    public void setUp() {
55  15 negotiator = new ResponseExpirationCalculator();
56    }
57   
 
58  1 toggle @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   
 
70  1 toggle @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   
 
77  1 toggle @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   
 
84  1 toggle @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   
 
91  1 toggle @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   
 
98  1 toggle @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   
 
105  1 toggle @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   
 
112  1 toggle @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   
 
119  1 toggle @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   
 
128  1 toggle @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   
 
135  1 toggle @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   
 
143  1 toggle @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   
 
151  1 toggle @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   
 
158  1 toggle @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   
 
165  1 toggle @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    }