1 package it.com.atlassian.gzipfilter;
2
3
4 import org.apache.commons.httpclient.Header;
5 import org.apache.commons.httpclient.HttpMethod;
6 import org.junit.Ignore;
7 import org.junit.Test;
8 import org.junit.runner.RunWith;
9 import org.junit.runners.Parameterized;
10
11 import static org.junit.Assert.*;
12 import static org.hamcrest.CoreMatchers.*;
13
14 import java.io.IOException;
15 import java.net.URLEncoder;
16 import java.util.Arrays;
17 import java.util.Collection;
18
19 @RunWith (Parameterized.class)
20 public class TestContentEncodingNotEmptyAfterHeaderRewrite
21 {
22 @Parameterized.Parameters(name = "{index}: expected gzippable: {1}")
23 public static Collection<Object[]> data() {
24 return Arrays.asList(new Object[][] {
25 { new String[]{"application/octet-stream","application/javascript"}, true},
26 { new String[]{"application/javascript","application/octet-stream"}, false},
27 { new String[]{"text/html;charset=UTF-8","application/atom+xml"}, false},
28 });
29 }
30
31 @Parameterized.Parameter(0)
32 public String[] mimeTypesSequence;
33 @Parameterized.Parameter(1)
34 public boolean gzipped;
35
36 public void testRewriteWith(String path, String outputMethod) throws IOException
37 {
38 StringBuilder sb = new StringBuilder(mimeTypesSequence[0]);
39 for (int i=1; i<mimeTypesSequence.length; i++) {
40 sb.append(",").append(mimeTypesSequence[i]);
41 }
42 HttpMethod method = IntegrationTestUtils.assertPathGzipped(
43 path + "?outputMethod=" + outputMethod +
44 "&mimetypes=" + URLEncoder.encode(sb.toString(), "UTF-8"),
45 null,
46 gzipped
47 );
48 String expectedCtype = mimeTypesSequence[mimeTypesSequence.length - 1];
49 Header contentTypeHeader = method.getResponseHeader("Content-Type");
50 assertNotNull("Content-Type header is NULL, expected: "+expectedCtype, contentTypeHeader);
51 assertThat(contentTypeHeader.getValue(), startsWith(expectedCtype));
52 }
53
54 @Test
55 public void testRewritePrintWriter() throws IOException
56 {
57 testRewriteWith("rewriter.html", "PrintWriter");
58 }
59
60 @Test
61 public void testRewriteOutputStream() throws IOException
62 {
63 testRewriteWith("rewriter.html", "OutputStream");
64 }
65
66 @Test
67 public void testRewriteFilterPrintWriter() throws IOException
68 {
69 testRewriteWith("rewriter/noop.html", "PrintWriter");
70 }
71
72 @Test
73 public void testRewriteFilterOutputStream() throws IOException
74 {
75 testRewriteWith("rewriter/noop.html", "OutputStream");
76 }
77
78 @Test
79 public void testIncludeRewriterPrintWriter() throws IOException
80 {
81 testRewriteWith("include_rewriter.html", "PrintWriter");
82 }
83
84 @Test
85 public void testIncludeRewriterOutputStream() throws IOException
86 {
87 testRewriteWith("include_rewriter.html", "OutputStream");
88 }
89
90 @Test
91 @Ignore("This is how JIRA now uses the filter, and it's failing")
92 public void testBrokenIncludeRewriterPrintWriter() throws IOException
93 {
94 testRewriteWith("broken/include_rewriter.html", "PrintWriter");
95 }
96
97 @Test
98 @Ignore("This is how JIRA now uses the filter, and it's failing")
99 public void testBrokenIncludeRewriterOutputStream() throws IOException
100 {
101 testRewriteWith("broken/include_rewriter.html", "OutputStream");
102 }
103 }