422388 Test for GzipFilter apply to resources with charset appended to the MIME type

This commit is contained in:
Jan Bartel 2013-11-25 13:06:26 +11:00
parent ffddaeceec
commit a6cdac1d88
2 changed files with 97 additions and 1 deletions

View File

@ -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 = "<html><head></head><body><h1>COMPRESSED</h1></body></html>";
@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
{

View File

@ -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);