View Javadoc

1   package it.com.atlassian.gzipfilter;
2   
3   import org.apache.commons.httpclient.Header;
4   import org.apache.commons.httpclient.HttpClient;
5   import org.apache.commons.httpclient.HttpMethod;
6   import org.apache.commons.httpclient.methods.GetMethod;
7   import org.junit.Ignore;
8   
9   import java.io.IOException;
10  
11  import static org.junit.Assert.assertEquals;
12  import static org.junit.Assert.fail;
13  
14  /**
15   * An utility class to facilitate testing
16   * abstract to prevent junit from running
17   */
18  public abstract class IntegrationTestUtils
19  {
20      static final String HTTP_PORT = System.getProperty("http.port", "8080");
21      static final String URL = "http://localhost:" + HTTP_PORT + "/test-webapp1/";
22  
23      static final Header GZIP_RESPONSE_HEADER = new Header("Content-Encoding", "gzip");
24      static final Header GZIP_ACCEPT_HEADER = new Header("Accept-Encoding", "gzip");
25  
26      static HttpMethod assertPathGzipped(String path, String userAgent, boolean shouldGzip) throws IOException
27      {
28          HttpClient client = new HttpClient();
29          HttpMethod method = new GetMethod(IntegrationTestUtils.URL + path);
30          method.addRequestHeader(IntegrationTestUtils.GZIP_ACCEPT_HEADER);
31          if (userAgent != null)
32              method.addRequestHeader("User-Agent", userAgent);
33  
34          int i = client.executeMethod(method);
35          assertEquals("Should return 200 - success", 200, i);
36          if (shouldGzip)
37              assertGzipped(method);
38          else
39              assertNonGzipped(method);
40          return method;
41      }
42  
43      static void assertGzipped(HttpMethod method)
44      {
45          assertEquals("Should return gzipped content", IntegrationTestUtils.GZIP_RESPONSE_HEADER, method.getResponseHeader("Content-Encoding"));
46      }
47  
48      static void assertNonGzipped(HttpMethod method)
49      {
50          Header responseHeader = method.getResponseHeader("Content-Encoding");
51          if (responseHeader == null)
52              return;
53          else if (responseHeader.getValue() == null)
54              return;
55          else if (responseHeader.getValue().length() == 0) {
56              fail("Content-Encoding header can't be empty");
57          }
58          fail("ReponseHeader should be empty but was " + responseHeader.getValue());
59      }
60  }