Verwirrenden Variable-Name "controller" in CacheControl in "handle" geändert
[percentcodec] / cachecontrol / src / test / java / de / halbekunst / juplo / cachecontrol / ParameterGuessingTest.java
1 package de.halbekunst.juplo.cachecontrol;
2
3 import com.meterware.httpunit.WebResponse;
4 import java.net.URLEncoder;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import java.text.SimpleDateFormat;
8 import java.util.Calendar;
9 import java.util.Date;
10 import java.util.HashSet;
11 import java.util.Locale;
12 import java.util.Set;
13 import org.junit.Assert;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.springframework.test.context.ContextConfiguration;
17 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18
19
20
21 /**
22  *
23  * @author kai
24  */
25 @RunWith(SpringJUnit4ClassRunner.class)
26 @ContextConfiguration(locations = {
27   "classpath:/config.xml"
28 })
29 public class ParameterGuessingTest extends HttpTestCase {
30   private final static Logger log = LoggerFactory.getLogger(ParameterGuessingTest.class);
31
32
33   @Test
34   public void testNothingSet() throws Exception {
35
36     log.info("-------- Test: Servlet does not implement getLastModified() and sets no Headers...");
37
38     WebResponse response = executeRequest("http://localhost/parameter-guessing?n=16");
39     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
40     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
41     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
42     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
43     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
44     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
45     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
46     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
47     long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
48     long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
49     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
50   }
51
52   @Test
53   public void testSetUnfilteredHeaders() throws Exception {
54
55     log.info("-------- Test: Servlet sets unfiltered Headers...");
56
57     WebResponse response = executeRequest("http://localhost/parameter-guessing?n=16&X-Debug=bla&Age=34&Content-Language=de");
58     Assert.assertEquals("bla", response.getHeaderField("X-Debug"));
59     Assert.assertEquals("34", response.getHeaderField("Age"));
60     Assert.assertEquals("de", response.getHeaderField("Content-Language"));
61     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
62     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
63     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
64     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
65     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
66     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
67     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
68     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
69     long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
70     long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
71     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
72   }
73
74   @Test
75   public void testETagSet() throws Exception {
76
77     log.info("-------- Test: Servlet sets Header \"ETag\"");
78
79     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
80     WebResponse response;
81     long date, expires;
82
83     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("\"bla\"", "UTF-8"));
84     Assert.assertEquals("\"bla\"", response.getHeaderField(Headers.HEADER_ETAG));
85     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
86     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
87     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
88     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
89     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
90     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
91     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
92     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
93     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
94
95     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("\"bÄl\"a\"", "UTF-8"));
96     Assert.assertEquals("\"bla\"", response.getHeaderField(Headers.HEADER_ETAG));
97     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
98     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
99     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
100     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
101     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
102     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
103     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
104     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
105     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
106
107     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("bla", "UTF-8"));
108     Assert.assertEquals("\"bla\"", response.getHeaderField(Headers.HEADER_ETAG));
109     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
110     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
111     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
112     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
113     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
114     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
115     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
116     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
117     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
118
119     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("bÄl\"a", "UTF-8"));
120     Assert.assertEquals("\"bla\"", response.getHeaderField(Headers.HEADER_ETAG));
121     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
122     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
123     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
124     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
125     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
126     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
127     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
128     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
129     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
130
131     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("W/\"blub\"", "UTF-8"));
132     Assert.assertEquals("W/\"blub\"", response.getHeaderField(Headers.HEADER_ETAG));
133     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
134     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
135     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
136     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
137     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
138     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
139     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
140     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
141     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
142
143     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("W/\"bÄl\"ub\"", "UTF-8"));
144     Assert.assertEquals("W/\"blub\"", response.getHeaderField(Headers.HEADER_ETAG));
145     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
146     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
147     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
148     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
149     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
150     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
151     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
152     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
153     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
154
155     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("W/blub", "UTF-8"));
156     Assert.assertEquals("W/\"blub\"", response.getHeaderField(Headers.HEADER_ETAG));
157     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
158     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
159     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
160     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
161     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
162     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
163     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
164     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
165     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
166
167     response = executeRequest("http://localhost/parameter-guessing?n=16&ETag=" + URLEncoder.encode("W/bÄl\"ub", "UTF-8"));
168     Assert.assertEquals("W/\"blub\"", response.getHeaderField(Headers.HEADER_ETAG));
169     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
170     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
171     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
172     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
173     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
174     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
175     date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
176     expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
177     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
178   }
179
180   @Test
181   public void testLastModifiedImplemented() throws Exception {
182
183     log.info("-------- Test: Servlet implements getLastModified()");
184
185     WebResponse response = executeRequest("http://localhost/parameter-guessing?n=16&l=1324162929861");
186     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
187     Assert.assertEquals("Sat, 17 Dec 2011 23:02:09 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
188     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
189     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
190     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
191     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
192     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
193     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
194     long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
195     long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
196     Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
197   }
198
199   @Test
200   public void testCacheControlSet() throws Exception {
201
202     log.info("-------- Test: Servlet sets Header \"Cache-Control\"");
203
204     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
205     StringBuilder uri;
206     WebResponse response;
207     Date date;
208     long expires;
209     Set<String> params;
210     Calendar calendar = Calendar.getInstance();
211     calendar.set(Calendar.MILLISECOND, 0);
212
213     /** max-age=120 */
214     uri = new StringBuilder();
215     uri.append("http://localhost/parameter-guessing");
216     uri.append("?n=16");
217     uri.append("&Cache-Control=");
218     uri.append(URLEncoder.encode("max-age=120", "UTF-8"));
219     response = executeRequest(uri.toString());
220     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
221     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
222     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
223     Assert.assertEquals("max-age=120", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
224     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
225     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
226     date = df.parse(response.getHeaderField(Headers.HEADER_DATE));
227     expires = (date.getTime()/1000l + 120l) * 1000l;
228     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
229     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires, df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
230
231     /** max-age=120, s-max-age=60, private, must-revalidate  */
232     uri = new StringBuilder();
233     uri.append("http://localhost/parameter-guessing");
234     uri.append("?n=16");
235     uri.append("&Cache-Control=");
236     uri.append(URLEncoder.encode("max-age=120, s-max-age=60, must-revalidate", "UTF-8"));
237     response = executeRequest(uri.toString());
238     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
239     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
240     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
241     params = new HashSet<String>();
242     for (String param : response.getHeaderFields(Headers.HEADER_CACHE_CONTROL))
243       for (String part : param.split(","))
244         params.add(part.trim());
245     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"max-age=120\" nicht!", params.contains("max-age=120"));
246     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"s-max-age=60\" nicht!", params.contains("s-max-age=60"));
247     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"must-revalidate\" nicht!", params.contains("must-revalidate"));
248     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
249     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
250     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
251     date = df.parse(response.getHeaderField(Headers.HEADER_DATE));
252     expires = (date.getTime()/1000l + 120l) * 1000l;
253     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires, df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
254
255     /** max-age=120, s-max-age=60, private, must-revalidate, BUT: several other values are set before  */
256     uri = new StringBuilder();
257     uri.append("http://localhost/parameter-guessing");
258     uri.append("?n=16");
259     uri.append("&Cache-Control=");
260     uri.append(URLEncoder.encode("no-store", "UTF-8"));
261     uri.append("&Cache-Control=");
262     uri.append(URLEncoder.encode("max-age=360, s-max-age=600, private", "UTF-8"));
263     uri.append("&Cache-Control=");
264     uri.append(URLEncoder.encode("public", "UTF-8"));
265     uri.append("&Cache-Control=");
266     uri.append(URLEncoder.encode("max-age=120, s-max-age=60, must-revalidate", "UTF-8"));
267     response = executeRequest(uri.toString());
268     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
269     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
270     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
271     params = new HashSet<String>();
272     for (String param : response.getHeaderFields(Headers.HEADER_CACHE_CONTROL))
273       for (String part : param.split(","))
274         params.add(part.trim());
275     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"max-age=120\" nicht!", params.contains("max-age=120"));
276     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"s-max-age=60\" nicht!", params.contains("s-max-age=60"));
277     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"must-revalidate\" nicht!", params.contains("must-revalidate"));
278     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
279     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
280     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
281     date = df.parse(response.getHeaderField(Headers.HEADER_DATE));
282     expires = (date.getTime()/1000l + 120l) * 1000l;
283     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires, df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
284   }
285
286   @Test
287   public void testDateSet() throws Exception {
288
289     log.info("-------- Test: Servlet sets Header \"Date\"");
290
291     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
292     StringBuilder uri;
293     WebResponse response;
294     Date date, expires;
295     Calendar calendar = Calendar.getInstance();
296     calendar.set(Calendar.MILLISECOND, 0);
297
298     /** Date ca NOW -1m */
299     calendar.add(Calendar.MINUTE, -1);
300     date = calendar.getTime();
301     calendar.add(Calendar.MINUTE, 60); /** default max-age=3600 yields 60m! */
302     expires = calendar.getTime();
303     uri = new StringBuilder();
304     uri.append("http://localhost/parameter-guessing");
305     uri.append("?n=16");
306     uri.append("&Date=");
307     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
308     response = executeRequest(uri.toString());
309     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
310     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
311     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
312     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
313     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
314     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
315     Assert.assertEquals("Unerwartetr Wert für den Date-Header!", date.getTime(), df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime());
316     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
317     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
318
319     /** Date ca NOW -1m, BUT: is set to some garbage values before */
320     calendar.add(Calendar.MINUTE, -1);
321     date = calendar.getTime();
322     calendar.add(Calendar.MINUTE, 60); /** default max-age=3600 yields 60m! */
323     expires = calendar.getTime();
324     uri = new StringBuilder();
325     uri.append("http://localhost/parameter-guessing");
326     uri.append("?n=16");
327     uri.append("&Date=");
328     calendar.add(Calendar.MINUTE, 10);
329     uri.append(URLEncoder.encode(df.format(calendar.getTime()), "UTF-8"));
330     uri.append("&Date=");
331     calendar.add(Calendar.HOUR, -2);
332     uri.append(URLEncoder.encode(df.format(calendar.getTime()), "UTF-8"));
333     uri.append("&Date=");
334     calendar.add(Calendar.DATE, 1);
335     uri.append(URLEncoder.encode(df.format(calendar.getTime()), "UTF-8"));
336     uri.append("&Date=");
337     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
338     response = executeRequest(uri.toString());
339     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
340     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
341     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
342     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
343     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
344     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
345     Assert.assertEquals("Unerwartetr Wert für den Date-Header!", date.getTime(), df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime());
346     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
347     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
348   }
349
350   @Test
351   public void testExpiresSet() throws Exception {
352
353     log.info("-------- Test: Servlet sets Header \"Expires\"");
354
355     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
356     StringBuilder uri;
357     WebResponse response;
358     Date date, expires;
359     long age;
360     Calendar calendar = Calendar.getInstance();
361     calendar.set(Calendar.MILLISECOND, 0);
362
363     /** Expires ca. NOW + 10m */
364     calendar.add(Calendar.MINUTE, 10);
365     expires = calendar.getTime();
366     uri = new StringBuilder();
367     uri.append("http://localhost/parameter-guessing");
368     uri.append("?n=16");
369     uri.append("&Expires=");
370     uri.append(URLEncoder.encode(df.format(expires), "UTF-8"));
371     response = executeRequest(uri.toString());
372     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
373     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
374     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
375     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
376     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
377     date = df.parse(response.getHeaderField(Headers.HEADER_DATE));
378     age = (expires.getTime() - date.getTime())/1000l;
379     Assert.assertEquals("max-age=" + age, response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
380     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
381     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
382
383     /** Expires ca. NOW + 10m, BUT: is set to some garbage values before */
384     calendar.add(Calendar.MINUTE, 10);
385     uri = new StringBuilder();
386     uri.append("http://localhost/parameter-guessing");
387     uri.append("?n=16");
388     uri.append("&Expires=");
389     calendar.add(Calendar.MINUTE, 10);
390     uri.append(URLEncoder.encode(df.format(calendar.getTime()), "UTF-8"));
391     uri.append("&Expires=");
392     calendar.add(Calendar.HOUR, -2);
393     uri.append(URLEncoder.encode(df.format(calendar.getTime()), "UTF-8"));
394     uri.append("&Expires=");
395     calendar.add(Calendar.DATE, 1);
396     uri.append(URLEncoder.encode(df.format(calendar.getTime()), "UTF-8"));
397     uri.append("&Expires=");
398     uri.append(URLEncoder.encode(df.format(expires), "UTF-8"));
399     response = executeRequest(uri.toString());
400     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
401     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
402     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
403     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
404     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
405     date = df.parse(response.getHeaderField(Headers.HEADER_DATE));
406     age = (expires.getTime() - date.getTime())/1000l;
407     Assert.assertEquals("max-age=" + age, response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
408     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
409     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
410   }
411
412   @Test
413   public void testDateAndExpiresSet() throws Exception {
414
415     log.info("-------- Test: Servlet sets Header's \"Date\" and \"Expires\"");
416
417     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
418     StringBuilder uri;
419     WebResponse response;
420     Date date, expires, garbage;
421     Calendar calendar = Calendar.getInstance();
422     calendar.set(Calendar.MILLISECOND, 0);
423
424     /** Expires = Date + 30m */
425     date = calendar.getTime();
426     calendar.add(Calendar.MINUTE, 30);
427     expires = calendar.getTime();
428     uri = new StringBuilder();
429     uri.append("http://localhost/parameter-guessing");
430     uri.append("?n=16");
431     uri.append("&Date=");
432     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
433     uri.append("&Expires=");
434     uri.append(URLEncoder.encode(df.format(expires), "UTF-8"));
435     response = executeRequest(uri.toString());
436     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
437     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
438     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
439     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
440     Assert.assertEquals("max-age=1800", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
441     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
442     Assert.assertEquals("Unerwartetr Wert für den Date-Header!", date.getTime(), df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime());
443     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
444     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
445
446     /** Expires = Date + 30m, BUT: Date is set to Date - 2h first and Expires to Date */
447     date = calendar.getTime();
448     calendar.add(Calendar.MINUTE, 30);
449     expires = calendar.getTime();
450     calendar.add(Calendar.HOUR, -2);
451     garbage = calendar.getTime();
452     uri = new StringBuilder();
453     uri.append("http://localhost/parameter-guessing");
454     uri.append("?n=16");
455     uri.append("&Date=");
456     uri.append(URLEncoder.encode(df.format(garbage), "UTF-8"));
457     uri.append("&Expires=");
458     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
459     uri.append("&Date=");
460     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
461     uri.append("&Expires=");
462     uri.append(URLEncoder.encode(df.format(expires), "UTF-8"));
463     response = executeRequest(uri.toString());
464     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
465     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
466     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
467     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
468     Assert.assertEquals("max-age=1800", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
469     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
470     Assert.assertEquals("Unerwartetr Wert für den Date-Header!", date.getTime(), df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime());
471     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
472     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
473
474     /** Expires = Date - 1h --> will be ignored! */
475     date = calendar.getTime();
476     calendar.add(Calendar.MINUTE, -60);
477     garbage = calendar.getTime();
478     calendar.setTime(date);
479     calendar.add(Calendar.MINUTE, 60); /** default max-age=3600 yields 60m! */
480     expires = calendar.getTime();
481     uri = new StringBuilder();
482     uri.append("http://localhost/parameter-guessing");
483     uri.append("?n=16");
484     uri.append("&Date=");
485     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
486     uri.append("&Expires=");
487     uri.append(URLEncoder.encode(df.format(garbage), "UTF-8"));
488     response = executeRequest(uri.toString());
489     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
490     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
491     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
492     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
493     Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
494     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
495     Assert.assertEquals("Unerwartetr Wert für den Date-Header!", date.getTime(), df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime());
496     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
497     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
498   }
499
500   @Test
501   public void testCacheControlDateAndExpiresSet() throws Exception {
502
503     log.info("-------- Test: Servlet sets Header's \"Cache-Control\", \"Date\" and \"Expires\"");
504
505     SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
506     StringBuilder uri;
507     WebResponse response;
508     Date date, expires, expected;
509     Set<String> params;
510     Calendar calendar = Calendar.getInstance();
511     calendar.set(Calendar.MILLISECOND, 0);
512
513     /** Expires = Date + 30m, Cache-Control: must-revalidate, no-store */
514     date = calendar.getTime();
515     calendar.add(Calendar.MINUTE, 30);
516     expires = calendar.getTime();
517     uri = new StringBuilder();
518     uri.append("http://localhost/parameter-guessing");
519     uri.append("?n=16");
520     uri.append("&Cache-Control=");
521     uri.append(URLEncoder.encode("must-revalidate, no-store", "UTF-8"));
522     uri.append("&Date=");
523     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
524     uri.append("&Expires=");
525     uri.append(URLEncoder.encode(df.format(expires), "UTF-8"));
526     response = executeRequest(uri.toString());
527     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
528     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
529     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
530     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
531     params = new HashSet<String>();
532     for (String param : response.getHeaderFields(Headers.HEADER_CACHE_CONTROL))
533       for (String part : param.split(","))
534         params.add(part.trim());
535     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"max-age=1800\" nicht!", params.contains("max-age=1800"));
536     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"must-revalidate\" nicht!", params.contains("must-revalidate"));
537     Assert.assertTrue(response.getHeaderField(Headers.HEADER_CACHE_CONTROL) + " enthält \"no-store\" nicht!", params.contains("no-store"));
538     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
539     Assert.assertEquals("Unerwartetr Wert für den Date-Header!", date.getTime(), df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime());
540     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
541     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expires.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
542
543     /** Expires = Date + 30m, BUT: max-age is set to 600s */
544     date = calendar.getTime();
545     calendar.add(Calendar.MINUTE, 10);
546     expected = calendar.getTime();
547     calendar.add(Calendar.MINUTE, 20);
548     expires = calendar.getTime();
549     uri = new StringBuilder();
550     uri.append("http://localhost/parameter-guessing");
551     uri.append("?n=16");
552     uri.append("&Date=");
553     uri.append(URLEncoder.encode(df.format(date), "UTF-8"));
554     uri.append("&Expires=");
555     uri.append(URLEncoder.encode(df.format(expires), "UTF-8"));
556     uri.append("&Cache-Control=");
557     uri.append(URLEncoder.encode("max-age=600", "UTF-8"));
558     response = executeRequest(uri.toString());
559     Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
560     Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
561     Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
562     Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
563     Assert.assertEquals("max-age=600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
564     Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
565     Assert.assertEquals("Unerwartetr Wert für den Date-Header!", date.getTime(), df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime());
566     Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
567     Assert.assertEquals("Unerwartetr Wert für den Expires-Header!", expected.getTime(), df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime());
568   }
569 }