Verwirrenden Variable-Name "controller" in CacheControl in "handle" geƤndert
[percentcodec] / cachecontrol / src / test / java / de / halbekunst / juplo / cachecontrol / RequestSizeTest.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.Locale;
9 import org.junit.Assert;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.test.context.ContextConfiguration;
13 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
14
15
16
17 /**
18  *
19  * @author kai
20  */
21 @RunWith(SpringJUnit4ClassRunner.class)
22 @ContextConfiguration(locations = {
23   "classpath:/config.xml"
24 })
25 public class RequestSizeTest extends HttpTestCase {
26   private final static Logger log = LoggerFactory.getLogger(RequestSizeTest.class);
27
28
29   @Test
30   public void testSimpleRequestWithGzip() throws Exception {
31
32     log.info("-------- Test: gzipped simple request");
33
34     client.getClientProperties().setAcceptGzip(true);
35
36     for (int i=0; i<33; i++) {
37       /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
38       WebResponse response = executeRequest("http://localhost/request-size?n=" + i*128);
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       if (i==0)
42         Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
43       else
44         Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
45       Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
46       Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
47       Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
48       Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
49       SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
50       long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
51       long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
52       Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
53     }
54   }
55
56   @Test
57   public void testSimpleRequestWithoutGzip() throws Exception {
58
59     log.info("-------- Test: uncompressed simple request");
60
61     client.getClientProperties().setAcceptGzip(false);
62
63     for (int i=0; i<33; i++) {
64       /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
65       WebResponse response = executeRequest("http://localhost/request-size?n=" + i*128);
66       Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
67       Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
68       Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
69       Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
70       Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
71       Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
72       Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
73       SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
74       long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
75       long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
76       Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
77     }
78   }
79
80   @Test
81   public void testForwardWithGzip() throws Exception {
82
83     log.info("-------- Test: gzipped request with forward");
84
85     client.getClientProperties().setAcceptGzip(true);
86
87     for (int i=0; i<33; i++) {
88       /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
89       StringBuilder uri = new StringBuilder();
90       uri.append("http://localhost/request-size");
91       uri.append("?n=");
92       uri.append(i*128);
93       uri.append("&f=");
94       uri.append(URLEncoder.encode("/forwarded?n=" + i*128, "UTF-8"));
95       WebResponse response = executeRequest(uri.toString());
96       Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
97       Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
98       if (i==0)
99         Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
100       else
101         Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
102       Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
103       Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
104       Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
105       Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
106       SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
107       long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
108       long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
109       Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
110     }
111   }
112
113   @Test
114   public void testForwardWithoutGzip() throws Exception {
115
116     log.info("-------- Test: uncompressed request with forward");
117
118     client.getClientProperties().setAcceptGzip(false);
119
120     for (int i=0; i<33; i++) {
121       /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
122       StringBuilder uri = new StringBuilder();
123       uri.append("http://localhost/request-size");
124       uri.append("?n=");
125       uri.append(i*128);
126       uri.append("&f=");
127       uri.append(URLEncoder.encode("/forwarded?n=" + i*128, "UTF-8"));
128       try {
129         WebResponse response = executeRequest(uri.toString());
130         if (i*128 > 2048)
131           Assert.fail("Error expected while forwarding after " + i*128 + " bytes written!");
132         Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
133         Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
134         Assert.assertNull(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         SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
140         long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
141         long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
142         Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
143       }
144       catch (IllegalStateException e) {
145         if (i*128 > 2048)
146           log.debug("Expected error while forwarding after {} bytes written: {}", i*128, e.getMessage());
147         else
148           Assert.fail("Unexpected error while forwarding after " + i*128 + " bytes written: " + e.getMessage());
149       }
150     }
151   }
152
153   @Test
154   public void testIncludeWithGzip() throws Exception {
155
156     log.info("-------- Test: gzipped request with includes");
157
158     client.getClientProperties().setAcceptGzip(true);
159
160     for (int i=0; i<33; i++) {
161       /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
162       StringBuilder uri = new StringBuilder();
163       uri.append("http://localhost/request-size");
164       uri.append("?n=");
165       uri.append(i*128);
166       for (int j=0; j < i%4+1; j++) {
167         uri.append("&i=");
168         uri.append(URLEncoder.encode("/included?n=" + i*32*(4-j), "UTF-8"));
169       }
170       WebResponse response = executeRequest(uri.toString());
171       Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
172       Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
173       if (i==0)
174         Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
175       else
176         Assert.assertEquals("gzip", response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
177       Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
178       Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
179       Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
180       Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
181       SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
182       long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
183       long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
184       Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
185     }
186   }
187
188   @Test
189   public void testIncludeWithoutGzip() throws Exception {
190
191     log.info("-------- Test: uncompressed request with includes");
192
193     client.getClientProperties().setAcceptGzip(false);
194
195     for (int i=0; i<33; i++) {
196       /** 33 requests ranging from 0 B to 4 KB - response ist buffered up to 1 KB */
197       StringBuilder uri = new StringBuilder();
198       uri.append("http://localhost/request-size");
199       uri.append("?n=");
200       uri.append(i*128);
201       for (int j=0; j < i%4+1; j++) {
202         uri.append("&i=");
203         uri.append(URLEncoder.encode("/included?n=" + i*32*(4-j), "UTF-8"));
204       }
205       WebResponse response = executeRequest(uri.toString());
206       Assert.assertEquals("W/\"Hallo Welt!\"", response.getHeaderField(Headers.HEADER_ETAG));
207       Assert.assertEquals("Thu, 01 Jan 1970 00:00:00 GMT", response.getHeaderField(Headers.HEADER_LAST_MODIFIED));
208       Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
209       Assert.assertEquals("max-age=3600", response.getHeaderField(Headers.HEADER_CACHE_CONTROL));
210       Assert.assertEquals("text/plain; charset=iso-8859-1", response.getHeaderField(Headers.HEADER_CONTENT_TYPE));
211       Assert.assertNotNull("Date-Header was not set!", response.getHeaderField(Headers.HEADER_DATE));
212       Assert.assertNotNull("Expires-Header was not set!", response.getHeaderField(Headers.HEADER_EXPIRES));
213       SimpleDateFormat df = new SimpleDateFormat(Headers.RFC_1123_DATE_FORMAT, Locale.US);
214       long date = df.parse(response.getHeaderField(Headers.HEADER_DATE)).getTime();
215       long expires = df.parse(response.getHeaderField(Headers.HEADER_EXPIRES)).getTime();
216       Assert.assertTrue("Expires-Header passt nicht zum Date-Header! Unterschied: " + (expires-date)/1000 + " Sekunden.", date + 3600000 == expires);
217     }
218   }
219 }