Skip to content

Commit

Permalink
Update DebugHandlerTest (#12424)
Browse files Browse the repository at this point in the history
* Update DebugHandlerTest
  • Loading branch information
joakime authored Oct 30, 2024
1 parent 85ce152 commit 6b1efe2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 20 deletions.
5 changes: 5 additions & 0 deletions jetty-core/jetty-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
<artifactId>jetty-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-slf4j-impl</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util-ajax</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,14 +147,15 @@ public void setShowHeaders(boolean showHeaders)
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
Thread thread = Thread.currentThread();
String name = thread.getName() + ":" + request.getHttpURI();
String name = thread.getName() + ":" + request.getHttpURI().getPathQuery();
boolean willHandle = false;
Throwable ex = null;
String rname = findRequestName(request);
HandlingCallback handlingCallback = new HandlingCallback(callback, request, response);

try
{
thread.setName(name);
String headers = _showHeaders ? ("\n" + request.getHeaders().toString()) : "";

log(">> r=%s %s %s %s %s %s",
Expand All @@ -164,7 +165,6 @@ public boolean handle(Request request, Response response, Callback callback) thr
request.getConnectionMetaData().getProtocol(),
request.getConnectionMetaData(),
headers);
thread.setName(name);

willHandle = getHandler().handle(request, response, handlingCallback);
return willHandle;
Expand Down Expand Up @@ -195,10 +195,11 @@ protected void log(String format, Object... arg)

String s = String.format(format, arg);

String threadName = Thread.currentThread().getName();
long now = System.currentTimeMillis();
long ms = now % 1000;
if (_print != null)
_print.printf("%s.%03d:%s%n", __date.format(now), ms, s);
_print.printf("%s.%03d:%s:%s%n", __date.format(now), ms, threadName, s);
}

protected String findRequestName(Request request)
Expand All @@ -218,7 +219,6 @@ protected String findRequestName(Request request)
}
catch (IllegalStateException e)
{
// TODO can we avoid creating and catching this exception? see #8024
// Handle the case when the request has already been completed
return String.format("%s@%x", request.getHttpURI(), request.hashCode());
}
Expand All @@ -234,7 +234,7 @@ protected void doStart() throws Exception
for (Connector connector : getServer().getConnectors())
{
if (connector instanceof AbstractConnector)
((AbstractConnector)connector).addBean(this, false);
connector.addBean(this, false);
}

super.doStart();
Expand All @@ -248,7 +248,7 @@ protected void doStop() throws Exception
for (Connector connector : getServer().getConnectors())
{
if (connector instanceof AbstractConnector)
((AbstractConnector)connector).removeBean(this);
connector.removeBean(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.net.HttpURLConnection;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyStore;
import java.util.concurrent.TimeUnit;
Expand All @@ -28,28 +29,32 @@
import javax.net.ssl.TrustManagerFactory;

import org.eclipse.jetty.io.ArrayByteBufferPool;
import org.eclipse.jetty.io.IOResources;
import org.eclipse.jetty.server.AbstractConnectionFactory;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.HttpConnectionFactory;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.toolchain.test.MavenPaths;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.awaitility.Awaitility.await;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;

@Disabled // TODO
public class DebugHandlerTest
{
private static final Logger LOG = LoggerFactory.getLogger(DebugHandlerTest.class);
public static final HostnameVerifier __hostnameverifier = (hostname, session) -> true;

private SSLContext sslContext;
Expand All @@ -72,7 +77,7 @@ public void startServer() throws Exception
httpConnector.setPort(0);
server.addConnector(httpConnector);

Path keystorePath = MavenTestingUtils.getTestResourcePath("keystore.p12");
Path keystorePath = MavenPaths.findTestResourceFile("keystore.p12");
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
sslContextFactory.setKeyStorePath(keystorePath.toAbsolutePath().toString());
sslContextFactory.setKeyStorePassword("storepwd");
Expand All @@ -85,19 +90,20 @@ public void startServer() throws Exception
debugHandler = new DebugHandler();
capturedLog = new ByteArrayOutputStream();
debugHandler.setOutputStream(capturedLog);
/* TODO
debugHandler.setHandler(new AbstractHandler()
debugHandler.setHandler(new Handler.Abstract()
{

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
public boolean handle(Request request, Response response, Callback callback) throws Exception
{
baseRequest.setHandled(true);
response.setStatus(HttpStatus.OK_200);
LOG.info("Abstract handle()");
response.setStatus(200);
callback.succeeded();
return true;
}
});
server.setHandler(debugHandler);

*/
server.start();

String host = httpConnector.getHost();
Expand All @@ -106,9 +112,15 @@ public void handle(String target, Request baseRequest, HttpServletRequest reques

serverURI = URI.create(String.format("http://%s:%d/", host, httpConnector.getLocalPort()));
secureServerURI = URI.create(String.format("https://%s:%d/", host, sslConnector.getLocalPort()));
}

@BeforeEach
public void trustAllHttpsUrlConnection() throws Exception
{
Path keystorePath = MavenPaths.findTestResourceFile("keystore.p12");

KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
try (InputStream stream = IOResources.asInputStream(sslContextFactory.getKeyStoreResource()))
try (InputStream stream = Files.newInputStream(keystorePath))
{
keystore.load(stream, "storepwd".toCharArray());
}
Expand Down Expand Up @@ -151,7 +163,7 @@ public void testThreadName() throws IOException
HttpURLConnection http = (HttpURLConnection)serverURI.resolve("/foo/bar?a=b").toURL().openConnection();
assertThat("Response Code", http.getResponseCode(), is(200));

String log = capturedLog.toString(StandardCharsets.UTF_8.name());
String log = capturedLog.toString(StandardCharsets.UTF_8);
String expectedThreadName = ":/foo/bar?a=b";
assertThat("ThreadName", log, containsString(expectedThreadName));
// Look for bad/mangled/duplicated schemes
Expand All @@ -165,7 +177,7 @@ public void testSecureThreadName() throws IOException
HttpURLConnection http = (HttpURLConnection)secureServerURI.resolve("/foo/bar?a=b").toURL().openConnection();
assertThat("Response Code", http.getResponseCode(), is(200));

String log = capturedLog.toString(StandardCharsets.UTF_8.name());
String log = capturedLog.toString(StandardCharsets.UTF_8);
String expectedThreadName = ":/foo/bar?a=b";
assertThat("ThreadName", log, containsString(expectedThreadName));
// Look for bad/mangled/duplicated schemes
Expand Down

0 comments on commit 6b1efe2

Please sign in to comment.