From 7db1abafbf33fbde5845d9a1c0ad3d0d4b5c0b58 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 14 Sep 2018 10:46:06 -0500 Subject: [PATCH] Fixing WebSocketUpgradeFilter Tests Signed-off-by: Joakim Erdfelt --- .../jetty/websocket/tests/LocalServer.java | 2 +- .../jetty/websocket/tests/WSServer.java | 9 +- .../WebSocketUpgradeFilterEmbeddedTest.java | 8 +- .../WebSocketUpgradeFilterWebappTest.java | 94 ++++++++----------- 4 files changed, 53 insertions(+), 60 deletions(-) diff --git a/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/LocalServer.java b/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/LocalServer.java index b54005fbb71..8c6e104277a 100644 --- a/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/LocalServer.java +++ b/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/LocalServer.java @@ -50,7 +50,7 @@ public class LocalServer extends ContainerLifeCycle implements LocalFuzzer.Provi private Server server; private ServerConnector connector; private LocalConnector localConnector; - private ServletContextHandler servletContextHandler; + protected ServletContextHandler servletContextHandler; private URI serverUri; private boolean ssl = false; private SslContextFactory sslContextFactory; diff --git a/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java b/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java index 4c7e214251b..8d3e6ab5ab9 100644 --- a/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java +++ b/jetty-websocket/websocket-tests/src/main/java/org/eclipse/jetty/websocket/tests/WSServer.java @@ -148,7 +148,7 @@ public class WSServer extends LocalServer implements LocalFuzzer.Provider { copyWebInf("empty-web.xml"); } - + public void deployWebapp(WebAppContext webapp) throws Exception { contexts.addHandler(webapp); @@ -160,7 +160,12 @@ public class WSServer extends LocalServer implements LocalFuzzer.Provider LOG.debug("{}", webapp.dump()); } } - + + public ContextHandlerCollection getContexts() + { + return contexts; + } + public Path getWebAppDir() { return this.contextDir; diff --git a/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterEmbeddedTest.java b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterEmbeddedTest.java index 8928a3252cf..be69ddc2d04 100644 --- a/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterEmbeddedTest.java +++ b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterEmbeddedTest.java @@ -166,6 +166,7 @@ public class WebSocketUpgradeFilterEmbeddedTest server.stop(); } + @SuppressWarnings("Duplicates") @ParameterizedTest(name = "[{index}] {0}") @MethodSource("scenarios") public void testNormalConfiguration(String testId, ContextProvider serverProvider) throws Exception @@ -190,6 +191,7 @@ public class WebSocketUpgradeFilterEmbeddedTest } } + @SuppressWarnings("Duplicates") @ParameterizedTest(name = "[{index}] {0}") @MethodSource("scenarios") public void testStopStartOfHandler(String testId, ContextProvider serverProvider) throws Exception @@ -243,13 +245,15 @@ public class WebSocketUpgradeFilterEmbeddedTest @Override protected Handler createRootHandler(Server server) throws Exception { - return contextProvider.configureRootHandler(server); + servletContextHandler = contextProvider.newServletContextHandler(server); + return servletContextHandler; } }; + this.server.start(); } interface ContextProvider { - Handler configureRootHandler(Server server) throws Exception; + ServletContextHandler newServletContextHandler(Server server) throws Exception; } } diff --git a/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterWebappTest.java b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterWebappTest.java index fac59ad189b..e2b3e75682e 100644 --- a/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterWebappTest.java +++ b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/server/WebSocketUpgradeFilterWebappTest.java @@ -52,7 +52,7 @@ public class WebSocketUpgradeFilterWebappTest private static File getNewTestDir() { - File testDir = MavenTestingUtils.getTargetTestingDir("WSUF-webxml-" + uniqTestDirId.getAndIncrement()); + File testDir = MavenTestingUtils.getTargetTestingDir("tests/WSUF-webxml-" + uniqTestDirId.getAndIncrement()); FS.ensureDirExists(testDir); return testDir; } @@ -66,61 +66,37 @@ public class WebSocketUpgradeFilterWebappTest { List cases = new ArrayList<>(); - // WSUF from web.xml, SCI active, apply app-ws configuration via ServletContextListener - - cases.add(Arguments.of("wsuf/WebAppContext/web.xml/ServletContextListener", (ServerConfiguration) (server) -> - { - server.copyWebInf("wsuf-config-via-listener.xml"); - server.copyClass(InfoSocket.class); - server.copyClass(InfoContextAttributeListener.class); - server.start(); - - WebAppContext webapp = server.createWebAppContext(); - server.deployWebapp(webapp); - })); - - // WSUF from web.xml, SCI active, apply app-ws configuration via ServletContextListener with WEB-INF/lib/jetty-http.jar - - cases.add(Arguments.of("wsuf/WebAppContext/web.xml/ServletContextListener/jetty-http.jar", (ServerConfiguration) (server) -> - { - server.copyWebInf("wsuf-config-via-listener.xml"); - server.copyClass(InfoSocket.class); - server.copyClass(InfoContextAttributeListener.class); - // Add a jetty-http.jar to ensure that the classloader constraints - // and the WebAppClassloader setup is sane and correct - // The odd version string is present to capture bad regex behavior in Jetty - server.copyLib(PathSpec.class, "jetty-http-9.99.999.jar"); - server.start(); - - WebAppContext webapp = server.createWebAppContext(); - server.deployWebapp(webapp); - })); - // WSUF from web.xml, SCI active, apply app-ws configuration via Servlet.init - cases.add(Arguments.of("wsuf/WebAppContext/web.xml/Servlet.init", (ServerConfiguration) (server) -> - { - server.copyWebInf("wsuf-config-via-servlet-init.xml"); - server.copyClass(InfoSocket.class); - server.copyClass(InfoServlet.class); - server.start(); + cases.add(Arguments.of( + "wsuf/WebAppContext/web.xml/Servlet.init", + (ServerConfiguration) (server) -> + { + server.copyWebInf("wsuf-config-via-servlet-init.xml"); + server.copyClass(InfoSocket.class); + server.copyClass(InfoServlet.class); + server.start(); - WebAppContext webapp = server.createWebAppContext(); - server.deployWebapp(webapp); - })); + WebAppContext webapp = server.createWebAppContext(); + server.deployWebapp(webapp); + } + )); // xml based, wsuf, on alternate url-pattern and config attribute location - cases.add(Arguments.of("wsuf/WebAppContext/web.xml/ServletContextListener/alt-config", (ServerConfiguration) (server) -> - { - server.copyWebInf("wsuf-alt-config-via-listener.xml"); - server.copyClass(InfoSocket.class); - server.copyClass(InfoContextAltAttributeListener.class); - server.start(); + cases.add(Arguments.of( + "wsuf/WebAppContext/web.xml/ServletContextListener/alt-config", + (ServerConfiguration) (server) -> + { + server.copyWebInf("wsuf-alt-config-via-listener.xml"); + server.copyClass(InfoSocket.class); + server.copyClass(InfoContextAltAttributeListener.class); + server.start(); - WebAppContext webapp = server.createWebAppContext(); - server.deployWebapp(webapp); - })); + WebAppContext webapp = server.createWebAppContext(); + server.deployWebapp(webapp); + } + )); // WSUF from web.xml, SCI active, apply app-ws configuration via ServletContextListener @@ -132,6 +108,9 @@ public class WebSocketUpgradeFilterWebappTest server.copyClass(InfoSocket.class); server.copyClass(InfoContextAttributeListener.class); server.start(); + + WebAppContext webapp = server.createWebAppContext(); + server.deployWebapp(webapp); } )); @@ -149,6 +128,9 @@ public class WebSocketUpgradeFilterWebappTest // The odd version string is present to capture bad regex behavior in Jetty server.copyLib(PathSpec.class, "jetty-http-9.99.999.jar"); server.start(); + + WebAppContext webapp = server.createWebAppContext(); + server.deployWebapp(webapp); } )); @@ -157,7 +139,7 @@ public class WebSocketUpgradeFilterWebappTest private void startServer(ServerConfiguration serverConfiguration) throws Exception { - WSServer server = new WSServer(getNewTestDir(), "/"); + server = new WSServer(getNewTestDir(), "app"); serverConfiguration.configure(server); } @@ -169,13 +151,14 @@ public class WebSocketUpgradeFilterWebappTest server.stop(); } + @SuppressWarnings("Duplicates") @ParameterizedTest(name = "[{index}] {0}") @MethodSource("scenarios") public void testNormalConfiguration(String testId, ServerConfiguration serverConfiguration) throws Exception { startServer(serverConfiguration); - try (LocalFuzzer session = server.newLocalFuzzer("/info/")) + try (LocalFuzzer session = server.newLocalFuzzer("/app/info/")) { session.sendFrames( new TextFrame().setPayload("hello"), @@ -193,13 +176,14 @@ public class WebSocketUpgradeFilterWebappTest } } + @SuppressWarnings("Duplicates") @ParameterizedTest(name = "[{index}] {0}") @MethodSource("scenarios") public void testStopStartOfHandler(String testId, ServerConfiguration serverConfiguration) throws Exception { startServer(serverConfiguration); - try (LocalFuzzer session = server.newLocalFuzzer("/info/")) + try (LocalFuzzer session = server.newLocalFuzzer("/app/info/")) { session.sendFrames( new TextFrame().setPayload("hello 1"), @@ -216,12 +200,12 @@ public class WebSocketUpgradeFilterWebappTest assertThat("Frame.text-payload", frame.getPayloadAsUTF8(), containsString("session.maxTextMessageSize=" + (10 * 1024 * 1024))); } - server.getServletContextHandler().stop(); - server.getServletContextHandler().start(); + server.getContexts().stop(); + server.getContexts().start(); // Make request again (server should have retained websocket configuration) - try (LocalFuzzer session = server.newLocalFuzzer("/info/")) + try (LocalFuzzer session = server.newLocalFuzzer("/app/info/")) { session.sendFrames( new TextFrame().setPayload("hello 2"),