482243 GzipHandler include test
This commit is contained in:
parent
12ec44894b
commit
8ed34b3395
|
@ -53,5 +53,12 @@
|
|||
<artifactId>jetty-test-helper</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jetty</groupId>
|
||||
<artifactId>jetty-http</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<classifier>tests</classifier>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
// ========================================================================
|
||||
//
|
||||
|
||||
package org.eclipse.jetty.embedded;
|
||||
package org.eclipse.jetty.servlet;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
@ -29,28 +29,19 @@ import java.io.PrintWriter;
|
|||
import java.util.zip.GZIPInputStream;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.eclipse.jetty.http.GzipHttpContent;
|
||||
import org.eclipse.jetty.http.HttpHeader;
|
||||
import org.eclipse.jetty.http.HttpStatus;
|
||||
import org.eclipse.jetty.http.HttpTester;
|
||||
import org.eclipse.jetty.server.Handler;
|
||||
import org.eclipse.jetty.server.LocalConnector;
|
||||
import org.eclipse.jetty.server.Request;
|
||||
import org.eclipse.jetty.server.Server;
|
||||
import org.eclipse.jetty.server.handler.AbstractHandler;
|
||||
import org.eclipse.jetty.server.handler.gzip.GzipHandler;
|
||||
import org.eclipse.jetty.toolchain.test.AdvancedRunner;
|
||||
import org.eclipse.jetty.util.IO;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
@RunWith(AdvancedRunner.class)
|
||||
public class GzipHandlerTest
|
||||
{
|
||||
private static String __content =
|
||||
|
@ -67,6 +58,8 @@ public class GzipHandlerTest
|
|||
"Aliquam purus mauris, consectetur nec convallis lacinia, porta sed ante. Suspendisse "+
|
||||
"et cursus magna. Donec orci enim, molestie a lobortis eu, imperdiet vitae neque.";
|
||||
|
||||
private static String __icontent = "BEFORE"+__content+"AFTER";
|
||||
|
||||
private Server _server;
|
||||
private LocalConnector _connector;
|
||||
|
||||
|
@ -77,37 +70,51 @@ public class GzipHandlerTest
|
|||
_connector = new LocalConnector(_server);
|
||||
_server.addConnector(_connector);
|
||||
|
||||
Handler testHandler = new AbstractHandler()
|
||||
{
|
||||
|
||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException,
|
||||
ServletException
|
||||
{
|
||||
String etag="W/\"foobar\"";
|
||||
response.setHeader(HttpHeader.ETAG.asString(),etag);
|
||||
|
||||
String inm = request.getHeader(HttpHeader.IF_NONE_MATCH.asString());
|
||||
if (inm!=null && inm.contains(etag))
|
||||
{
|
||||
response.setStatus(HttpStatus.NOT_MODIFIED_304);
|
||||
}
|
||||
else
|
||||
{
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write(__content);
|
||||
writer.close();
|
||||
}
|
||||
|
||||
baseRequest.setHandled(true);
|
||||
}
|
||||
};
|
||||
|
||||
GzipHandler gzipHandler = new GzipHandler();
|
||||
gzipHandler.setHandler(testHandler);
|
||||
|
||||
ServletContextHandler context = new ServletContextHandler(gzipHandler,"/ctx");
|
||||
ServletHandler servlets = context.getServletHandler();
|
||||
|
||||
_server.setHandler(gzipHandler);
|
||||
gzipHandler.setHandler(context);
|
||||
context.setHandler(servlets);
|
||||
servlets.addServletWithMapping(TestServlet.class,"/content");
|
||||
servlets.addServletWithMapping(ForwardServlet.class,"/forward");
|
||||
servlets.addServletWithMapping(IncludeServlet.class,"/include");
|
||||
|
||||
_server.start();
|
||||
}
|
||||
|
||||
public static class TestServlet extends HttpServlet
|
||||
{
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
PrintWriter writer = response.getWriter();
|
||||
writer.write(__content);
|
||||
}
|
||||
}
|
||||
|
||||
public static class ForwardServlet extends HttpServlet
|
||||
{
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
getServletContext().getRequestDispatcher("/content").forward(request,response);
|
||||
}
|
||||
}
|
||||
|
||||
public static class IncludeServlet extends HttpServlet
|
||||
{
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
|
||||
{
|
||||
response.getWriter().write("BEFORE");
|
||||
getServletContext().getRequestDispatcher("/content").include(request,response);
|
||||
response.getWriter().write("AFTER");
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void destroy() throws Exception
|
||||
|
@ -116,56 +123,46 @@ public class GzipHandlerTest
|
|||
_server.join();
|
||||
}
|
||||
|
||||
HttpTester.Request newRequest()
|
||||
{
|
||||
HttpTester.Request request = HttpTester.newRequest();
|
||||
request.setMethod("GET");
|
||||
request.setVersion("HTTP/1.0");
|
||||
request.setHeader("Host","tester");
|
||||
request.setURI("/");
|
||||
return request;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoGzipHandler() throws Exception
|
||||
{
|
||||
// generated and parsed test
|
||||
HttpTester.Request request = newRequest();
|
||||
request.setHeader("accept-encoding","deflate");
|
||||
|
||||
HttpTester.Response response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
Assert.assertNull(response.get("Content-Encoding"));
|
||||
assertEquals(__content, response.getContent());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGzipHandler() throws Exception
|
||||
{
|
||||
// generated and parsed test
|
||||
HttpTester.Request request = newRequest();
|
||||
HttpTester.Request request = HttpTester.newRequest();
|
||||
HttpTester.Response response;
|
||||
|
||||
request.setMethod("GET");
|
||||
request.setVersion("HTTP/1.0");
|
||||
request.setHeader("Host","tester");
|
||||
request.setHeader("accept-encoding","gzip");
|
||||
request.setURI("/ctx/content");
|
||||
|
||||
HttpTester.Response response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
assertTrue(response.get("Content-Encoding").equalsIgnoreCase("gzip"));
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
|
||||
InputStream testIn = new GZIPInputStream(new ByteArrayInputStream(response.getContentBytes()));
|
||||
ByteArrayOutputStream testOut = new ByteArrayOutputStream();
|
||||
IO.copy(testIn,testOut);
|
||||
|
||||
assertEquals(__content, testOut.toString("UTF8"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeflateGzipHandler() throws Exception
|
||||
public void testForwardGzipHandler() throws Exception
|
||||
{
|
||||
// generated and parsed test
|
||||
HttpTester.Request request = newRequest();
|
||||
request.setHeader("accept-encoding","deflate;q=0.1,gzip;q=0.5");
|
||||
HttpTester.Request request = HttpTester.newRequest();
|
||||
HttpTester.Response response;
|
||||
|
||||
HttpTester.Response response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
request.setMethod("GET");
|
||||
request.setVersion("HTTP/1.0");
|
||||
request.setHeader("Host","tester");
|
||||
request.setHeader("accept-encoding","gzip");
|
||||
request.setURI("/ctx/forward");
|
||||
|
||||
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
|
||||
assertTrue(response.get("Content-Encoding").equalsIgnoreCase("gzip"));
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
|
@ -177,57 +174,28 @@ public class GzipHandlerTest
|
|||
assertEquals(__content, testOut.toString("UTF8"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Test
|
||||
public void testEtagGzipHandler() throws Exception
|
||||
public void testIncludeGzipHandler() throws Exception
|
||||
{
|
||||
// generated and parsed test
|
||||
HttpTester.Request request = newRequest();
|
||||
HttpTester.Response response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
Assert.assertNull(response.get("Content-Encoding"));
|
||||
assertEquals(__content, response.getContent());
|
||||
String etag = response.get(HttpHeader.ETAG.asString());
|
||||
String etag_gzip=etag.substring(0,etag.length()-1)+GzipHttpContent.ETAG_GZIP_QUOTE;
|
||||
|
||||
request = newRequest();
|
||||
request.add(HttpHeader.IF_NONE_MATCH,"xxxx");
|
||||
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
Assert.assertNull(response.get("Content-Encoding"));
|
||||
assertEquals(__content, response.getContent());
|
||||
HttpTester.Request request = HttpTester.newRequest();
|
||||
HttpTester.Response response;
|
||||
|
||||
request = newRequest();
|
||||
request.add(HttpHeader.IF_NONE_MATCH,etag);
|
||||
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
assertEquals(HttpServletResponse.SC_NOT_MODIFIED,response.getStatus());
|
||||
assertEquals(etag,response.get(HttpHeader.ETAG.asString()));
|
||||
|
||||
request = newRequest();
|
||||
request.setMethod("GET");
|
||||
request.setVersion("HTTP/1.0");
|
||||
request.setHeader("Host","tester");
|
||||
request.setHeader("accept-encoding","gzip");
|
||||
request.add(HttpHeader.IF_NONE_MATCH,etag);
|
||||
request.setURI("/ctx/include");
|
||||
|
||||
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
assertEquals(HttpServletResponse.SC_NOT_MODIFIED,response.getStatus());
|
||||
assertEquals(etag,response.get(HttpHeader.ETAG.asString()));
|
||||
|
||||
request = newRequest();
|
||||
request.setHeader("accept-encoding","gzip");
|
||||
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
assertTrue(response.get("Content-Encoding").equalsIgnoreCase("gzip"));
|
||||
assertEquals(HttpServletResponse.SC_OK,response.getStatus());
|
||||
|
||||
InputStream testIn = new GZIPInputStream(new ByteArrayInputStream(response.getContentBytes()));
|
||||
ByteArrayOutputStream testOut = new ByteArrayOutputStream();
|
||||
IO.copy(testIn,testOut);
|
||||
assertEquals(__content, testOut.toString("UTF8"));
|
||||
assertEquals(etag_gzip,response.get(HttpHeader.ETAG.asString()));
|
||||
|
||||
request = newRequest();
|
||||
request.setHeader("accept-encoding","gzip");
|
||||
request.add(HttpHeader.IF_NONE_MATCH,etag_gzip);
|
||||
response = HttpTester.parseResponse(_connector.getResponses(request.generate()));
|
||||
assertEquals(HttpServletResponse.SC_NOT_MODIFIED,response.getStatus());
|
||||
assertEquals(etag,response.get(HttpHeader.ETAG.asString()));
|
||||
|
||||
|
||||
assertEquals(__icontent, testOut.toString("UTF8"));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue