View Javadoc

1   package it.com.atlassian.gzipfilter;
2   
3   import junit.framework.TestCase;
4   
5   import java.io.IOException;
6   
7   import org.apache.commons.httpclient.HttpClient;
8   import org.apache.commons.httpclient.HttpMethod;
9   import org.apache.commons.httpclient.methods.GetMethod;
10  import org.junit.Before;
11  import org.junit.Test;
12  import static org.junit.Assert.*;
13  
14  public class TestResponseTypes
15  {
16      private HttpMethod method;
17  
18      @Before
19      public void setUp() throws Exception
20      {
21          method = null;
22      }
23  
24      @Test
25      public void test304HasNoBody() throws IOException
26      {
27          int i = executeMethodAcceptingGzip("304.html");
28          assertEquals("Should return 304 - not modified", 304, i);
29          assertEmptyBody(method);
30      }
31  
32      @Test
33      public void testRedirect() throws IOException
34      {
35          HttpClient client = new HttpClient();
36          HttpMethod method = new GetMethod(IntegrationTestUtils.URL + "redirect.html");
37          method.addRequestHeader(IntegrationTestUtils.GZIP_ACCEPT_HEADER);
38          method.setFollowRedirects(false);
39  
40          // test mime-type of image/png is not gzipped
41          int i = client.executeMethod(method);
42          assertEquals("Should return 302 - redirect", 302, i);
43  
44          // According to the HTTP spec, redirect should have a body:
45          //   http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
46          // "Unless the request method was HEAD, the entity of the response
47          // SHOULD contain a short hypertext note with a hyperlink to the new URI(s)"
48  //        assertEmptyBody(method);
49      }
50  
51      @Test
52      public void testUrlRewriteClassesAreNotFound() throws IOException
53      {
54          int i = executeMethodAcceptingGzip("classes.html");
55          assertEquals("Should return 200 if classes can not be found", 200, i);
56      }
57  
58  
59  
60      private static void assertEmptyBody(HttpMethod method) throws IOException
61      {
62          if (method.getResponseBody() != null && method.getResponseBody().length > 0)
63              fail("Content should be null, but was '" + new String(method.getResponseBody(), "UTF-8") + "'");
64      }
65  
66      private int executeMethodAcceptingGzip(String s) throws IOException
67      {
68          HttpClient client = new HttpClient();
69          method = new GetMethod(IntegrationTestUtils.URL + s);
70          method.addRequestHeader(IntegrationTestUtils.GZIP_ACCEPT_HEADER);
71          return client.executeMethod(method);
72      }
73  }