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.WebResponse;
20  import net.sf.ehcache.constructs.web.AbstractWebTest;
21  import net.sf.ehcache.constructs.web.PageInfo;
22  import org.apache.commons.httpclient.Header;
23  import org.apache.commons.httpclient.HttpClient;
24  import org.apache.commons.httpclient.HttpMethod;
25  import org.apache.commons.httpclient.methods.GetMethod;
26  
27  /**
28   * @author <a href="mailto:gluck@thoughtworks.com">Greg Luck</a>
29   * @version $Id: PageFragmentCachingFilterTest.java 512 2007-07-10 09:18:45Z gregluck $
30   */
31  public class PageFragmentCachingFilterTest extends AbstractWebTest {
32  
33      /**
34       * Tests that we can get the page successfully
35       */
36      public void testBasic() throws Exception {
37          WebResponse response = getResponseFromAcceptGzipRequest("/include/Footer.jsp");
38          assertResponseOk(response);
39          assertIncludeHeadersSane(response);
40          assertPageNotBlank(response);
41      }
42  
43      /**
44       * Tests that a page which is in the cache fragment filter pattern is cached.
45       */
46      public void testCachedPageFragmentIsCached() throws Exception {
47          WebResponse response = getResponseFromAcceptGzipRequest("/fragment/CachedFragment.jsp");
48          assertResponseOk(response);
49          assertIncludeHeadersSane(response);
50          assertPageNotBlank(response);
51          String firstResponse = response.getText();
52  
53          response = getResponseFromAcceptGzipRequest("/fragment/CachedFragment.jsp");
54          assertResponseOk(response);
55          assertIncludeHeadersSane(response);
56          assertPageNotBlank(response);
57          String secondResponse = response.getText();
58  
59          assertEquals(firstResponse, secondResponse);
60      }
61  
62      /**
63       * Tests that a page which is not storeGzipped is not gzipped when the user agent accepts gzip encoding
64       */
65      public void testNotGzippedWhenAcceptEncodingPageFragment() throws Exception {
66          HttpClient httpClient = new HttpClient();
67          HttpMethod httpMethod = new GetMethod(buildUrl("/include/Footer.jsp"));
68          //httpMethod.setRequestHeader(new Header("Accept-encoding", "gzip"));
69          httpClient.executeMethod(httpMethod);
70          byte[] responseBody = httpMethod.getResponseBody();
71          assertFalse(PageInfo.isGzipped(responseBody));
72          assertNotSame("gzip", httpMethod.getResponseHeader("Accept-encoding"));
73      }
74  
75      /**
76       * Tests that a page which is not storeGzipped is not gzipped when the user agent does not accept gzip encoding
77       */
78      public void testNotGzippedWhenNotAcceptEncodingPageFragment() throws Exception {
79          HttpClient httpClient = new HttpClient();
80          HttpMethod httpMethod = new GetMethod(buildUrl("/include/Footer.jsp"));
81          httpMethod.setRequestHeader(new Header("Accept-encoding", "gzip"));
82          httpClient.executeMethod(httpMethod);
83          byte[] responseBody = httpMethod.getResponseBody();
84          assertFalse(PageInfo.isGzipped(responseBody));
85          assertNotSame("gzip", httpMethod.getResponseHeader("Accept-encoding"));
86      }
87  
88      /**
89       * Checks that a cached fragment contained in a non-cached out page works.
90       *
91       * @throws Exception
92       */
93      public void testFromJSPInclude() throws Exception {
94          WebResponse response = getResponseFromAcceptGzipRequest("/fragment/NonCachedPageIncludingCachedFragment.jsp");
95          assertResponseOk(response);
96          assertHeadersSane(response);
97          assertPageNotBlank(response);
98          assertTrue(response.getText().indexOf("</html>") != -1);
99  
100     }
101 
102 
103 }