Parameterize testWebsocketClientInWebapp over both http and https

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2020-10-14 16:35:58 +11:00
parent 27a0879798
commit 26f4062d93
5 changed files with 68 additions and 58 deletions

View File

@ -21,6 +21,7 @@ package org.eclipse.jetty.tests.distribution;
import java.util.function.Supplier; import java.util.function.Supplier;
import org.eclipse.jetty.client.HttpClient; import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
public class AbstractDistributionTest public class AbstractDistributionTest
@ -29,7 +30,15 @@ public class AbstractDistributionTest
protected void startHttpClient() throws Exception protected void startHttpClient() throws Exception
{ {
startHttpClient(HttpClient::new); startHttpClient(false);
}
protected void startHttpClient(boolean secure) throws Exception
{
if (secure)
startHttpClient(() -> new HttpClient(new SslContextFactory.Client(true)));
else
startHttpClient(HttpClient::new);
} }
protected void startHttpClient(Supplier<HttpClient> supplier) throws Exception protected void startHttpClient(Supplier<HttpClient> supplier) throws Exception

View File

@ -309,8 +309,9 @@ public class DistributionTests extends AbstractDistributionTest
} }
} }
@Test @ParameterizedTest
public void testWebsocketClientInWebapp() throws Exception @ValueSource(strings = {"http", "https"})
public void testWebsocketClientInWebapp(String scheme) throws Exception
{ {
Path jettyBase = Files.createTempDirectory("jetty_base"); Path jettyBase = Files.createTempDirectory("jetty_base");
String jettyVersion = System.getProperty("jettyVersion"); String jettyVersion = System.getProperty("jettyVersion");
@ -323,7 +324,7 @@ public class DistributionTests extends AbstractDistributionTest
String[] args1 = { String[] args1 = {
"--create-startd", "--create-startd",
"--approve-all-licenses", "--approve-all-licenses",
"--add-to-start=resources,server,http,webapp,deploy,jsp,jmx,servlet,servlets,websocket" "--add-to-start=resources,server,webapp,deploy,jsp,jmx,servlet,servlets,websocket," + scheme
}; };
try (DistributionTester.Run run1 = distribution.start(args1)) try (DistributionTester.Run run1 = distribution.start(args1))
{ {
@ -336,6 +337,7 @@ public class DistributionTests extends AbstractDistributionTest
int port = distribution.freePort(); int port = distribution.freePort();
String[] args2 = { String[] args2 = {
"jetty.http.port=" + port, "jetty.http.port=" + port,
"jetty.ssl.port=" + port,
// "jetty.server.dumpAfterStart=true", // "jetty.server.dumpAfterStart=true",
// "jetty.webapp.addSystemClasses+=,org.eclipse.jetty.client.", // "jetty.webapp.addSystemClasses+=,org.eclipse.jetty.client.",
// "jetty.webapp.addServerClasses+=,-org.eclipse.jetty.client.", // "jetty.webapp.addServerClasses+=,-org.eclipse.jetty.client.",
@ -350,13 +352,15 @@ public class DistributionTests extends AbstractDistributionTest
assertTrue(run2.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS)); assertTrue(run2.awaitConsoleLogsFor("Started @", 10, TimeUnit.SECONDS));
// We should get the correct configuration from the jetty-websocket-httpclient.xml file. // We should get the correct configuration from the jetty-websocket-httpclient.xml file.
startHttpClient(); startHttpClient(scheme.equals("https"));
URI serverUri = URI.create("ws://localhost:" + port + "/test"); URI serverUri = URI.create(scheme + "://localhost:" + port + "/test");
ContentResponse response = client.GET(serverUri); ContentResponse response = client.GET(serverUri);
assertEquals(HttpStatus.OK_200, response.getStatus()); assertEquals(HttpStatus.OK_200, response.getStatus());
String content = response.getContentAsString(); String content = response.getContentAsString();
// assertThat(content, containsString("ConnectTimeout: 4999")); // TODO: how to test this?
assertThat(content, containsString("WebSocketEcho: success")); assertThat(content, containsString("WebSocketEcho: success"));
// We cannot test the HttpClient timeout because it is a server class not exposed to the webapp.
// assertThat(content, containsString("ConnectTimeout: 4999"));
} }
} }
} }

Binary file not shown.

View File

@ -18,11 +18,12 @@
package org.eclipse.jetty.tests.webapp.websocket; package org.eclipse.jetty.tests.webapp.websocket;
import java.io.IOException; import java.io.PrintWriter;
import java.net.URI; import java.net.URI;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch; import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet; import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
@ -39,55 +40,57 @@ import org.eclipse.jetty.websocket.client.WebSocketClient;
@WebServlet("/") @WebServlet("/")
public class WebSocketClientServlet extends HttpServlet public class WebSocketClientServlet extends HttpServlet
{ {
@Override private WebSocketClient client;
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{ @Override
public void init() throws ServletException
{
client = new WebSocketClient();
WebSocketClient client = null;
try try
{ {
client = new WebSocketClient(); client.start();
WebSocketClient finalClient = client;
runThrowExceptionsAsRuntime(() -> finalClient.start());
resp.setContentType("text/html");
//resp.getWriter().println("ConnectTimeout: " + client.getHttpClient().getConnectTimeout());
ClientSocket clientSocket = new ClientSocket();
URI wsUri = WSURI.toWebsocket(req.getRequestURL()).resolve("echo");
client.connect(clientSocket, wsUri);
clientSocket.openLatch.await(5, TimeUnit.SECONDS);
clientSocket.session.getRemote().sendString("test message");
String response = clientSocket.textMessages.poll(5, TimeUnit.SECONDS);
if (!"test message".equals(response))
throw new RuntimeException("incorrect response");
clientSocket.session.close();
clientSocket.closeLatch.await(5, TimeUnit.SECONDS);
resp.getWriter().println("WebSocketEcho: success");
} }
catch (Exception e) catch (Exception e)
{ {
throw new RuntimeException(e); throw new ServletException(e);
}
finally
{
if (client != null)
{
WebSocketClient finalClient = client;
runThrowExceptionsAsRuntime(() -> finalClient.stop());
}
} }
} }
public interface ThrowingRunnable @Override
{ public void destroy()
void run() throws Exception;
}
public void runThrowExceptionsAsRuntime(ThrowingRunnable runnable)
{ {
try try
{ {
runnable.run(); client.stop();
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
try
{
resp.setContentType("text/html");
// Send and receive a websocket echo on the same server.
ClientSocket clientSocket = new ClientSocket();
URI wsUri = WSURI.toWebsocket(req.getRequestURL()).resolve("echo");
client.connect(clientSocket, wsUri).get(5, TimeUnit.SECONDS);
clientSocket.session.getRemote().sendString("test message");
String response = clientSocket.textMessages.poll(5, TimeUnit.SECONDS);
clientSocket.session.close();
clientSocket.closeLatch.await(5, TimeUnit.SECONDS);
PrintWriter writer = resp.getWriter();
writer.println("WebSocketEcho: " + ("test message".equals(response) ? "success" : "failure"));
writer.println("WebSocketEcho: success");
// We cannot test the HttpClient timeout because it is a server class not exposed to the webapp.
// writer.println("ConnectTimeout: " + client.getHttpClient().getConnectTimeout());
} }
catch (Exception e) catch (Exception e)
{ {
@ -122,4 +125,10 @@ public class WebSocketClientServlet extends HttpServlet
closeLatch.countDown(); closeLatch.countDown();
} }
} }
private void assertTrue(boolean value)
{
if (!value)
throw new RuntimeException("expected expression to be true but was false");
}
} }

View File

@ -3,19 +3,7 @@
<Configure class="org.eclipse.jetty.client.HttpClient"> <Configure class="org.eclipse.jetty.client.HttpClient">
<Arg> <Arg>
<New class="org.eclipse.jetty.util.ssl.SslContextFactory$Client"> <New class="org.eclipse.jetty.util.ssl.SslContextFactory$Client">
<Set name="trustAll" type="java.lang.Boolean">false</Set> <Set name="trustAll" type="java.lang.Boolean">true</Set>
<Call name="addExcludeProtocols">
<Arg>
<Array type="java.lang.String">
<Item>TLS/1.3</Item>
</Array>
</Arg>
</Call>
<Call name="setExcludeCipherSuites"><!-- websocket.org uses WEAK cipher suites -->
<Arg>
<Array type="java.lang.String" />
</Arg>
</Call>
</New> </New>
</Arg> </Arg>
<Set name="connectTimeout">4999</Set> <Set name="connectTimeout">4999</Set>