1 package de.halbekunst.juplo.cachecontrol;
3 import com.meterware.httpunit.WebResponse;
4 import java.net.URLEncoder;
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;
21 @RunWith(SpringJUnit4ClassRunner.class)
22 @ContextConfiguration(locations = {
23 "classpath:/config.xml"
25 public class RequestSizeTest extends HttpTestCase {
26 private final static Logger log = LoggerFactory.getLogger(RequestSizeTest.class);
30 public void testSimpleRequestWithGzip() throws Exception {
32 log.info("-------- Test: gzipped simple request");
34 client.getClientProperties().setAcceptGzip(true);
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));
42 Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
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);
57 public void testSimpleRequestWithoutGzip() throws Exception {
59 log.info("-------- Test: uncompressed simple request");
61 client.getClientProperties().setAcceptGzip(false);
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);
81 public void testForwardWithGzip() throws Exception {
83 log.info("-------- Test: gzipped request with forward");
85 client.getClientProperties().setAcceptGzip(true);
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");
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));
99 Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
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);
114 public void testForwardWithoutGzip() throws Exception {
116 log.info("-------- Test: uncompressed request with forward");
118 client.getClientProperties().setAcceptGzip(false);
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");
127 uri.append(URLEncoder.encode("/forwarded?n=" + i*128, "UTF-8"));
129 WebResponse response = executeRequest(uri.toString());
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);
144 catch (IllegalStateException e) {
146 log.debug("Expected error while forwarding after {} bytes written: {}", i*128, e.getMessage());
148 Assert.fail("Unexpected error while forwarding after " + i*128 + " bytes written: " + e.getMessage());
154 public void testIncludeWithGzip() throws Exception {
156 log.info("-------- Test: gzipped request with includes");
158 client.getClientProperties().setAcceptGzip(true);
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");
166 for (int j=0; j < i%4+1; j++) {
168 uri.append(URLEncoder.encode("/included?n=" + i*32*(4-j), "UTF-8"));
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));
174 Assert.assertNull(response.getHeaderField(Headers.HEADER_CONTENT_ENCODING));
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);
189 public void testIncludeWithoutGzip() throws Exception {
191 log.info("-------- Test: uncompressed request with includes");
193 client.getClientProperties().setAcceptGzip(false);
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");
201 for (int j=0; j < i%4+1; j++) {
203 uri.append(URLEncoder.encode("/included?n=" + i*32*(4-j), "UTF-8"));
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);