Issue #3446 - jetty-websocket tests for WebSocketUpgradeFilter
Signed-off-by: lachan-roberts <lachlan@webtide.com>
This commit is contained in:
parent
a01fc193a1
commit
265e0be6f7
|
@ -29,7 +29,7 @@ import org.eclipse.jetty.util.log.Log;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
import org.eclipse.jetty.websocket.api.Session;
|
import org.eclipse.jetty.websocket.api.Session;
|
||||||
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
|
import org.eclipse.jetty.websocket.api.WebSocketAdapter;
|
||||||
import org.eclipse.jetty.websocket.common.WebSocketSessionImpl;
|
import org.eclipse.jetty.websocket.common.WebSocketSession;
|
||||||
import org.eclipse.jetty.websocket.core.internal.WebSocketChannel;
|
import org.eclipse.jetty.websocket.core.internal.WebSocketChannel;
|
||||||
import org.eclipse.jetty.websocket.core.internal.WebSocketConnection;
|
import org.eclipse.jetty.websocket.core.internal.WebSocketConnection;
|
||||||
import org.hamcrest.Matcher;
|
import org.hamcrest.Matcher;
|
||||||
|
@ -117,9 +117,9 @@ public class CloseTrackingEndpoint extends WebSocketAdapter
|
||||||
public EndPoint getEndPoint()
|
public EndPoint getEndPoint()
|
||||||
{
|
{
|
||||||
Session session = getSession();
|
Session session = getSession();
|
||||||
assertThat("Session type", session, instanceOf(WebSocketSessionImpl.class));
|
assertThat("Session type", session, instanceOf(WebSocketSession.class));
|
||||||
|
|
||||||
WebSocketSessionImpl wsSession = (WebSocketSessionImpl) session;
|
WebSocketSession wsSession = (WebSocketSession) session;
|
||||||
WebSocketChannel wsChannel = (WebSocketChannel) wsSession.getCoreSession();
|
WebSocketChannel wsChannel = (WebSocketChannel) wsSession.getCoreSession();
|
||||||
WebSocketConnection wsConnection = wsChannel.getConnection();
|
WebSocketConnection wsConnection = wsChannel.getConnection();
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,79 @@
|
||||||
|
package org.eclipse.jetty.websocket.tests;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.concurrent.BlockingQueue;
|
||||||
|
import java.util.concurrent.CountDownLatch;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.util.BlockingArrayQueue;
|
||||||
|
import org.eclipse.jetty.util.log.Log;
|
||||||
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
|
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;
|
||||||
|
|
||||||
|
@WebSocket
|
||||||
|
public class EventSocket
|
||||||
|
{
|
||||||
|
private static Logger LOG = Log.getLogger(EventSocket.class);
|
||||||
|
|
||||||
|
protected Session session;
|
||||||
|
private String behavior;
|
||||||
|
public volatile Throwable failure = null;
|
||||||
|
|
||||||
|
public BlockingQueue<String> receivedMessages = new BlockingArrayQueue<>();
|
||||||
|
|
||||||
|
public CountDownLatch open = new CountDownLatch(1);
|
||||||
|
public CountDownLatch error = new CountDownLatch(1);
|
||||||
|
public CountDownLatch closed = new CountDownLatch(1);
|
||||||
|
|
||||||
|
@OnWebSocketConnect
|
||||||
|
public void onOpen(Session session)
|
||||||
|
{
|
||||||
|
this.session = session;
|
||||||
|
behavior = session.getPolicy().getBehavior().name();
|
||||||
|
LOG.info("{} onOpen(): {}", toString(), session);
|
||||||
|
open.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketMessage
|
||||||
|
public void onMessage(String message) throws IOException
|
||||||
|
{
|
||||||
|
LOG.info("{} onMessage(): {}", toString(), message);
|
||||||
|
receivedMessages.offer(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketClose
|
||||||
|
public void onClose(int statusCode, String reason)
|
||||||
|
{
|
||||||
|
LOG.info("{} onClose(): {}:{}", toString(), statusCode, reason);
|
||||||
|
closed.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnWebSocketError
|
||||||
|
public void onError(Throwable cause)
|
||||||
|
{
|
||||||
|
LOG.info("{} onError(): {}", toString(), cause);
|
||||||
|
failure = cause;
|
||||||
|
error.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString()
|
||||||
|
{
|
||||||
|
return String.format("[%s@%s]", behavior, Integer.toHexString(hashCode()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@WebSocket
|
||||||
|
public static class EchoSocket extends EventSocket
|
||||||
|
{
|
||||||
|
@Override
|
||||||
|
public void onMessage(String message) throws IOException
|
||||||
|
{
|
||||||
|
super.onMessage(message);
|
||||||
|
session.getRemote().sendStringByFuture(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,87 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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.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.client.WebSocketClient;
|
||||||
|
import org.eclipse.jetty.websocket.server.JettyWebSocketServerContainer;
|
||||||
|
import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer;
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class JettyWebSocketFilterTest
|
||||||
|
{
|
||||||
|
Server server;
|
||||||
|
WebSocketClient client;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void start() throws Exception
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
|
||||||
|
JettyWebSocketServerContainer container = JettyWebSocketServletContainerInitializer.configureContext(contextHandler);
|
||||||
|
container.addMapping("/", (req, resp)->new EventSocket.EchoSocket());
|
||||||
|
server.start();
|
||||||
|
|
||||||
|
client = new WebSocketClient();
|
||||||
|
client.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void stop() throws Exception
|
||||||
|
{
|
||||||
|
client.stop();
|
||||||
|
server.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test() throws Exception
|
||||||
|
{
|
||||||
|
URI uri = URI.create("ws://localhost:8080/filterPath");
|
||||||
|
EventSocket socket = new EventSocket();
|
||||||
|
CompletableFuture<Session> 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));
|
||||||
|
|
||||||
|
String msg = socket.receivedMessages.poll();
|
||||||
|
assertThat(msg, is("hello world"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,18 +20,12 @@ package org.eclipse.jetty.websocket.tests;
|
||||||
|
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.server.ServerConnector;
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
import org.eclipse.jetty.servlet.ServletContextHandler;
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
import org.eclipse.jetty.websocket.api.Session;
|
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.client.WebSocketClient;
|
||||||
import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer;
|
import org.eclipse.jetty.websocket.server.JettyWebSocketServletContainerInitializer;
|
||||||
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
|
import org.eclipse.jetty.websocket.servlet.WebSocketServlet;
|
||||||
|
@ -40,64 +34,24 @@ import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
public class JettyWebSocketTest
|
public class JettyWebSocketServletTest
|
||||||
{
|
{
|
||||||
|
|
||||||
@WebSocket
|
|
||||||
public static class EventSocket
|
|
||||||
{
|
|
||||||
CountDownLatch closed = new CountDownLatch(1);
|
|
||||||
|
|
||||||
String behavior;
|
|
||||||
|
|
||||||
@OnWebSocketConnect
|
|
||||||
public void onOpen(Session sess)
|
|
||||||
{
|
|
||||||
behavior = sess.getPolicy().getBehavior().name();
|
|
||||||
System.err.println(toString() + " Socket Connected: " + sess);
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnWebSocketMessage
|
|
||||||
public void onMessage(String message)
|
|
||||||
{
|
|
||||||
System.err.println(toString() + " Received TEXT message: " + message);
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnWebSocketClose
|
|
||||||
public void onClose(int statusCode, String reason)
|
|
||||||
{
|
|
||||||
System.err.println(toString() + " Socket Closed: " + statusCode + ":" + reason);
|
|
||||||
closed.countDown();
|
|
||||||
}
|
|
||||||
|
|
||||||
@OnWebSocketError
|
|
||||||
public void onError(Throwable cause)
|
|
||||||
{
|
|
||||||
cause.printStackTrace(System.err);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public String toString()
|
|
||||||
{
|
|
||||||
return String.format("[%s@%s]", behavior, Integer.toHexString(hashCode()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class MyWebSocketServlet extends WebSocketServlet
|
public static class MyWebSocketServlet extends WebSocketServlet
|
||||||
{
|
{
|
||||||
@Override
|
@Override
|
||||||
public void configure(WebSocketServletFactory factory)
|
public void configure(WebSocketServletFactory factory)
|
||||||
{
|
{
|
||||||
factory.addMapping("/",(req, resp)->new EventSocket());
|
factory.addMapping("/",(req, resp)->new EventSocket.EchoSocket());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Server server;
|
Server server;
|
||||||
WebSocketClient client;
|
WebSocketClient client;
|
||||||
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void start() throws Exception
|
public void start() throws Exception
|
||||||
{
|
{
|
||||||
|
@ -110,8 +64,7 @@ public class JettyWebSocketTest
|
||||||
contextHandler.setContextPath("/");
|
contextHandler.setContextPath("/");
|
||||||
server.setHandler(contextHandler);
|
server.setHandler(contextHandler);
|
||||||
|
|
||||||
contextHandler.addServlet(MyWebSocketServlet.class, "/testPath1");
|
contextHandler.addServlet(MyWebSocketServlet.class, "/servletPath");
|
||||||
contextHandler.addServlet(MyWebSocketServlet.class, "/testPath2");
|
|
||||||
|
|
||||||
JettyWebSocketServletContainerInitializer.configureContext(contextHandler);
|
JettyWebSocketServletContainerInitializer.configureContext(contextHandler);
|
||||||
server.start();
|
server.start();
|
||||||
|
@ -131,7 +84,7 @@ public class JettyWebSocketTest
|
||||||
@Test
|
@Test
|
||||||
public void test() throws Exception
|
public void test() throws Exception
|
||||||
{
|
{
|
||||||
URI uri = URI.create("ws://localhost:8080/testPath1");
|
URI uri = URI.create("ws://localhost:8080/servletPath");
|
||||||
EventSocket socket = new EventSocket();
|
EventSocket socket = new EventSocket();
|
||||||
CompletableFuture<Session> connect = client.connect(socket, uri);
|
CompletableFuture<Session> connect = client.connect(socket, uri);
|
||||||
try(Session session = connect.get(5, TimeUnit.SECONDS))
|
try(Session session = connect.get(5, TimeUnit.SECONDS))
|
||||||
|
@ -140,15 +93,7 @@ public class JettyWebSocketTest
|
||||||
}
|
}
|
||||||
assertTrue(socket.closed.await(10, TimeUnit.SECONDS));
|
assertTrue(socket.closed.await(10, TimeUnit.SECONDS));
|
||||||
|
|
||||||
|
String msg = socket.receivedMessages.poll();
|
||||||
uri = URI.create("ws://localhost:8080/testPath2");
|
assertThat(msg, is("hello world"));
|
||||||
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));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -20,6 +20,7 @@
|
||||||
# org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.Slf4jLog
|
# 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.util.log.class=org.eclipse.jetty.util.log.StdErrLog
|
||||||
org.eclipse.jetty.LEVEL=WARN
|
org.eclipse.jetty.LEVEL=WARN
|
||||||
|
org.eclipse.jetty.websocket.tests.LEVEL=DEBUG
|
||||||
# org.eclipse.jetty.util.log.stderr.LONG=true
|
# org.eclipse.jetty.util.log.stderr.LONG=true
|
||||||
# org.eclipse.jetty.server.AbstractConnector.LEVEL=DEBUG
|
# org.eclipse.jetty.server.AbstractConnector.LEVEL=DEBUG
|
||||||
# org.eclipse.jetty.io.WriteFlusher.LEVEL=DEBUG
|
# org.eclipse.jetty.io.WriteFlusher.LEVEL=DEBUG
|
||||||
|
|
Loading…
Reference in New Issue