diff --git a/jetty-websocket/pom.xml b/jetty-websocket/pom.xml index 5f6a256d041..c4dd30cd7df 100644 --- a/jetty-websocket/pom.xml +++ b/jetty-websocket/pom.xml @@ -17,6 +17,7 @@ websocket-core websocket-servlet + websocket-tests jetty-websocket-api jetty-websocket-common diff --git a/jetty-websocket/websocket-tests/pom.xml b/jetty-websocket/websocket-tests/pom.xml new file mode 100644 index 00000000000..57c0e967300 --- /dev/null +++ b/jetty-websocket/websocket-tests/pom.xml @@ -0,0 +1,140 @@ + + + + org.eclipse.jetty.websocket + websocket-parent + 10.0.0-SNAPSHOT + + + 4.0.0 + websocket-tests + Jetty :: Websocket :: Tests + + + ${project.groupId}.tests + + + + + + javax.websocket + javax.websocket-api + + + + org.eclipse.jetty.websocket + jetty-websocket-api + ${project.version} + + + + org.eclipse.jetty.websocket + javax-websocket-server + ${project.version} + + + + + org.eclipse.jetty.websocket + websocket-server + ${project.version} + + + + org.eclipse.jetty.websocket + websocket-client + ${project.version} + + + + org.eclipse.jetty.toolchain + jetty-test-helper + + + org.eclipse.jetty.websocket + websocket-server + 10.0.0-SNAPSHOT + test + + + + + + + + org.apache.maven.plugins + maven-jar-plugin + + + artifact-jars + + test-jar + + + + + + org.apache.maven.plugins + maven-enforcer-plugin + + + ban-ws-apis + + enforce + + + + + + org.eclipse.jetty.websocket:jetty-websocket-api + javax.websocket + + + + + + + ban-java-servlet-api + + enforce + + + + + + javax.servlet + servletapi + org.eclipse.jetty.orbit:javax.servlet + org.mortbay.jetty:servlet-api + jetty:servlet-api + + + + + + + + + org.apache.felix + maven-bundle-plugin + true + + + + manifest + + + + javax.websocket.servlet Implementation + + org.eclipse.jetty.websocket.servlet.*;version="${parsedVersion.majorVersion}.${parsedVersion.minorVersion}.${parsedVersion.incrementalVersion}" + + + + + + + + + diff --git a/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JavaxWebsocketTest.java b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JavaxWebsocketTest.java new file mode 100644 index 00000000000..b09aeea8d55 --- /dev/null +++ b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JavaxWebsocketTest.java @@ -0,0 +1,113 @@ +// +// ======================================================================== +// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.tests; + +import java.net.URI; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import javax.websocket.ClientEndpoint; +import javax.websocket.CloseReason; +import javax.websocket.ContainerProvider; +import javax.websocket.OnClose; +import javax.websocket.OnError; +import javax.websocket.OnMessage; +import javax.websocket.OnOpen; +import javax.websocket.Session; +import javax.websocket.WebSocketContainer; +import javax.websocket.server.ServerContainer; +import javax.websocket.server.ServerEndpoint; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.websocket.javax.server.JavaxWebSocketServletContainerInitializer; +import org.junit.jupiter.api.Test; + +public class JavaxWebsocketTest +{ + + @ClientEndpoint + @ServerEndpoint("/path") + public static class EventSocket + { + CountDownLatch closed = new CountDownLatch(1); + + @OnOpen + public void onOpen(Session sess) + { + System.out.println("Socket Connected: " + sess); + } + + @OnMessage + public void onMessage(String message) + { + System.out.println("Received TEXT message: " + message); + } + + @OnClose + public void onClose(CloseReason reason) + { + System.out.println("Socket Closed: " + reason); + closed.countDown(); + } + + @OnError + public void onError(Throwable cause) + { + cause.printStackTrace(System.err); + } + } + + + @Test + public void test() throws Exception + { + Server server = new Server(); + ServerConnector connector = new ServerConnector(server); + connector.setPort(8080); + server.addConnector(connector); + + ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); + contextHandler.setContextPath("/"); + server.setHandler(contextHandler); + + try + { + ServerContainer serverContainer = JavaxWebSocketServletContainerInitializer.configureContext(contextHandler); + serverContainer.addEndpoint(EventSocket.class); + server.start(); + + URI uri = URI.create("ws://localhost:8080/path"); + WebSocketContainer clientContainer = ContainerProvider.getWebSocketContainer(); + + EventSocket clientEndpoint = new EventSocket(); + try(Session session = clientContainer.connectToServer(clientEndpoint, uri)) + { + session.getBasicRemote().sendText("hello world"); + } + + clientEndpoint.closed.await(10, TimeUnit.SECONDS); + server.stop(); + } + catch (Throwable t) + { + t.printStackTrace(); + } + } +} diff --git a/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebsocketTest.java b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebsocketTest.java new file mode 100644 index 00000000000..0d59c5858be --- /dev/null +++ b/jetty-websocket/websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/JettyWebsocketTest.java @@ -0,0 +1,161 @@ +// +// ======================================================================== +// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.tests; + +import java.net.URI; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; + +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.websocket.api.Session; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketClose; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketError; +import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage; +import org.eclipse.jetty.websocket.api.annotations.WebSocket; +import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer; +import org.eclipse.jetty.websocket.servlet.WebSocketServlet; +import org.eclipse.jetty.websocket.servlet.WebSocketServletFactory; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class JettyWebsocketTest +{ + + @WebSocket + public static class EventSocket + { + CountDownLatch closed = new CountDownLatch(1); + String id; + + public EventSocket() + { + id = ""; + } + + public EventSocket(String id) + { + this.id = id; + } + + @OnWebSocketConnect + public void onOpen(Session sess) + { + System.out.println("["+id+"]Socket Connected: " + sess); + } + + @OnWebSocketMessage + public void onMessage(String message) + { + System.out.println("["+id+"]Received TEXT message: " + message); + } + + @OnWebSocketClose + public void onClose(int statusCode, String reason) + { + System.out.println("["+id+"]Socket Closed: " + statusCode + ":" + reason); + closed.countDown(); + } + + @OnWebSocketError + public void onError(Throwable cause) + { + cause.printStackTrace(System.err); + } + } + + public static class MyWebSocketServlet1 extends WebSocketServlet + { + @Override + public void configure(WebSocketServletFactory factory) + { + System.err.println("Configuring MyWebSocketServlet1"); + factory.addMapping("/",(req, resp)->new EventSocket("MyWebSocketServlet1")); + } + } + + public static class MyWebSocketServlet2 extends WebSocketServlet + { + @Override + public void configure(WebSocketServletFactory factory) + { + System.err.println("Configuring MyWebSocketServlet2"); + factory.addMapping("/",(req, resp)->new EventSocket("MyWebSocketServlet2")); + } + } + + + @Test + public void test() throws Exception + { + Server server = new Server(); + ServerConnector connector = new ServerConnector(server); + connector.setPort(8080); + server.addConnector(connector); + + ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS); + contextHandler.setContextPath("/"); + server.setHandler(contextHandler); + + contextHandler.addServlet(MyWebSocketServlet1.class, "/testPath1"); + contextHandler.addServlet(MyWebSocketServlet2.class, "/testPath2"); + + try + { + JettyWebSocketServletContainerInitializer.configure(contextHandler); + server.start(); + + URI uri = URI.create("ws://localhost:8080/testPath1"); + + WebSocketClient client = new WebSocketClient(); + client.start(); + + + EventSocket socket = new EventSocket(); + CompletableFuture connect = client.connect(socket, uri); + try(Session session = connect.get(5, TimeUnit.SECONDS)) + { + session.getRemote().sendString("hello world"); + } + assertTrue(socket.closed.await(10, TimeUnit.SECONDS)); + + + uri = URI.create("ws://localhost:8080/testPath2"); + socket = new EventSocket(); + connect = client.connect(socket, uri); + try(Session session = connect.get(5, TimeUnit.SECONDS)) + { + session.getRemote().sendString("hello world"); + } + assertTrue(socket.closed.await(10, TimeUnit.SECONDS)); + + + server.stop(); + } + catch (Throwable t) + { + t.printStackTrace(); + } + } +} diff --git a/jetty-websocket/websocket-tests/src/test/resources/jetty-logging.properties b/jetty-websocket/websocket-tests/src/test/resources/jetty-logging.properties new file mode 100644 index 00000000000..d51a5031cea --- /dev/null +++ b/jetty-websocket/websocket-tests/src/test/resources/jetty-logging.properties @@ -0,0 +1,43 @@ +# +# +# ======================================================================== +# Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd. +# ------------------------------------------------------------------------ +# All rights reserved. This program and the accompanying materials +# are made available under the terms of the Eclipse Public License v1.0 +# and Apache License v2.0 which accompanies this distribution. +# +# The Eclipse Public License is available at +# http://www.eclipse.org/legal/epl-v10.html +# +# The Apache License v2.0 is available at +# http://www.opensource.org/licenses/apache2.0.php +# +# You may elect to redistribute this code under either of these licenses. +# ======================================================================== +# +# +# org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog +org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog +org.eclipse.jetty.LEVEL=WARN +# org.eclipse.jetty.util.log.stderr.LONG=true +# org.eclipse.jetty.server.AbstractConnector.LEVEL=DEBUG +# org.eclipse.jetty.io.WriteFlusher.LEVEL=DEBUG +# org.eclipse.jetty.io.FillInterest.LEVEL=DEBUG +# org.eclipse.jetty.client.LEVEL=DEBUG +# org.eclipse.jetty.io.LEVEL=DEBUG +# org.eclipse.jetty.io.ManagedSelector.LEVEL=INFO +# org.eclipse.jetty.websocket.LEVEL=DEBUG +# org.eclipse.jetty.websocket.core.internal.WebSocketChannel.LEVEL=DEBUG +# org.eclipse.jetty.websocket.jsr356.tests.LEVEL=DEBUG +# org.eclipse.jetty.websocket.LEVEL=INFO +# org.eclipse.jetty.websocket.jsr356.messages.LEVEL=DEBUG +# org.eclipse.jetty.websocket.tests.LEVEL=DEBUG +# org.eclipse.jetty.websocket.tests.client.LEVEL=DEBUG +# org.eclipse.jetty.websocket.tests.client.jsr356.LEVEL=DEBUG +# org.eclipse.jetty.websocket.tests.server.LEVEL=DEBUG +# org.eclipse.jetty.websocket.tests.server.jsr356.LEVEL=DEBUG +### Showing any unintended (ignored) errors from CompletionCallback +# org.eclipse.jetty.websocket.common.CompletionCallback.LEVEL=ALL +### Disabling intentional error out of RFCSocket +org.eclipse.jetty.websocket.tests.server.RFCSocket.LEVEL=OFF