Issue #5684 Fix and re-enable ServletRequestLogTest. (#5877)

Signed-off-by: Jan Bartel <janb@webtide.com>
This commit is contained in:
Jan Bartel 2021-01-13 22:35:36 +01:00 committed by GitHub
parent a5a8327a1e
commit b99eb3f76c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 57 additions and 112 deletions

View File

@ -19,10 +19,7 @@
package org.eclipse.jetty.servlet;
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;
import java.util.ArrayList;
@ -39,6 +36,7 @@ import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.server.Connector;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpChannel;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
@ -48,24 +46,24 @@ import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.server.handler.DefaultHandler;
import org.eclipse.jetty.server.handler.ErrorHandler;
import org.eclipse.jetty.server.handler.HandlerCollection;
import org.eclipse.jetty.toolchain.test.IO;
import org.eclipse.jetty.util.component.AbstractLifeCycle;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.log.StacklessLogging;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static java.time.Duration.ofSeconds;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
/**
* Servlet equivalent of the jetty-server's RequestLogHandlerTest, but with more ErrorHandler twists.
*/
@Disabled
public class ServletRequestLogTest
{
private static final Logger LOG = Log.getLogger(ServletRequestLogTest.class);
@ -109,7 +107,7 @@ public class ServletRequestLogTest
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.sendError(500, "Whoops");
response.sendError(500, "FromResponseSendErrorServlet");
}
}
@ -119,7 +117,7 @@ public class ServletRequestLogTest
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
throw new ServletException("Whoops");
throw new ServletException("FromServletExceptionServlet");
}
}
@ -129,7 +127,7 @@ public class ServletRequestLogTest
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
throw new IOException("Whoops");
throw new IOException("FromIOExceptionServlet");
}
}
@ -139,7 +137,7 @@ public class ServletRequestLogTest
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
throw new RuntimeException("Whoops");
throw new RuntimeException("FromRuntimeExceptionServlet");
}
}
@ -273,7 +271,6 @@ public class ServletRequestLogTest
data.add(new Object[]{new HelloServlet(), "/test/", "GET /test/ HTTP/1.1 200"});
data.add(new Object[]{new AsyncOnTimeoutCompleteServlet(), "/test/", "GET /test/ HTTP/1.1 200"});
data.add(new Object[]{new AsyncOnTimeoutDispatchServlet(), "/test/", "GET /test/ HTTP/1.1 200"});
data.add(new Object[]{new AsyncOnStartIOExceptionServlet(), "/test/", "GET /test/ HTTP/1.1 500"});
data.add(new Object[]{new ResponseSendErrorServlet(), "/test/", "GET /test/ HTTP/1.1 500"});
data.add(new Object[]{new ServletExceptionServlet(), "/test/", "GET /test/ HTTP/1.1 500"});
@ -323,21 +320,28 @@ public class ServletRequestLogTest
// Add the test servlet
ServletHolder testHolder = new ServletHolder(testServlet);
app.addServlet(testHolder, "/test");
app.addServlet(testHolder, "/test/*");
try
try (StacklessLogging scope = new StacklessLogging(HttpChannel.class))
{
server.start();
Assertions.assertTimeoutPreemptively(ofSeconds(4), () ->
{
server.start();
connector.addBean(new HttpChannel.Listener()
{
@Override
public void onComplete(Request request)
{
assertRequestLog(expectedLogEntry, captureLog);
}
});
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
@ -345,24 +349,11 @@ public class ServletRequestLogTest
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(expectedLogEntry, captureLog);
});
}
finally
@ -413,22 +404,28 @@ public class ServletRequestLogTest
// Add the test servlet
ServletHolder testHolder = new ServletHolder(testServlet);
app.addServlet(testHolder, "/test");
app.addServlet(testHolder, "/test/*");
try
try (StacklessLogging scope = new StacklessLogging(HttpChannel.class))
{
server.start();
Assertions.assertTimeoutPreemptively(ofSeconds(4), () ->
{
connector.addBean(new HttpChannel.Listener()
{
@Override
public void onComplete(Request request)
{
assertRequestLog(expectedLogEntry, captureLog);
}
});
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
@ -436,24 +433,11 @@ public class ServletRequestLogTest
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(expectedLogEntry, captureLog);
});
}
finally
@ -509,20 +493,26 @@ public class ServletRequestLogTest
errorMapper.addErrorPage(500, "/errorpage");
app.setErrorHandler(errorMapper);
try
try (StacklessLogging scope = new StacklessLogging(HttpChannel.class))
{
server.start();
Assertions.assertTimeoutPreemptively(ofSeconds(4), () ->
{
connector.addBean(new HttpChannel.Listener()
{
@Override
public void onComplete(Request request)
{
assertRequestLog(expectedLogEntry, captureLog);
}
});
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, requestPath, null, null);
// Make call to test handler
@ -530,24 +520,11 @@ public class ServletRequestLogTest
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(expectedLogEntry, captureLog);
});
}
finally
@ -608,13 +585,20 @@ public class ServletRequestLogTest
Assertions.assertTimeoutPreemptively(ofSeconds(4), () ->
{
connector.addBean(new HttpChannel.Listener()
{
@Override
public void onComplete(Request request)
{
assertRequestLog(expectedLogEntry, captureLog);
}
});
String host = connector.getHost();
if (host == null)
{
host = "localhost";
}
int port = connector.getLocalPort();
int port = connector.getLocalPort();
URI serverUri = new URI("http", null, host, port, "/test", null, null);
// Make call to test handler
@ -622,24 +606,11 @@ public class ServletRequestLogTest
try
{
connection.setAllowUserInteraction(false);
// log response status code
int statusCode = connection.getResponseCode();
LOG.info("Response Status Code: {}", statusCode);
if (statusCode == 200)
{
// collect response message and log it
String content = getResponseContent(connection);
LOG.info("Response Content: {}", content);
}
}
finally
{
connection.disconnect();
}
assertRequestLog(expectedLogEntry, captureLog);
});
}
finally
@ -650,33 +621,7 @@ public class ServletRequestLogTest
private void assertRequestLog(final String expectedLogEntry, CaptureLog captureLog)
{
int captureCount = captureLog.captured.size();
if (captureCount != 1)
{
LOG.warn("Capture Log size is {}, expected to be 1", captureCount);
if (captureCount > 1)
{
for (int i = 0; i < captureCount; i++)
{
LOG.warn("[{}] {}", i, captureLog.captured.get(i));
}
}
assertThat("Capture Log Entry Count", captureLog.captured.size(), is(1));
}
String actual = captureLog.captured.get(0);
assertThat("Capture Log", actual, is(expectedLogEntry));
}
private String getResponseContent(HttpURLConnection connection) throws IOException
{
try (InputStream in = connection.getInputStream();
InputStreamReader reader = new InputStreamReader(in))
{
StringWriter writer = new StringWriter();
IO.copy(reader, writer);
return writer.toString();
}
assertThat("Request log size", captureLog.captured, not(empty()));
assertThat("Request log entry",captureLog.captured.get(0), is(expectedLogEntry));
}
}