Merge pull request #3366 from lachlan-roberts/jetty-10.0.x-3165-javax-websocket-server-tests
Issue #3165 - javax-websocket-server tests
This commit is contained in:
commit
dcb1cc663d
|
@ -50,6 +50,8 @@
|
||||||
<configuration>
|
<configuration>
|
||||||
<argLine>
|
<argLine>
|
||||||
@{argLine} ${jetty.surefire.argLine}
|
@{argLine} ${jetty.surefire.argLine}
|
||||||
|
--add-opens org.eclipse.jetty.websocket.javax.server/org.eclipse.jetty.websocket.javax.server.examples=org.eclipse.jetty.websocket.javax.common
|
||||||
|
--add-reads org.eclipse.jetty.websocket.javax.server=org.eclipse.jetty.security
|
||||||
--add-reads org.eclipse.jetty.websocket.javax.common=org.eclipse.jetty.websocket.javax.server
|
--add-reads org.eclipse.jetty.websocket.javax.common=org.eclipse.jetty.websocket.javax.server
|
||||||
</argLine>
|
</argLine>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
|
|
@ -0,0 +1,199 @@
|
||||||
|
//
|
||||||
|
// ========================================================================
|
||||||
|
// 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.javax.server;
|
||||||
|
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.concurrent.ArrayBlockingQueue;
|
||||||
|
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 org.eclipse.jetty.security.ConstraintMapping;
|
||||||
|
import org.eclipse.jetty.security.ConstraintSecurityHandler;
|
||||||
|
import org.eclipse.jetty.security.HashLoginService;
|
||||||
|
import org.eclipse.jetty.security.SecurityHandler;
|
||||||
|
import org.eclipse.jetty.security.UserStore;
|
||||||
|
import org.eclipse.jetty.security.authentication.BasicAuthenticator;
|
||||||
|
import org.eclipse.jetty.server.Server;
|
||||||
|
import org.eclipse.jetty.server.ServerConnector;
|
||||||
|
import org.eclipse.jetty.servlet.ServletContextHandler;
|
||||||
|
import org.eclipse.jetty.util.security.Constraint;
|
||||||
|
import org.eclipse.jetty.util.security.Credential;
|
||||||
|
import org.eclipse.jetty.websocket.javax.server.examples.GetHttpSessionSocket;
|
||||||
|
import org.eclipse.jetty.websocket.javax.server.examples.MyAuthedSocket;
|
||||||
|
import org.eclipse.jetty.websocket.javax.server.examples.StreamingEchoSocket;
|
||||||
|
import org.junit.jupiter.api.AfterAll;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
|
public class WebSocketServerExamplesTest
|
||||||
|
{
|
||||||
|
@ClientEndpoint
|
||||||
|
public static class ClientSocket
|
||||||
|
{
|
||||||
|
CountDownLatch closed = new CountDownLatch(1);
|
||||||
|
ArrayBlockingQueue<String> messageQueue = new ArrayBlockingQueue<>(2);
|
||||||
|
|
||||||
|
@OnOpen
|
||||||
|
public void onOpen(Session sess)
|
||||||
|
{
|
||||||
|
System.err.println("ClientSocket Connected: " + sess);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnMessage
|
||||||
|
public void onMessage(String message)
|
||||||
|
{
|
||||||
|
messageQueue.offer(message);
|
||||||
|
System.err.println("Received TEXT message: " + message);
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnClose
|
||||||
|
public void onClose(CloseReason closeReason)
|
||||||
|
{
|
||||||
|
System.err.println("ClientSocket Closed: " + closeReason);
|
||||||
|
closed.countDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@OnError
|
||||||
|
public void onError(Throwable cause)
|
||||||
|
{
|
||||||
|
cause.printStackTrace(System.err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static Server _server;
|
||||||
|
static ServletContextHandler _context;
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
public static void setup() throws Exception
|
||||||
|
{
|
||||||
|
_server = new Server();
|
||||||
|
ServerConnector connector = new ServerConnector(_server);
|
||||||
|
connector.setPort(8080);
|
||||||
|
_server.addConnector(connector);
|
||||||
|
|
||||||
|
_context = new ServletContextHandler(ServletContextHandler.SESSIONS);
|
||||||
|
_context.setContextPath("/");
|
||||||
|
_context.setSecurityHandler(getSecurityHandler("user", "password", "testRealm"));
|
||||||
|
_server.setHandler(_context);
|
||||||
|
|
||||||
|
ServerContainer serverContainer = JavaxWebSocketServletContainerInitializer.configureContext(_context);
|
||||||
|
serverContainer.addEndpoint(MyAuthedSocket.class);
|
||||||
|
serverContainer.addEndpoint(StreamingEchoSocket.class);
|
||||||
|
serverContainer.addEndpoint(GetHttpSessionSocket.class);
|
||||||
|
|
||||||
|
_server.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterAll
|
||||||
|
public static void stop() throws Exception
|
||||||
|
{
|
||||||
|
_server.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static SecurityHandler getSecurityHandler(String username, String password, String realm) {
|
||||||
|
|
||||||
|
HashLoginService loginService = new HashLoginService();
|
||||||
|
UserStore userStore = new UserStore();
|
||||||
|
userStore.addUser(username, Credential.getCredential(password), new String[] {"websocket"});
|
||||||
|
loginService.setUserStore(userStore);
|
||||||
|
loginService.setName(realm);
|
||||||
|
|
||||||
|
Constraint constraint = new Constraint();
|
||||||
|
constraint.setName("auth");
|
||||||
|
constraint.setAuthenticate(true);
|
||||||
|
constraint.setRoles(new String[]{"**"});
|
||||||
|
|
||||||
|
ConstraintMapping mapping = new ConstraintMapping();
|
||||||
|
mapping.setPathSpec("/secured/socket/*");
|
||||||
|
mapping.setConstraint(constraint);
|
||||||
|
|
||||||
|
ConstraintSecurityHandler security = new ConstraintSecurityHandler();
|
||||||
|
security.addConstraintMapping(mapping);
|
||||||
|
security.setAuthenticator(new BasicAuthenticator());
|
||||||
|
security.setLoginService(loginService);
|
||||||
|
|
||||||
|
return security;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMyAuthedSocket() throws Exception
|
||||||
|
{
|
||||||
|
//HttpClient is configured for BasicAuthentication with the XmlHttpClientProvider
|
||||||
|
URI uri = URI.create("ws://localhost:8080/secured/socket");
|
||||||
|
WebSocketContainer clientContainer = ContainerProvider.getWebSocketContainer();
|
||||||
|
|
||||||
|
ClientSocket clientEndpoint = new ClientSocket();
|
||||||
|
try(Session session = clientContainer.connectToServer(clientEndpoint, uri))
|
||||||
|
{
|
||||||
|
session.getBasicRemote().sendText("hello world");
|
||||||
|
}
|
||||||
|
clientEndpoint.closed.await(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
String msg = clientEndpoint.messageQueue.poll(5, TimeUnit.SECONDS);
|
||||||
|
assertThat(msg, is("hello world"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStreamingEchoSocket() throws Exception
|
||||||
|
{
|
||||||
|
URI uri = URI.create("ws://localhost:8080/echo");
|
||||||
|
WebSocketContainer clientContainer = ContainerProvider.getWebSocketContainer();
|
||||||
|
|
||||||
|
ClientSocket clientEndpoint = new ClientSocket();
|
||||||
|
try(Session session = clientContainer.connectToServer(clientEndpoint, uri))
|
||||||
|
{
|
||||||
|
session.getBasicRemote().sendText("hello world");
|
||||||
|
}
|
||||||
|
clientEndpoint.closed.await(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
String msg = clientEndpoint.messageQueue.poll(5, TimeUnit.SECONDS);
|
||||||
|
assertThat(msg, is("hello world"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetHttpSessionSocket() throws Exception
|
||||||
|
{
|
||||||
|
URI uri = URI.create("ws://localhost:8080/example");
|
||||||
|
WebSocketContainer clientContainer = ContainerProvider.getWebSocketContainer();
|
||||||
|
|
||||||
|
ClientSocket clientEndpoint = new ClientSocket();
|
||||||
|
try(Session session = clientContainer.connectToServer(clientEndpoint, uri))
|
||||||
|
{
|
||||||
|
session.getBasicRemote().sendText("hello world");
|
||||||
|
}
|
||||||
|
clientEndpoint.closed.await(5, TimeUnit.SECONDS);
|
||||||
|
|
||||||
|
String msg = clientEndpoint.messageQueue.poll(5, TimeUnit.SECONDS);
|
||||||
|
assertThat(msg, is("hello world"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,16 +16,17 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.browser;
|
||||||
|
|
||||||
import org.eclipse.jetty.http.QuotedCSV;
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.websocket.Extension;
|
import javax.websocket.Extension;
|
||||||
import javax.websocket.HandshakeResponse;
|
import javax.websocket.HandshakeResponse;
|
||||||
import javax.websocket.server.HandshakeRequest;
|
import javax.websocket.server.HandshakeRequest;
|
||||||
import javax.websocket.server.ServerEndpointConfig;
|
import javax.websocket.server.ServerEndpointConfig;
|
||||||
import java.util.Collections;
|
|
||||||
import java.util.List;
|
import org.eclipse.jetty.http.QuotedCSV;
|
||||||
|
|
||||||
public class JsrBrowserConfigurator extends ServerEndpointConfig.Configurator
|
public class JsrBrowserConfigurator extends ServerEndpointConfig.Configurator
|
||||||
{
|
{
|
|
@ -16,7 +16,7 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.browser;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
|
@ -16,11 +16,14 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.browser;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.StringUtil;
|
import java.text.DateFormat;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import java.text.SimpleDateFormat;
|
||||||
import org.eclipse.jetty.util.log.Logger;
|
import java.util.Calendar;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Random;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.websocket.CloseReason;
|
import javax.websocket.CloseReason;
|
||||||
import javax.websocket.OnClose;
|
import javax.websocket.OnClose;
|
||||||
|
@ -29,12 +32,10 @@ import javax.websocket.OnOpen;
|
||||||
import javax.websocket.RemoteEndpoint.Async;
|
import javax.websocket.RemoteEndpoint.Async;
|
||||||
import javax.websocket.Session;
|
import javax.websocket.Session;
|
||||||
import javax.websocket.server.ServerEndpoint;
|
import javax.websocket.server.ServerEndpoint;
|
||||||
import java.text.DateFormat;
|
|
||||||
import java.text.SimpleDateFormat;
|
import org.eclipse.jetty.util.StringUtil;
|
||||||
import java.util.Calendar;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
import java.util.Locale;
|
import org.eclipse.jetty.util.log.Logger;
|
||||||
import java.util.Random;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
@ServerEndpoint(value = "/", subprotocols = { "tool" }, configurator = JsrBrowserConfigurator.class)
|
@ServerEndpoint(value = "/", subprotocols = { "tool" }, configurator = JsrBrowserConfigurator.class)
|
||||||
public class JsrBrowserSocket
|
public class JsrBrowserSocket
|
|
@ -16,7 +16,7 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.examples;
|
||||||
|
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import javax.websocket.HandshakeResponse;
|
import javax.websocket.HandshakeResponse;
|
|
@ -16,7 +16,9 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.examples;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import javax.servlet.http.HttpSession;
|
import javax.servlet.http.HttpSession;
|
||||||
import javax.websocket.EndpointConfig;
|
import javax.websocket.EndpointConfig;
|
||||||
|
@ -24,7 +26,6 @@ import javax.websocket.OnMessage;
|
||||||
import javax.websocket.OnOpen;
|
import javax.websocket.OnOpen;
|
||||||
import javax.websocket.Session;
|
import javax.websocket.Session;
|
||||||
import javax.websocket.server.ServerEndpoint;
|
import javax.websocket.server.ServerEndpoint;
|
||||||
import java.io.IOException;
|
|
||||||
|
|
||||||
@ServerEndpoint(value = "/example", configurator = GetHttpSessionConfigurator.class)
|
@ServerEndpoint(value = "/example", configurator = GetHttpSessionConfigurator.class)
|
||||||
public class GetHttpSessionSocket
|
public class GetHttpSessionSocket
|
|
@ -16,12 +16,13 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.examples;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
|
||||||
import javax.websocket.HandshakeResponse;
|
import javax.websocket.HandshakeResponse;
|
||||||
import javax.websocket.server.HandshakeRequest;
|
import javax.websocket.server.HandshakeRequest;
|
||||||
import javax.websocket.server.ServerEndpointConfig;
|
import javax.websocket.server.ServerEndpointConfig;
|
||||||
import java.security.Principal;
|
|
||||||
|
|
||||||
public class MyAuthedConfigurator extends ServerEndpointConfig.Configurator
|
public class MyAuthedConfigurator extends ServerEndpointConfig.Configurator
|
||||||
{
|
{
|
|
@ -16,7 +16,7 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.examples;
|
||||||
|
|
||||||
import javax.websocket.OnMessage;
|
import javax.websocket.OnMessage;
|
||||||
import javax.websocket.server.ServerEndpoint;
|
import javax.websocket.server.ServerEndpoint;
|
|
@ -16,16 +16,17 @@
|
||||||
// ========================================================================
|
// ========================================================================
|
||||||
//
|
//
|
||||||
|
|
||||||
package examples;
|
package org.eclipse.jetty.websocket.javax.server.examples;
|
||||||
|
|
||||||
import org.eclipse.jetty.util.IO;
|
import java.io.IOException;
|
||||||
|
import java.io.Reader;
|
||||||
|
import java.io.Writer;
|
||||||
|
|
||||||
import javax.websocket.OnMessage;
|
import javax.websocket.OnMessage;
|
||||||
import javax.websocket.Session;
|
import javax.websocket.Session;
|
||||||
import javax.websocket.server.ServerEndpoint;
|
import javax.websocket.server.ServerEndpoint;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.Reader;
|
import org.eclipse.jetty.util.IO;
|
||||||
import java.io.Writer;
|
|
||||||
|
|
||||||
@ServerEndpoint("/echo")
|
@ServerEndpoint("/echo")
|
||||||
public class StreamingEchoSocket
|
public class StreamingEchoSocket
|
|
@ -0,0 +1,21 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
|
<Configure class="org.eclipse.jetty.client.HttpClient">
|
||||||
|
<Arg>
|
||||||
|
<New class="org.eclipse.jetty.util.ssl.SslContextFactory"/>
|
||||||
|
</Arg>
|
||||||
|
<Call name="getAuthenticationStore">
|
||||||
|
<Call name="addAuthentication">
|
||||||
|
<Arg>
|
||||||
|
<New class="org.eclipse.jetty.client.util.BasicAuthentication">
|
||||||
|
<Arg>
|
||||||
|
<New class="java.net.URI" arg="ws://localhost:8080/secured/socket"/>
|
||||||
|
</Arg>
|
||||||
|
<Arg>testRealm</Arg>
|
||||||
|
<Arg>user</Arg>
|
||||||
|
<Arg>password</Arg>
|
||||||
|
</New>
|
||||||
|
</Arg>
|
||||||
|
</Call>
|
||||||
|
</Call>
|
||||||
|
</Configure>
|
Loading…
Reference in New Issue