View Javadoc

1   package it.com.atlassian.gzipfilter;
2   
3   import com.atlassian.gzipfilter.test.web.UTF8Servlet;
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.Test;
8   import static org.junit.Assert.*;
9   
10  import java.io.ByteArrayInputStream;
11  import java.io.ByteArrayOutputStream;
12  import java.io.IOException;
13  import java.net.URLEncoder;
14  import java.util.Arrays;
15  import java.util.zip.GZIPInputStream;
16  import java.util.zip.GZIPOutputStream;
17  
18  public class TestMimeTypeAndBrowserHandling
19  {
20      @Test
21      public void testWebappDoesNotGzipWhenClientDoesNotSupportIt() throws IOException
22      {
23          HttpClient client = new HttpClient();
24          HttpMethod method = new GetMethod(IntegrationTestUtils.URL + "test.html");
25  
26          int i = client.executeMethod(method);
27          assertEquals("Should return 200 - success", 200, i);
28          IntegrationTestUtils.assertNonGzipped(method);
29      }
30  
31      @Test
32      public void testWebappDoesGzipAndContentIsCorrect() throws IOException
33      {
34          HttpClient client = new HttpClient();
35          HttpMethod method = new GetMethod(IntegrationTestUtils.URL + "test.html");
36          method.addRequestHeader(IntegrationTestUtils.GZIP_ACCEPT_HEADER);
37          int i = client.executeMethod(method);
38          assertEquals("Should return 200 - success", 200, i);
39          IntegrationTestUtils.assertGzipped(method);
40          assertTrue("Content does not match." +
41                          "Should be '" + new String(gzipContent("<h1>Test Servlet</h1>"), "UTF-8")
42                          + "' but was '" + new String(method.getResponseBody(), "UTF-8") + "'",
43                  Arrays.equals(gzipContent("<h1>Test Servlet</h1>"), method.getResponseBody()));
44      }
45  
46      @Test
47      public void testWebappOnlyGzipsCertainMimeTypes() throws IOException
48      {
49          // test mime-types that should be gzipped
50          assertGzipped("text/plain", true);
51          assertGzipped("text/html", true);
52          assertGzipped("application/javascript", true);
53          assertGzipped("application/xhtml+xml", true);
54          assertGzipped("application/xml", true);
55  
56          // test mime-types that shouldn't be gzipped
57          assertGzipped("application/octet-stream", false);
58          assertGzipped("image/png", false);
59          assertGzipped("image/jpeg", false);
60          assertGzipped("image/jpg", false);
61      }
62  
63      @Test
64      public void testWebappOnlyGzipsCertainMimeTypesForCertainBrowsers() throws IOException
65      {
66          final String IE_6 = "Mozilla/4.0 (compatible; MSIE 6.0b; Windows NT 5.1)";
67          final String FIREFOX_3 = "Mozilla/6.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:2.0.0.0) Gecko/20061028 Firefox/3.0";
68  
69          assertGzipped("text/css", IE_6, false);
70          assertGzipped("text/html", IE_6, false);
71          assertGzipped("text/javascript", IE_6, false);
72  
73          assertGzipped("text/css", FIREFOX_3, true);
74          assertGzipped("text/html", FIREFOX_3, true);
75          assertGzipped("text/javascript", FIREFOX_3, true);
76      }
77  
78      @Test
79      public void testUTF8() throws IOException
80      {
81          HttpClient client = new HttpClient();
82          HttpMethod method = new GetMethod(IntegrationTestUtils.URL + "utf8.html");
83          method.addRequestHeader(IntegrationTestUtils.GZIP_ACCEPT_HEADER);
84          int i = client.executeMethod(method);
85          assertEquals("Should return 200 - success", 200, i);
86          IntegrationTestUtils.assertGzipped(method);
87          String expectedText = "<h1>Test Servlet: '" + UTF8Servlet.UTF8_TEXT + "' </h1>";
88          assertTrue("Content does not match.  Should be '" + expectedText + "' but was '" + gunzipContent(method.getResponseBody(), "UTF-8") + "'",
89                  Arrays.equals(gzipContent(expectedText), method.getResponseBody()));
90      }
91  
92      private void assertGzipped(String mimeType, boolean shouldGzip) throws IOException
93      {
94          assertGzipped(mimeType, null, shouldGzip);
95      }
96  
97      private void assertGzipped(final String mimeType, final String userAgent, final boolean shouldGzip)
98              throws IOException
99      {
100         IntegrationTestUtils.assertPathGzipped("test.html?mimetype=" + URLEncoder.encode(mimeType, "UTF-8"), userAgent, shouldGzip);
101     }
102 
103     protected byte[] gzipContent(String content) throws IOException
104     {
105         ByteArrayOutputStream bao = new ByteArrayOutputStream();
106         GZIPOutputStream outputStream = new GZIPOutputStream(bao);
107         outputStream.write(content.getBytes("UTF-8"));
108         outputStream.close();
109         return bao.toByteArray();
110     }
111 
112     protected String gunzipContent(byte[] content, String mimeType) throws IOException
113     {
114         ByteArrayInputStream bai = new ByteArrayInputStream(content);
115         ByteArrayOutputStream bao = new ByteArrayOutputStream();
116         GZIPInputStream inputStream = new GZIPInputStream(bai);
117         int i;
118         while ((i = inputStream.read()) != -1)
119         {
120             bao.write(i);
121         }
122         return new String(bao.toByteArray(), mimeType);
123     }
124 }