From c8a1dacdd09b8445c2f144d6b2590186608dc6d9 Mon Sep 17 00:00:00 2001 From: Lachlan Roberts Date: Mon, 27 Apr 2020 10:54:06 +1000 Subject: [PATCH] Issue #4800 - Improve WS DistributionTest to also test working webapp Signed-off-by: Lachlan Roberts --- tests/test-distribution/pom.xml | 13 +++++ .../tests/distribution/DistributionTests.java | 51 ++++++++++++++++--- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/tests/test-distribution/pom.xml b/tests/test-distribution/pom.xml index 59abe41b08b..d7176621af5 100644 --- a/tests/test-distribution/pom.xml +++ b/tests/test-distribution/pom.xml @@ -113,6 +113,12 @@ ${project.version} test + + org.eclipse.jetty.websocket + websocket-jetty-client + ${project.version} + test + org.eclipse.jetty.tests test-felix-webapp @@ -120,6 +126,13 @@ test war + + org.eclipse.jetty.tests + test-websocket-webapp + ${project.version} + test + war + org.eclipse.jetty.tests test-bad-websocket-webapp diff --git a/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java b/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java index d9f98759c84..f791c760886 100644 --- a/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java +++ b/tests/test-distribution/src/test/java/org/eclipse/jetty/tests/distribution/DistributionTests.java @@ -20,12 +20,14 @@ package org.eclipse.jetty.tests.distribution; import java.io.BufferedWriter; import java.io.File; +import java.net.URI; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.nio.file.StandardOpenOption; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import org.eclipse.jetty.client.HttpClient; @@ -40,6 +42,9 @@ import org.eclipse.jetty.unixsocket.server.UnixSocketConnector; import org.eclipse.jetty.util.IO; import org.eclipse.jetty.util.StringUtil; import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.eclipse.jetty.websocket.api.StatusCode; +import org.eclipse.jetty.websocket.api.WebSocketListener; +import org.eclipse.jetty.websocket.client.WebSocketClient; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.condition.DisabledOnJre; import org.junit.jupiter.api.condition.JRE; @@ -48,6 +53,7 @@ import org.junit.jupiter.params.provider.ValueSource; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -426,9 +432,13 @@ public class DistributionTests extends AbstractDistributionTest assertTrue(run1.awaitFor(5, TimeUnit.SECONDS)); assertEquals(0, run1.getExitValue()); - File war = distribution.resolveArtifact("org.eclipse.jetty.tests:test-bad-websocket-webapp:war:" + jettyVersion); - distribution.installWarFile(war, "test1"); - distribution.installWarFile(war, "test2"); + File webApp = distribution.resolveArtifact("org.eclipse.jetty.tests:test-websocket-webapp:war:" + jettyVersion); + File badWebApp = distribution.resolveArtifact("org.eclipse.jetty.tests:test-bad-websocket-webapp:war:" + jettyVersion); + + distribution.installWarFile(webApp, "test1"); + distribution.installWarFile(badWebApp, "test2"); + distribution.installWarFile(badWebApp, "test3"); + distribution.installWarFile(webApp, "test4"); int port = distribution.freePort(); String[] args2 = { @@ -436,21 +446,50 @@ public class DistributionTests extends AbstractDistributionTest "jetty.http.port=" + port//, //"jetty.server.dumpAfterStart=true" }; + try (DistributionTester.Run run2 = distribution.start(args2)) { assertTrue(run2.awaitConsoleLogsFor("Started Server@", 10, TimeUnit.SECONDS)); assertFalse(run2.getLogs().stream().anyMatch(s -> s.contains("LinkageError"))); startHttpClient(); - ContentResponse response = client.GET("http://localhost:" + port + "/test1/index.jsp"); + WebSocketClient wsClient = new WebSocketClient(client); + wsClient.start(); + URI serverUri = URI.create("ws://localhost:" + port); + + // Verify /test1 is able to establish a WebSocket connection. + WsListener webSocketListener = new WsListener(); + wsClient.connect(webSocketListener, serverUri.resolve("/test1/onopen/a")).get(5, TimeUnit.SECONDS).close(); + assertTrue(webSocketListener.closeLatch.await(5, TimeUnit.SECONDS)); + assertThat(webSocketListener.closeCode, is(StatusCode.NORMAL)); + + // Verify that /test2 and /test3 could not be started. + ContentResponse response = client.GET(serverUri.resolve("/test2/badonopen/a")); + assertEquals(HttpStatus.SERVICE_UNAVAILABLE_503, response.getStatus()); + client.GET("http://localhost:" + port + "/test3/badonopen/a"); assertEquals(HttpStatus.SERVICE_UNAVAILABLE_503, response.getStatus()); - client.GET("http://localhost:" + port + "/test2/index.jsp"); - assertEquals(HttpStatus.SERVICE_UNAVAILABLE_503, response.getStatus()); + // Verify /test4 is able to establish a WebSocket connection. + webSocketListener = new WsListener(); + wsClient.connect(webSocketListener, serverUri.resolve("/test4/onopen/a")).get(5, TimeUnit.SECONDS).close(); + assertTrue(webSocketListener.closeLatch.await(5, TimeUnit.SECONDS)); + assertThat(webSocketListener.closeCode, is(StatusCode.NORMAL)); } } } + public static class WsListener implements WebSocketListener + { + private CountDownLatch closeLatch = new CountDownLatch(1); + private int closeCode; + + @Override + public void onWebSocketClose(int statusCode, String reason) + { + this.closeCode = statusCode; + } + } + @Test public void testStartStopLog4j2Modules() throws Exception {