diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java index 21f10d41185..cdb0564c5d5 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/GzipFilterDefaultTest.java @@ -23,6 +23,7 @@ import java.util.Arrays; import java.util.Collection; import javax.servlet.ServletException; +import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -100,6 +101,23 @@ public class GzipFilterDefaultTest resp.setStatus(_status); } } + + + public static class HttpContentTypeWithEncoding extends HttpServlet + { + public static final String COMPRESSED_CONTENT = "

COMPRESSED

"; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException + { + resp.setContentType("text/plain;charset=UTF8"); + resp.setStatus(200); + ServletOutputStream out = resp.getOutputStream(); + out.print(COMPRESSED_CONTENT); + } + + } @Rule public TestingDir testingdir = new TestingDir(); @@ -369,7 +387,28 @@ public class GzipFilterDefaultTest tester.stop(); } } - + + @Test + public void testGzipCompressedByContentTypeWithEncoding() throws Exception + { + GzipTester tester = new GzipTester(testingdir, compressionType); + FilterHolder holder = tester.setContentServlet(HttpContentTypeWithEncoding.class); + holder.setInitParameter("mimeTypes","text/plain"); + try + { + tester.start(); + HttpTester.Response http = tester.assertNonStaticContentIsResponseGzipCompressed("GET","xxx", HttpContentTypeWithEncoding.COMPRESSED_CONTENT); + Assert.assertEquals("Accept-Encoding",http.get("Vary")); + System.err.println(http.get("Content-Type")); + System.err.println(http.get("Content-Encoding")); + } + finally + { + tester.stop(); + } + } + + @Test public void testIsNotGzipCompressedByDeferredContentType() throws Exception { diff --git a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java index b4e95548a68..b4ecdc1af8c 100644 --- a/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java +++ b/jetty-servlets/src/test/java/org/eclipse/jetty/servlets/gzip/GzipTester.java @@ -92,6 +92,63 @@ public class GzipTester return assertIsResponseGzipCompressed(method,requestedFilename,serverFilename,-1); } + + public HttpTester.Response assertNonStaticContentIsResponseGzipCompressed(String method, String path, String expected) throws Exception + { + HttpTester.Request request = HttpTester.newRequest(); + HttpTester.Response response; + + request.setMethod(method); + request.setVersion("HTTP/1.0"); + request.setHeader("Host","tester"); + request.setHeader("Accept-Encoding",compressionType); + + if (this.userAgent != null) + request.setHeader("User-Agent", this.userAgent); + request.setURI("/context/" + path); + + // Issue the request + response = HttpTester.parseResponse(tester.getResponses(request.generate())); + + int qindex = compressionType.indexOf(";"); + if (qindex < 0) + Assert.assertThat("Response.header[Content-Encoding]",response.get("Content-Encoding"),containsString(compressionType)); + else + Assert.assertThat("Response.header[Content-Encoding]", response.get("Content-Encoding"),containsString(compressionType.substring(0,qindex))); + + ByteArrayInputStream bais = null; + InputStream in = null; + ByteArrayOutputStream out = null; + String actual = null; + + try + { + bais = new ByteArrayInputStream(response.getContentBytes()); + if (compressionType.startsWith(GzipFilter.GZIP)) + { + in = new GZIPInputStream(bais); + } + else if (compressionType.startsWith(GzipFilter.DEFLATE)) + { + in = new InflaterInputStream(bais, new Inflater(true)); + } + out = new ByteArrayOutputStream(); + IO.copy(in,out); + + actual = out.toString(encoding); + assertThat("Uncompressed contents",actual,equalTo(expected)); + } + finally + { + IO.close(out); + IO.close(in); + IO.close(bais); + } + + + return response; + } + public HttpTester.Response assertIsResponseGzipCompressed(String method, String requestedFilename, String serverFilename, long ifmodifiedsince) throws Exception { // System.err.printf("[GzipTester] requesting /context/%s%n",requestedFilename);