1   /**
2    *  Copyright 2003-2007 Greg Luck
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
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   * Test cases for the Caching filter and Gzip.
30   *
31   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
32   * @version $Id: GzipFilterTest.java 512 2007-07-10 09:18:45Z gregluck $
33   */
34  public class GzipFilterTest extends AbstractWebTest {
35  
36      /**
37       * Fetch NoFiltersPage.jsp, which is excluded from all filters and check it is not gzipped.
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       * Tests that a page which is storeGzipped is gzipped when the user agent accepts gzip encoding
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          //Check that we are dealing with Cyrillic characters ok
73          assertTrue(response.getText().indexOf("&#8593;") != -1);
74          //Check non ascii symbol
75          assertTrue(response.getText().indexOf("&#1052;") != -1);
76      }
77  
78      /**
79       * A 0 length body should give a 0 length gzip body and content length
80       * <p/>
81       * Manual test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/empty.html
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      * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED.
102      * In this case there should not be a body but there is. Orion seems to kill the body
103      * after is has left the Servlet filter chain. To avoid wget going into an inifinite
104      * retry loop, and presumably some other web clients, the content length should be 0
105      * and the body 0.
106      * <p/>
107      * Manual test: wget -d --server-response --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NOT_MODIFIED.jsp
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      * JSPs and Servlets can send bodies when the response is SC_NOT_MODIFIED.
127      * In this case there should not be a body but there is. Orion seems to kill the body
128      * after is has left the Servlet filter chain. To avoid wget going into an inifinite
129      * retry loop, and presumably some other web clients, the content length should be 0
130      * and the body 0.
131      * <p/>
132      * Manual test: wget -d --server-response --timestamping --header='If-modified-Since: Fri, 13 May 3006 23:54:18 GMT' --header='Accept-Encoding: gzip' http://localhost:9080/empty_gzip/SC_NO_CONTENT.jsp
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      * Tests that a page which is storeGzipped is not gzipped when the user agent does not accept gzip encoding
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      * Tests that hitting a page with a non gzip browser then a gzip browser causes the
168      * right behaviours.
169      */
170     public void testNonGzipThenGzipBrowserHomePage() throws Exception {
171         testNotGzippedWhenNotAcceptEncodingHomePage();
172         testGzippedWhenAcceptEncodingHomePage();
173     }
174 
175         /**
176      * When the servlet container generates a 404 page not found, we want to pass
177      * it through without caching and without adding anything to it.
178      * <p/>
179      * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/PageNotFoundGzip.jsp
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      * When the servlet container generates a 404 page not found, we want to pass
197      * it through without caching and without adding anything to it.
198      * <p/>
199      * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/SendRedirectGzip.jsp
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         //httpclient follows redirects, so gets the home page.
209         assertEquals(HttpURLConnection.HTTP_OK, responseCode);
210         String responseBody = httpMethod.getResponseBodyAsString();
211         assertNotNull(responseBody);
212         assertNull(httpMethod.getResponseHeader("Content-Encoding"));
213     }
214 
215     /**
216      * When the servlet container forwards to a page does it work?
217      * <p/>
218      * Manual Test: wget -d --server-response --header='Accept-Encoding: gzip'  http://localhost:9080/non_ok/ForwardFromGzip.jsp
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         //httpclient follows redirects, so gets the home page.
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