1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ehcache.constructs.web.filter;
18
19 import com.meterware.httpunit.WebConversation;
20 import com.meterware.httpunit.WebResponse;
21 import net.sf.ehcache.constructs.web.AbstractWebTest;
22 import org.apache.commons.httpclient.HttpClient;
23 import org.apache.commons.httpclient.HttpMethod;
24 import org.apache.commons.httpclient.methods.GetMethod;
25
26 import java.net.HttpURLConnection;
27
28
29
30
31
32
33
34 public class GzipFilterTest extends AbstractWebTest {
35
36
37
38
39 public void testNegativeGzip() throws Exception {
40 WebConversation client = createWebConversation(true);
41 client.getClientProperties().setAcceptGzip(true);
42 String url = buildUrl("/NoFiltersPage.jsp");
43 WebResponse response = client.getResponse(url);
44
45 assertNotNull(response);
46 assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
47
48 String responseURL = response.getURL().toString();
49 assertEquals(url, responseURL);
50 assertNotSame("gzip", response.getHeaderField("Content-Encoding"));
51
52
53 }
54
55
56
57
58
59 public void testGzippedWhenAcceptEncodingHomePage() throws Exception {
60 WebConversation client = createWebConversation(true);
61 client.getClientProperties().setAcceptGzip(true);
62 String url = buildUrl("/GzipOnlyPage.jsp");
63 WebResponse response = client.getResponse(url);
64
65 assertNotNull(response);
66 assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
67
68 String responseURL = response.getURL().toString();
69 assertEquals(url, responseURL);
70 assertEquals("gzip", response.getHeaderField("Content-Encoding"));
71
72
73 assertTrue(response.getText().indexOf("↑") != -1);
74
75 assertTrue(response.getText().indexOf("М") != -1);
76 }
77
78
79
80
81
82
83 public void testZeroLengthHTML() throws Exception {
84
85 String url = "http://localhost:9080/empty_gzip/empty.html";
86 HttpClient httpClient = new HttpClient();
87 HttpMethod httpMethod = new GetMethod(url);
88 httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
89 httpMethod.addRequestHeader("Accept-Encoding", "gzip");
90 int responseCode = httpClient.executeMethod(httpMethod);
91 assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode);
92 byte[] responseBody = httpMethod.getResponseBody();
93 assertEquals(null, responseBody);
94 assertNull(httpMethod.getResponseHeader("Content-Encoding"));
95 checkNullOrZeroContentLength(httpMethod);
96 }
97
98
99
100
101
102
103
104
105
106
107
108
109 public void testNotModifiedJSPGzipFilter() throws Exception {
110
111 String url = "http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp";
112 HttpClient httpClient = new HttpClient();
113 HttpMethod httpMethod = new GetMethod(url);
114 httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
115 httpMethod.addRequestHeader("Accept-Encoding", "gzip");
116 int responseCode = httpClient.executeMethod(httpMethod);
117 assertEquals(HttpURLConnection.HTTP_NOT_MODIFIED, responseCode);
118 byte[] responseBody = httpMethod.getResponseBody();
119 assertEquals(null, responseBody);
120 assertNull(httpMethod.getResponseHeader("Content-Encoding"));
121 assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
122 checkNullOrZeroContentLength(httpMethod);
123 }
124
125
126
127
128
129
130
131
132
133
134 public void testNoContentJSPGzipFilter() throws Exception {
135
136 String url = "http://localhost:9080/empty_gzip/SC_NO_CONTENT.jsp";
137 HttpClient httpClient = new HttpClient();
138 HttpMethod httpMethod = new GetMethod(url);
139 httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
140 httpMethod.addRequestHeader("Accept-Encoding", "gzip");
141 int responseCode = httpClient.executeMethod(httpMethod);
142 assertEquals(HttpURLConnection.HTTP_NO_CONTENT, responseCode);
143 byte[] responseBody = httpMethod.getResponseBody();
144 assertEquals(null, responseBody);
145 assertNull(httpMethod.getResponseHeader("Content-Encoding"));
146 assertNotNull(httpMethod.getResponseHeader("Last-Modified").getValue());
147 checkNullOrZeroContentLength(httpMethod);
148 }
149
150
151
152
153 public void testNotGzippedWhenNotAcceptEncodingHomePage() throws Exception {
154 WebConversation client = createWebConversation(false);
155 String url = buildUrl("/GzipOnlyPage.jsp");
156 WebResponse response = client.getResponse(url);
157
158 assertNotNull(response);
159 assertEquals(HttpURLConnection.HTTP_OK, response.getResponseCode());
160
161 String responseURL = response.getURL().toString();
162 assertEquals(url, responseURL);
163 assertFalse("gzip".equals(response.getHeaderField("Content-Encoding")));
164 }
165
166
167
168
169
170 public void testNonGzipThenGzipBrowserHomePage() throws Exception {
171 testNotGzippedWhenNotAcceptEncodingHomePage();
172 testGzippedWhenAcceptEncodingHomePage();
173 }
174
175
176
177
178
179
180
181 public void testNotFound() throws Exception {
182
183 String url = "http://localhost:9080/non_ok/PageNotFoundGzip.jsp";
184 HttpClient httpClient = new HttpClient();
185 HttpMethod httpMethod = new GetMethod(url);
186 httpMethod.addRequestHeader("If-modified-Since", "Fri, 13 May 3006 23:54:18 GMT");
187 httpMethod.addRequestHeader("Accept-Encoding", "gzip");
188 int responseCode = httpClient.executeMethod(httpMethod);
189 assertEquals(HttpURLConnection.HTTP_NOT_FOUND, responseCode);
190 String responseBody = httpMethod.getResponseBodyAsString();
191 assertNotNull(responseBody);
192 assertNull(httpMethod.getResponseHeader("Content-Encoding"));
193 }
194
195
196
197
198
199
200
201 public void testRedirect() throws Exception {
202
203 String url = "http://localhost:9080/non_ok/SendRedirectGzip.jsp";
204 HttpClient httpClient = new HttpClient();
205 HttpMethod httpMethod = new GetMethod(url);
206 httpMethod.addRequestHeader("Accept-Encoding", "gzip");
207 int responseCode = httpClient.executeMethod(httpMethod);
208
209 assertEquals(HttpURLConnection.HTTP_OK, responseCode);
210 String responseBody = httpMethod.getResponseBodyAsString();
211 assertNotNull(responseBody);
212 assertNull(httpMethod.getResponseHeader("Content-Encoding"));
213 }
214
215
216
217
218
219
220 public void testForward() throws Exception {
221
222 String url = "http://localhost:9080/non_ok/ForwardFromGzip.jsp";
223 HttpClient httpClient = new HttpClient();
224 HttpMethod httpMethod = new GetMethod(url);
225 httpMethod.addRequestHeader("Accept-Encoding", "gzip");
226 int responseCode = httpClient.executeMethod(httpMethod);
227
228 assertEquals(HttpURLConnection.HTTP_OK, responseCode);
229 String responseBody = httpMethod.getResponseBodyAsString();
230 assertNotNull(responseBody);
231 assertEquals("gzip", httpMethod.getResponseHeader("Content-Encoding").getValue());
232 }
233
234 }
235