Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoakim Erdfelt2014-10-10 14:21:40 -0400
committerJoakim Erdfelt2014-10-10 14:21:40 -0400
commite7b86b99cba10ae817f0d15ce2ce87265a7fbcfd (patch)
tree23c57bbe4ef65adceae6b1bc538ada6aca56f93e /jetty-server
parente292b572f135f67623c4c9a773e56289aa5a8f66 (diff)
downloadorg.eclipse.jetty.project-e7b86b99cba10ae817f0d15ce2ce87265a7fbcfd.tar.gz
org.eclipse.jetty.project-e7b86b99cba10ae817f0d15ce2ce87265a7fbcfd.tar.xz
org.eclipse.jetty.project-e7b86b99cba10ae817f0d15ce2ce87265a7fbcfd.zip
445823 - RequestLogHandler at end of HandlerCollection doesn't work
+ Adding more varieties of server side collection vs wrapper with error handlers + Adding servlet version of tests to demonstrate behavior in servlet context, when also using the mapped error handler
Diffstat (limited to 'jetty-server')
-rw-r--r--jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java422
1 files changed, 393 insertions, 29 deletions
diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java
index 239ca2ecb1..3ddb2cbe94 100644
--- a/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java
+++ b/jetty-server/src/test/java/org/eclipse/jetty/server/handler/RequestLogHandlerTest.java
@@ -24,6 +24,7 @@ import static org.junit.Assert.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
+import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.HttpURLConnection;
import java.net.URI;
@@ -33,6 +34,7 @@ import java.util.List;
import javax.servlet.AsyncContext;
import javax.servlet.AsyncEvent;
import javax.servlet.AsyncListener;
+import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -55,7 +57,13 @@ import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
+/**
+ * Tests for RequestLogHandler behavior.
+ * <p>
+ * Tests different request handler behavior against different server+error configurations
+ */
@RunWith(Parameterized.class)
+@Ignore
public class RequestLogHandlerTest
{
private static final Logger LOG = Log.getLogger(RequestLogHandlerTest.class);
@@ -67,10 +75,10 @@ public class RequestLogHandlerTest
@Override
public void log(Request request, Response response)
{
- captured.add(String.format("%s %s %s %03d",request.getMethod(),request.getUri().toString(),request.getProtocol(),response.getStatus()));
+ captured.add(String.format("%s %s %s %03d",request.getMethod(),request.getRequestURI(),request.getProtocol(),response.getStatus()));
}
}
-
+
private static abstract class AbstractTestHandler extends AbstractHandler
{
@Override
@@ -90,17 +98,17 @@ public class RequestLogHandlerTest
baseRequest.setHandled(true);
}
}
-
+
private static class ResponseSendErrorHandler extends AbstractTestHandler
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
- response.sendError(500, "Whoops");
+ response.sendError(500,"Whoops");
baseRequest.setHandled(true);
}
}
-
+
private static class ServletExceptionHandler extends AbstractTestHandler
{
@Override
@@ -109,7 +117,7 @@ public class RequestLogHandlerTest
throw new ServletException("Whoops");
}
}
-
+
private static class IOExceptionHandler extends AbstractTestHandler
{
@Override
@@ -118,7 +126,7 @@ public class RequestLogHandlerTest
throw new IOException("Whoops");
}
}
-
+
private static class RuntimeExceptionHandler extends AbstractTestHandler
{
@Override
@@ -127,7 +135,7 @@ public class RequestLogHandlerTest
throw new RuntimeException("Whoops");
}
}
-
+
private static class AsyncOnTimeoutCompleteHandler extends AbstractTestHandler implements AsyncListener
{
@Override
@@ -160,7 +168,7 @@ public class RequestLogHandlerTest
{
}
}
-
+
private static class AsyncOnTimeoutCompleteUnhandledHandler extends AbstractTestHandler implements AsyncListener
{
@Override
@@ -192,13 +200,13 @@ public class RequestLogHandlerTest
{
}
}
-
+
private static class AsyncOnTimeoutDispatchHandler extends AbstractTestHandler implements AsyncListener
{
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
- if(request.getAttribute("deep") == null)
+ if (request.getAttribute("deep") == null)
{
AsyncContext ac = request.startAsync();
ac.setTimeout(1000);
@@ -229,7 +237,7 @@ public class RequestLogHandlerTest
{
}
}
-
+
private static class AsyncOnStartIOExceptionHandler extends AbstractTestHandler implements AsyncListener
{
@Override
@@ -264,37 +272,108 @@ public class RequestLogHandlerTest
{
}
}
-
- @Parameters(name="{0}")
+
+ public static class OKErrorHandler extends ErrorHandler
+ {
+ @Override
+ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
+ {
+ if (baseRequest.isHandled() || response.isCommitted())
+ {
+ return;
+ }
+
+ // collect error details
+ String reason = (response instanceof Response)?((Response)response).getReason():null;
+ int status = response.getStatus();
+
+ // intentionally set response status to OK (this is a test to see what is actually logged)
+ response.setStatus(200);
+ response.setContentType("text/plain");
+ PrintWriter out = response.getWriter();
+ out.printf("Error %d: %s%n",status,reason);
+ baseRequest.setHandled(true);
+ }
+ }
+
+ public static class DispatchErrorHandler extends ErrorHandler
+ {
+ @Override
+ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
+ {
+ if (baseRequest.isHandled() || response.isCommitted())
+ {
+ return;
+ }
+
+ RequestDispatcher dispatcher = request.getRequestDispatcher("/errorok/");
+ assertThat("Dispatcher", dispatcher, notNullValue());
+
+ try
+ {
+ dispatcher.forward(request,response);
+ }
+ catch (ServletException e)
+ {
+ throw new IOException("Dispatch.forward failed",e);
+ }
+ }
+ }
+
+ public static class AltErrorHandler extends ErrorHandler
+ {
+ @Override
+ public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException
+ {
+ if (baseRequest.isHandled() || response.isCommitted())
+ {
+ return;
+ }
+
+ // collect error details
+ String reason = (response instanceof Response)?((Response)response).getReason():null;
+ int status = response.getStatus();
+
+ // intentionally set response status to OK (this is a test to see what is actually logged)
+ response.setContentType("text/plain");
+ PrintWriter out = response.getWriter();
+ out.printf("Error %d: %s%n",status,reason);
+ baseRequest.setHandled(true);
+ }
+ }
+
+ @Parameters(name = "{0}")
public static List<Object[]> data()
{
List<Object[]> data = new ArrayList<>();
- data.add(new Object[] { new HelloHandler(), "/test", "GET /test HTTP/1.1 200" });
- data.add(new Object[] { new AsyncOnTimeoutCompleteHandler(), "/test", "GET /test HTTP/1.1 200" });
- data.add(new Object[] { new AsyncOnTimeoutCompleteUnhandledHandler(), "/test", "GET /test HTTP/1.1 200" });
- data.add(new Object[] { new AsyncOnTimeoutDispatchHandler(), "/test", "GET /test HTTP/1.1 200" });
-
- data.add(new Object[] { new AsyncOnStartIOExceptionHandler(), "/test", "GET /test HTTP/1.1 500" });
- data.add(new Object[] { new ResponseSendErrorHandler(), "/test", "GET /test HTTP/1.1 500" });
- data.add(new Object[] { new ServletExceptionHandler(), "/test", "GET /test HTTP/1.1 500" });
- data.add(new Object[] { new IOExceptionHandler(), "/test", "GET /test HTTP/1.1 500" });
- data.add(new Object[] { new RuntimeExceptionHandler(), "/test", "GET /test HTTP/1.1 500" });
+ data.add(new Object[] { new HelloHandler(), "/test/", "GET /test/ HTTP/1.1 200" });
+ data.add(new Object[] { new AsyncOnTimeoutCompleteHandler(), "/test/", "GET /test/ HTTP/1.1 200" });
+ data.add(new Object[] { new AsyncOnTimeoutCompleteUnhandledHandler(), "/test/", "GET /test/ HTTP/1.1 200" });
+ data.add(new Object[] { new AsyncOnTimeoutDispatchHandler(), "/test/", "GET /test/ HTTP/1.1 200" });
+
+ data.add(new Object[] { new AsyncOnStartIOExceptionHandler(), "/test/", "GET /test/ HTTP/1.1 500" });
+ data.add(new Object[] { new ResponseSendErrorHandler(), "/test/", "GET /test/ HTTP/1.1 500" });
+ data.add(new Object[] { new ServletExceptionHandler(), "/test/", "GET /test/ HTTP/1.1 500" });
+ data.add(new Object[] { new IOExceptionHandler(), "/test/", "GET /test/ HTTP/1.1 500" });
+ data.add(new Object[] { new RuntimeExceptionHandler(), "/test/", "GET /test/ HTTP/1.1 500" });
return data;
}
@Parameter(0)
public Handler testHandler;
-
+
@Parameter(1)
public String requestPath;
@Parameter(2)
public String expectedLogEntry;
- @Test(timeout=4000)
- @Ignore
+ /**
+ * Test a RequestLogHandler at the end of a HandlerCollection. all other configuration on server at defaults.
+ */
+ @Test(timeout = 4000)
public void testLogHandlerCollection() throws Exception
{
Server server = new Server();
@@ -353,8 +432,293 @@ public class RequestLogHandlerTest
server.stop();
}
}
+
+ /**
+ * Test a RequestLogHandler at the end of a HandlerCollection and also with the default ErrorHandler as server bean in place.
+ */
+ @Test(timeout = 4000)
+ public void testLogHandlerCollection_ErrorHandler_ServerBean() throws Exception
+ {
+ Server server = new Server();
+ ServerConnector connector = new ServerConnector(server);
+ connector.setPort(0);
+ server.setConnectors(new Connector[] { connector });
+
+ ErrorHandler errorHandler = new ErrorHandler();
+ server.addBean(errorHandler);
+
+ CaptureLog captureLog = new CaptureLog();
+
+ RequestLogHandler requestLog = new RequestLogHandler();
+ requestLog.setRequestLog(captureLog);
+
+ HandlerCollection handlers = new HandlerCollection();
+ handlers.setHandlers(new Handler[] { testHandler, requestLog });
+ server.setHandler(handlers);
+
+ try
+ {
+ server.start();
+
+ String host = connector.getHost();
+ if (host == null)
+ {
+ host = "localhost";
+ }
+ int port = connector.getLocalPort();
+
+ URI serverUri = new URI("http",null,host,port,requestPath,null,null);
+
+ // Make call to test handler
+ HttpURLConnection connection = (HttpURLConnection)serverUri.toURL().openConnection();
+ try
+ {
+ connection.setAllowUserInteraction(false);
+
+ // log response status code
+ int statusCode = connection.getResponseCode();
+ LOG.debug("Response Status Code: {}",statusCode);
+
+ if (statusCode == 200)
+ {
+ // collect response message and log it
+ String content = getResponseContent(connection);
+ LOG.debug("Response Content: {}",content);
+ }
+ }
+ finally
+ {
+ connection.disconnect();
+ }
+
+ assertRequestLog(captureLog);
+ }
+ finally
+ {
+ server.stop();
+ }
+ }
+
+ /**
+ * Test a RequestLogHandler at the end of a HandlerCollection and also with the ErrorHandler in place.
+ */
+ @Test(timeout=4000)
+ public void testLogHandlerCollection_AltErrorHandler() throws Exception
+ {
+ Server server = new Server();
+ ServerConnector connector = new ServerConnector(server);
+ connector.setPort(0);
+ server.setConnectors(new Connector[] { connector });
+
+ AltErrorHandler errorDispatcher = new AltErrorHandler();
+ server.addBean(errorDispatcher);
+
+ ContextHandlerCollection contexts = new ContextHandlerCollection();
+ ContextHandler errorContext = new ContextHandler("/errorpage");
+ errorContext.setHandler(new AltErrorHandler());
+ ContextHandler testContext = new ContextHandler("/test");
+ testContext.setHandler(testHandler);
+ contexts.addHandler(errorContext);
+ contexts.addHandler(testContext);
+
+ RequestLogHandler requestLog = new RequestLogHandler();
+ CaptureLog captureLog = new CaptureLog();
+ requestLog.setRequestLog(captureLog);
+
+ HandlerCollection handlers = new HandlerCollection();
+ handlers.setHandlers(new Handler[] { contexts, requestLog });
+ server.setHandler(handlers);
+
+ try
+ {
+ server.start();
+
+ String host = connector.getHost();
+ if (host == null)
+ {
+ host = "localhost";
+ }
+ int port = connector.getLocalPort();
+
+ URI serverUri = new URI("http",null,host,port,requestPath,null,null);
+
+ // Make call to test handler
+ HttpURLConnection connection = (HttpURLConnection)serverUri.toURL().openConnection();
+ try
+ {
+ connection.setAllowUserInteraction(false);
+
+ // log response status code
+ int statusCode = connection.getResponseCode();
+ LOG.debug("Response Status Code: {}",statusCode);
+
+ if (statusCode == 200)
+ {
+ // collect response message and log it
+ String content = getResponseContent(connection);
+ LOG.debug("Response Content: {}",content);
+ }
+ }
+ finally
+ {
+ connection.disconnect();
+ }
+
+ assertRequestLog(captureLog);
+ }
+ finally
+ {
+ server.stop();
+ }
+ }
+ /**
+ * Test a RequestLogHandler at the end of a HandlerCollection and also with the ErrorHandler in place.
+ */
@Test(timeout=4000)
+ public void testLogHandlerCollection_OKErrorHandler() throws Exception
+ {
+ Server server = new Server();
+ ServerConnector connector = new ServerConnector(server);
+ connector.setPort(0);
+ server.setConnectors(new Connector[] { connector });
+
+ OKErrorHandler errorDispatcher = new OKErrorHandler();
+ server.addBean(errorDispatcher);
+
+ ContextHandlerCollection contexts = new ContextHandlerCollection();
+ ContextHandler errorContext = new ContextHandler("/errorpage");
+ errorContext.setHandler(new AltErrorHandler());
+ ContextHandler testContext = new ContextHandler("/test");
+ testContext.setHandler(testHandler);
+ contexts.addHandler(errorContext);
+ contexts.addHandler(testContext);
+
+ RequestLogHandler requestLog = new RequestLogHandler();
+ CaptureLog captureLog = new CaptureLog();
+ requestLog.setRequestLog(captureLog);
+
+ HandlerCollection handlers = new HandlerCollection();
+ handlers.setHandlers(new Handler[] { contexts, requestLog });
+ server.setHandler(handlers);
+
+ try
+ {
+ server.start();
+
+ String host = connector.getHost();
+ if (host == null)
+ {
+ host = "localhost";
+ }
+ int port = connector.getLocalPort();
+
+ URI serverUri = new URI("http",null,host,port,requestPath,null,null);
+
+ // Make call to test handler
+ HttpURLConnection connection = (HttpURLConnection)serverUri.toURL().openConnection();
+ try
+ {
+ connection.setAllowUserInteraction(false);
+
+ // log response status code
+ int statusCode = connection.getResponseCode();
+ LOG.debug("Response Status Code: {}",statusCode);
+
+ if (statusCode == 200)
+ {
+ // collect response message and log it
+ String content = getResponseContent(connection);
+ LOG.debug("Response Content: {}",content);
+ }
+ }
+ finally
+ {
+ connection.disconnect();
+ }
+
+ assertRequestLog(captureLog);
+ }
+ finally
+ {
+ server.stop();
+ }
+ }
+
+ /**
+ * Test a RequestLogHandler at the end of a HandlerCollection and also with the ErrorHandler in place.
+ */
+ @Test(timeout=4000)
+ public void testLogHandlerCollection_DispatchErrorHandler() throws Exception
+ {
+ Server server = new Server();
+ ServerConnector connector = new ServerConnector(server);
+ connector.setPort(0);
+ server.setConnectors(new Connector[] { connector });
+
+ DispatchErrorHandler errorDispatcher = new DispatchErrorHandler();
+ server.addBean(errorDispatcher);
+
+ ContextHandlerCollection contexts = new ContextHandlerCollection();
+ ContextHandler errorContext = new ContextHandler("errorok");
+ errorContext.setHandler(new OKErrorHandler());
+ ContextHandler testContext = new ContextHandler("test");
+ testContext.setHandler(testHandler);
+ contexts.addHandler(errorContext);
+ contexts.addHandler(testContext);
+
+ RequestLogHandler requestLog = new RequestLogHandler();
+ CaptureLog captureLog = new CaptureLog();
+ requestLog.setRequestLog(captureLog);
+
+ HandlerCollection handlers = new HandlerCollection();
+ handlers.setHandlers(new Handler[] { contexts, requestLog });
+ server.setHandler(handlers);
+
+ try
+ {
+ server.start();
+
+ String host = connector.getHost();
+ if (host == null)
+ {
+ host = "localhost";
+ }
+ int port = connector.getLocalPort();
+
+ URI serverUri = new URI("http",null,host,port,requestPath,null,null);
+
+ // Make call to test handler
+ HttpURLConnection connection = (HttpURLConnection)serverUri.toURL().openConnection();
+ try
+ {
+ connection.setAllowUserInteraction(false);
+
+ // log response status code
+ int statusCode = connection.getResponseCode();
+ LOG.debug("Response Status Code: {}",statusCode);
+
+ if (statusCode == 200)
+ {
+ // collect response message and log it
+ String content = getResponseContent(connection);
+ LOG.debug("Response Content: {}",content);
+ }
+ }
+ finally
+ {
+ connection.disconnect();
+ }
+
+ assertRequestLog(captureLog);
+ }
+ finally
+ {
+ server.stop();
+ }
+ }
+
+ @Test(timeout = 4000)
public void testLogHandlerWrapped() throws Exception
{
Server server = new Server();
@@ -366,7 +730,7 @@ public class RequestLogHandlerTest
RequestLogHandler requestLog = new RequestLogHandler();
requestLog.setRequestLog(captureLog);
-
+
requestLog.setHandler(testHandler);
server.setHandler(requestLog);
@@ -382,7 +746,7 @@ public class RequestLogHandlerTest
}
int port = connector.getLocalPort();
- URI serverUri = new URI("http",null,host,port,"/test",null,null);
+ URI serverUri = new URI("http",null,host,port,requestPath,null,null);
// Make call to test handler
HttpURLConnection connection = (HttpURLConnection)serverUri.toURL().openConnection();

Back to the top