Merge branch 'jetty-9.2.x'

Conflicts:
	jetty-servlets/src/test/java/org/eclipse/jetty/server/handler/gzip/GzipDefaultTest.java
	jetty-servlets/src/test/java/org/eclipse/jetty/server/handler/gzip/GzipTester.java
This commit is contained in:
Joakim Erdfelt 2014-11-10 19:31:31 -07:00
commit 00692c0ff3
2 changed files with 227 additions and 209 deletions

View File

@ -18,8 +18,16 @@
package org.eclipse.jetty.server.handler.gzip;
import java.io.IOException;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import javax.servlet.DispatcherType;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
@ -29,7 +37,10 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpTester;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.FilterHolder;
import org.eclipse.jetty.toolchain.test.IO;
import org.eclipse.jetty.toolchain.test.TestingDir;
import org.eclipse.jetty.util.StringUtil;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
@ -83,18 +94,16 @@ public class GzipDefaultTest
}
}
@SuppressWarnings("serial")
public static class HttpContentTypeWithEncoding extends HttpServlet
{
public static final String COMPRESSED_CONTENT = "<html><head></head><body><h1>COMPRESSABLE CONTENT</h1>"+
"This content must be longer than the default min gzip length, which is 256 bytes. "+
"The moon is blue to a fish in love. How now brown cow. The quick brown fox jumped over the lazy dog. A woman needs a man like a fish needs a bicycle!"+
"</body></html>";
public static final String COMPRESSED_CONTENT = "<html><head></head><body><h1>COMPRESSABLE CONTENT</h1>"
+ "This content must be longer than the default min gzip length, which is 256 bytes. "
+ "The moon is blue to a fish in love. How now brown cow. The quick brown fox jumped over the lazy dog. A woman needs a man like a fish needs a bicycle!"
+ "</body></html>";
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
resp.setContentType("text/plain;charset=UTF8");
resp.setStatus(200);
@ -107,25 +116,38 @@ public class GzipDefaultTest
@Rule
public TestingDir testingdir = new TestingDir();
@Test
public void testIsGzipByMethod() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Configure Gzip Handler
tester.getGzipHandler().setIncludedMethods("POST","WIBBLE");
// Prepare Server File
int filesize = tester.getOutputBufferSize() * 2;
tester.prepareServerFile("file.txt",filesize);
// Content Servlet
tester.setContentServlet(GetServlet.class);
tester.getGzipHandler().setIncludedMethods("POST","WIBBLE");
try
{
tester.start();
HttpTester.Response response;
tester.assertIsResponseGzipCompressed("POST","file.txt");
tester.assertIsResponseGzipCompressed("WIBBLE","file.txt");
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,200);
response = tester.executeRequest("GET","/context/file.txt",2,TimeUnit.SECONDS);
assertThat("Response status",response.getStatus(),is(HttpStatus.OK_200));
assertThat("Content-Encoding",response.get("Content-Encoding"),not(containsString(compressionType)));
String content = tester.readResponse(response);
assertThat("Response content size",content.length(),is(filesize));
String expectedContent = IO.readToString(testingdir.getFile("file.txt"));
assertThat("Response content",content,is(expectedContent));
}
finally
{
@ -155,21 +177,35 @@ public class GzipDefaultTest
}
}
@Test
public void testIsGzipCompressedEmpty() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Configure Gzip Handler
tester.getGzipHandler().addIncludedMimeTypes("text/plain");
// Prepare server file
tester.prepareServerFile("empty.txt",0);
tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class);
// Set content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
tester.assertIsResponseNotGzipCompressed("GET","empty.txt",0,200);
HttpTester.Response response;
response = tester.executeRequest("GET","/context/empty.txt",2,TimeUnit.SECONDS);
assertThat("Response status",response.getStatus(),is(HttpStatus.OK_200));
assertThat("Content-Encoding",response.get("Content-Encoding"),not(containsString(compressionType)));
String content = tester.readResponse(response);
assertThat("Response content size",content.length(),is(0));
String expectedContent = IO.readToString(testingdir.getFile("empty.txt"));
assertThat("Response content",content,is(expectedContent));
}
finally
{
@ -222,7 +258,6 @@ public class GzipDefaultTest
}
}
@Test
public void testGzipedIfModified() throws Exception
{
@ -287,22 +322,26 @@ public class GzipDefaultTest
}
}
@Test
public void testIsNotGzipCompressedWithZeroQ() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType + "; q=0");
// Configure Gzip Handler
tester.getGzipHandler().addIncludedMimeTypes("text/plain");
// Prepare server file
int filesize = tester.getOutputBufferSize() / 4;
tester.prepareServerFile("file.txt",filesize);
tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class);
// Add content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
HttpTester.Response http = tester.assertIsResponseNotGzipCompressed("GET","file.txt", filesize, HttpStatus.OK_200);
Assert.assertEquals("Accept-Encoding, User-Agent",http.get("Vary"));
HttpTester.Response http = assertIsResponseNotGzipCompressed(tester,"GET","file.txt",filesize,HttpStatus.OK_200);
assertThat("Response[Vary]",http.get("Vary"),containsString("Accept-Encoding"));
}
finally
{
@ -338,15 +377,17 @@ public class GzipDefaultTest
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("file.mp3",filesize);
tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class);
// Add content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
HttpTester.Response http = tester.assertIsResponseNotGzipCompressed("GET","file.mp3", filesize, HttpStatus.OK_200);
HttpTester.Response http = assertIsResponseNotGzipCompressed(tester,"GET","file.mp3",filesize,HttpStatus.OK_200);
Assert.assertNull(http.get("Vary"));
}
finally
@ -355,22 +396,25 @@ public class GzipDefaultTest
}
}
@Test
public void testIsNotGzipCompressedByExcludedContentType() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Configure Gzip Handler
tester.getGzipHandler().addExcludedMimeTypes("text/plain");
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("test_quotes.txt",filesize);
tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class);
tester.getGzipHandler().setExcludedMimeTypes("text/plain");
// Add content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
HttpTester.Response http = tester.assertIsResponseNotGzipCompressed("GET","test_quotes.txt", filesize, HttpStatus.OK_200);
HttpTester.Response http = assertIsResponseNotGzipCompressed(tester,"GET","test_quotes.txt",filesize,HttpStatus.OK_200);
Assert.assertNull(http.get("Vary"));
}
finally
@ -379,23 +423,26 @@ public class GzipDefaultTest
}
}
@Test
public void testIsNotGzipCompressedByExcludedContentTypeWithCharset() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Configure Gzip Handler
tester.getGzipHandler().addExcludedMimeTypes("text/plain");
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("test_quotes.txt",filesize);
tester.addMimeType("txt","text/plain;charset=UTF-8");
tester.setContentServlet(org.eclipse.jetty.servlet.DefaultServlet.class);
tester.getGzipHandler().addExcludedMimeTypes("text/plain");
// Add content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
HttpTester.Response http = tester.assertIsResponseNotGzipCompressed("GET","test_quotes.txt", filesize, HttpStatus.OK_200);
HttpTester.Response http = assertIsResponseNotGzipCompressed(tester,"GET","test_quotes.txt",filesize,HttpStatus.OK_200);
Assert.assertNull(http.get("Vary"));
}
finally
@ -423,22 +470,26 @@ public class GzipDefaultTest
}
}
@Test
public void testIsNotGzipCompressedByDeferredContentType() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Configure Gzip Handler
tester.getGzipHandler().addIncludedMimeTypes("text/plain");
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("file.mp3.deferred",filesize);
// Add content servlet
tester.setContentServlet(GetServlet.class);
try
{
tester.start();
HttpTester.Response http = tester.assertIsResponseNotGzipCompressed("GET","file.mp3.deferred", filesize, HttpStatus.OK_200);
Assert.assertNull(http.get("Vary"));
HttpTester.Response response = assertIsResponseNotGzipCompressed(tester,"GET","file.mp3.deferred",filesize,HttpStatus.OK_200);
assertThat("Response[Vary]", response.get("Vary"), isEmptyOrNullString());
}
finally
{
@ -451,13 +502,20 @@ public class GzipDefaultTest
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Configure Gzip Handler
tester.getGzipHandler().addIncludedMimeTypes("text/plain");
// Test error code 204
tester.setContentServlet(HttpStatusServlet.class);
try
{
tester.start();
tester.assertIsResponseNotGzipCompressed("GET",-1, 204);
HttpTester.Response response = tester.executeRequest("GET","/context/",2,TimeUnit.SECONDS);
assertThat("Response status",response.getStatus(),is(HttpStatus.NO_CONTENT_204));
assertThat("Content-Encoding",response.get("Content-Encoding"),not(containsString(compressionType)));
}
finally
{
@ -471,38 +529,51 @@ public class GzipDefaultTest
{
GzipTester tester = new GzipTester(testingdir,compressionType);
// Configure Gzip Handler
tester.getGzipHandler().addIncludedMimeTypes("text/plain");
// Test error code 400
tester.setContentServlet(HttpErrorServlet.class);
try
{
tester.start();
tester.assertIsResponseNotGzipCompressedAndEqualToExpectedString("GET","error message", -1, 400);
HttpTester.Response response = tester.executeRequest("GET","/context/",2,TimeUnit.SECONDS);
assertThat("Response status",response.getStatus(),is(HttpStatus.BAD_REQUEST_400));
assertThat("Content-Encoding",response.get("Content-Encoding"),not(containsString(compressionType)));
String content = tester.readResponse(response);
assertThat("Response content",content,is("error message"));
}
finally
{
tester.stop();
}
}
@Test
public void testUserAgentExclusion() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
tester.setContentServlet(DefaultServlet.class);
tester.getGzipHandler().setExcludedAgentPatterns("bar","foo");
tester.setUserAgent("foo");
// Configure Gzip Handler
tester.getGzipHandler().addIncludedMimeTypes("text/plain");
tester.getGzipHandler().setExcludedAgentPatterns("bar","foo");
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("file.txt",filesize);
// Add content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
assertIsResponseNotGzipCompressed(tester,"GET","file.txt",filesize,HttpStatus.OK_200);
}
finally
{
@ -514,9 +585,7 @@ public class GzipDefaultTest
public void testUserAgentExclusionDefault() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
tester.setContentServlet(DefaultServlet.class);
tester.setUserAgent("Some MSIE 6.0 user-agent");
int filesize = tester.getOutputBufferSize() * 4;
@ -525,7 +594,7 @@ public class GzipDefaultTest
try
{
tester.start();
HttpTester.Response http = tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
HttpTester.Response http = assertIsResponseNotGzipCompressed(tester,"GET","file.txt",filesize,HttpStatus.OK_200);
Assert.assertEquals("Accept-Encoding, User-Agent",http.get("Vary"));
}
finally
@ -538,18 +607,22 @@ public class GzipDefaultTest
public void testUserAgentExclusionByExcludedAgentPatterns() throws Exception
{
GzipTester tester = new GzipTester(testingdir,compressionType);
tester.setContentServlet(DefaultServlet.class);
tester.getGzipHandler().setExcludedAgentPatterns("bar","fo.*");
tester.setUserAgent("foo");
// Configure Gzip Handler
tester.getGzipHandler().setExcludedAgentPatterns("bar","fo.*");
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("file.txt",filesize);
// Set content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
assertIsResponseNotGzipCompressed(tester,"GET","file.txt",filesize,HttpStatus.OK_200);
}
finally
{
@ -562,16 +635,20 @@ public class GzipDefaultTest
{
GzipTester tester = new GzipTester(testingdir,compressionType);
tester.setContentServlet(DefaultServlet.class);
// Configure Gzip Handler
tester.getGzipHandler().setExcludedPaths("*.txt");
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("file.txt",filesize);
// Set content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
tester.assertIsResponseNotGzipCompressed("GET","file.txt",filesize,HttpStatus.OK_200);
assertIsResponseNotGzipCompressed(tester,"GET","file.txt",filesize,HttpStatus.OK_200);
}
finally
{
@ -584,13 +661,17 @@ public class GzipDefaultTest
{
GzipTester tester = new GzipTester(testingdir,compressionType);
tester.setContentServlet(DefaultServlet.class);
// Configure Gzip Handler
tester.getGzipHandler().setExcludedPaths("*.txt");
tester.getGzipHandler().setIncludedPaths("/file.txt");
// Prepare server file
int filesize = tester.getOutputBufferSize() * 4;
tester.prepareServerFile("file.txt",filesize);
// Set content servlet
tester.setContentServlet(DefaultServlet.class);
try
{
tester.start();
@ -602,6 +683,44 @@ public class GzipDefaultTest
}
}
public HttpTester.Response assertIsResponseNotGzipCompressed(GzipTester tester, String method, String filename, int expectedFilesize, int status)
throws Exception
{
HttpTester.Response response = tester.executeRequest(method,"/context/" + filename,2,TimeUnit.SECONDS);
assertThat("Response status",response.getStatus(),is(status));
assertThat("Content-Encoding",response.get("Content-Encoding"),not(containsString(compressionType)));
assertResponseContent(tester,response,status,filename,expectedFilesize);
return response;
}
private void assertResponseContent(GzipTester tester, HttpTester.Response response, int status, String filename, int expectedFilesize) throws IOException,
UnsupportedEncodingException
{
if (expectedFilesize >= 0)
{
assertThat("filename",filename,notNullValue());
assertThat("Response contentBytes.length",response.getContentBytes().length,is(expectedFilesize));
String contentLength = response.get("Content-Length");
if (StringUtil.isNotBlank(contentLength))
{
assertThat("Content-Length",response.get("Content-Length"),is(Integer.toString(expectedFilesize)));
}
if (status >= 200 && status < 300)
{
assertThat("ETag",response.get("ETAG"),startsWith("W/"));
}
File serverFile = testingdir.getFile(filename);
String expectedResponse = IO.readToString(serverFile);
String actual = tester.readResponse(response);
Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
}
}
@Test
@ -611,6 +730,7 @@ public class GzipDefaultTest
tester.setContentServlet(DefaultServlet.class);
tester.copyTestServerFile("test.svgz");
try
{
tester.start();

View File

@ -40,7 +40,6 @@ import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
import javax.servlet.DispatcherType;
import javax.servlet.Filter;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletResponse;
@ -77,7 +76,6 @@ public class GzipTester
}
}
private Class<? extends Filter> gzipFilterClass = null;
private String encoding = "ISO8859_1";
private String userAgent = null;
private final ServletTester tester = new ServletTester("/context",ServletContextHandler.GZIP);
@ -438,95 +436,6 @@ public class GzipTester
return mat.group();
}
/**
* Asserts that the requested filename results in a properly structured GzipFilter response, where the content is not compressed, and the content-length is
* returned appropriately.
*
* @param filename
* the filename used for the request, and also used to compare the response to the server file, assumes that the file is suitable for
* {@link Assert#assertEquals(Object, Object)} use. (in other words, the contents of the file are text)
* @param expectedFilesize
* the expected filesize to be specified on the Content-Length portion of the response headers. (note: passing -1 will disable the Content-Length
* assertion)
* @throws Exception
*/
public HttpTester.Response assertIsResponseNotGzipCompressed(String method, String filename, int expectedFilesize, int status) throws Exception
{
String uri = "/context/" + filename;
HttpTester.Response response = executeRequest(method,uri);
assertResponseHeaders(expectedFilesize,status,response);
// Assert that the contents are what we expect.
if (filename != null)
{
File serverFile = testdir.getFile(filename);
String expectedResponse = IO.readToString(serverFile);
String actual = readResponse(response);
Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
}
return response;
}
/**
* Asserts that the request results in a properly structured GzipFilter response, where the content is not compressed, and the content-length is returned
* appropriately.
*
* @param expectedResponse
* the expected response body string
* @param expectedFilesize
* the expected filesize to be specified on the Content-Length portion of the response headers. (note: passing -1 will disable the Content-Length
* assertion)
* @throws Exception
*/
public void assertIsResponseNotGzipCompressedAndEqualToExpectedString(String method, String expectedResponse, int expectedFilesize, int status)
throws Exception
{
String uri = "/context/";
HttpTester.Response response = executeRequest(method,uri);
assertResponseHeaders(expectedFilesize,status,response);
String actual = readResponse(response);
Assert.assertEquals("Expected response equals actual response",expectedResponse,actual);
}
/**
* Asserts that the request results in a properly structured GzipFilter response, where the content is not compressed, and the content-length is returned
* appropriately.
*
* @param expectedFilesize
* the expected filesize to be specified on the Content-Length portion of the response headers. (note: passing -1 will disable the Content-Length
* assertion)
* @throws Exception
*/
public void assertIsResponseNotGzipCompressed(String method, int expectedFilesize, int status) throws Exception
{
String uri = "/context/";
HttpTester.Response response = executeRequest(method,uri);
assertResponseHeaders(expectedFilesize,status,response);
}
private void assertResponseHeaders(int expectedFilesize, int status, HttpTester.Response response)
{
Assert.assertThat("Response.status",response.getStatus(),is(status));
Assert.assertThat("Response.header[Content-Encoding]",response.get("Content-Encoding"),not(containsString(compressionType)));
if (expectedFilesize != (-1))
{
Assert.assertEquals(expectedFilesize,response.getContentBytes().length);
String cl = response.get("Content-Length");
if (cl != null)
{
int serverLength = Integer.parseInt(response.get("Content-Length"));
Assert.assertEquals(serverLength,expectedFilesize);
}
if (status >= 200 && status < 300)
Assert.assertThat(response.get("ETAG"),Matchers.startsWith("W/"));
}
Assert.assertThat("Response.header[Content-Encoding]",response.get("Content-Encoding"),not(containsString(compressionType)));
}
public HttpTester.Response executeRequest(String method, String path, int idleFor, TimeUnit idleUnit) throws Exception
{
HttpTester.Request request = HttpTester.newRequest();
@ -548,12 +457,7 @@ public class GzipTester
return HttpTester.parseResponse(tester.getResponses(request.generate(),idleFor,idleUnit));
}
private HttpTester.Response executeRequest(String method, String path) throws IOException, Exception
{
return executeRequest(method,path,2,TimeUnit.SECONDS);
}
private String readResponse(HttpTester.Response response) throws IOException, UnsupportedEncodingException
public String readResponse(HttpTester.Response response) throws IOException, UnsupportedEncodingException
{
String actual = null;
InputStream in = null;
@ -677,12 +581,6 @@ public class GzipTester
ServletHolder servletHolder = tester.addServlet(servletClass,"/");
servletHolder.setInitParameter("baseDir",testdir.getDir().getAbsolutePath());
servletHolder.setInitParameter("etags","true");
if (gzipFilterClass != null)
{
FilterHolder holder = tester.addFilter(gzipFilterClass,"/*",EnumSet.of(DispatcherType.REQUEST));
holder.setInitParameter("vary","Accept-Encoding");
}
}
public void setEncoding(String encoding)