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 org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.AfterEach;
public class AbstractDistributionTest
@ -29,6 +30,14 @@ public class AbstractDistributionTest
protected void startHttpClient() throws Exception
{
startHttpClient(false);
}
protected void startHttpClient(boolean secure) throws Exception
{
if (secure)
startHttpClient(() -> new HttpClient(new SslContextFactory.Client(true)));
else
startHttpClient(HttpClient::new);
}

View File

@ -309,8 +309,9 @@ public class DistributionTests extends AbstractDistributionTest
}
}
@Test
public void testWebsocketClientInWebapp() throws Exception
@ParameterizedTest
@ValueSource(strings = {"http", "https"})
public void testWebsocketClientInWebapp(String scheme) throws Exception
{
Path jettyBase = Files.createTempDirectory("jetty_base");
String jettyVersion = System.getProperty("jettyVersion");
@ -323,7 +324,7 @@ public class DistributionTests extends AbstractDistributionTest
String[] args1 = {
"--create-startd",
"--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))
{
@ -336,6 +337,7 @@ public class DistributionTests extends AbstractDistributionTest
int port = distribution.freePort();
String[] args2 = {
"jetty.http.port=" + port,
"jetty.ssl.port=" + port,
// "jetty.server.dumpAfterStart=true",
// "jetty.webapp.addSystemClasses+=,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));
// We should get the correct configuration from the jetty-websocket-httpclient.xml file.
startHttpClient();
URI serverUri = URI.create("ws://localhost:" + port + "/test");
startHttpClient(scheme.equals("https"));
URI serverUri = URI.create(scheme + "://localhost:" + port + "/test");
ContentResponse response = client.GET(serverUri);
assertEquals(HttpStatus.OK_200, response.getStatus());
String content = response.getContentAsString();
// assertThat(content, containsString("ConnectTimeout: 4999")); // TODO: how to test this?
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;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URI;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
@ -39,55 +40,57 @@ import org.eclipse.jetty.websocket.client.WebSocketClient;
@WebServlet("/")
public class WebSocketClientServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
private WebSocketClient client;
WebSocketClient client = null;
try
@Override
public void init() throws ServletException
{
client = new WebSocketClient();
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");
try
{
client.start();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
finally
{
if (client != null)
{
WebSocketClient finalClient = client;
runThrowExceptionsAsRuntime(() -> finalClient.stop());
}
throw new ServletException(e);
}
}
public interface ThrowingRunnable
{
void run() throws Exception;
}
public void runThrowExceptionsAsRuntime(ThrowingRunnable runnable)
@Override
public void destroy()
{
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)
{
@ -122,4 +125,10 @@ public class WebSocketClientServlet extends HttpServlet
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">
<Arg>
<New class="org.eclipse.jetty.util.ssl.SslContextFactory$Client">
<Set name="trustAll" type="java.lang.Boolean">false</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>
<Set name="trustAll" type="java.lang.Boolean">true</Set>
</New>
</Arg>
<Set name="connectTimeout">4999</Set>