Search in sources :

Example 11 with BasicText

use of org.apache.tomcat.websocket.TesterMessageCountClient.BasicText in project tomcat by apache.

the class TestWebSocketFrameClient method echoTester.

public void echoTester(String path) throws Exception {
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + path));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(1, messages.size());
    for (String message : messages) {
        Assert.assertEquals("Hello", message);
    }
    wsSession.close();
}
Also used : BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session)

Example 12 with BasicText

use of org.apache.tomcat.websocket.TesterMessageCountClient.BasicText in project tomcat70 by apache.

the class TestWebSocketFrameClientSSL method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterFirehoseServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    TesterSupport.initSsl(tomcat);
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    URL truststoreUrl = this.getClass().getClassLoader().getResource(TesterSupport.CA_JKS);
    File truststoreFile = new File(truststoreUrl.toURI());
    clientEndpointConfig.getUserProperties().put(WsWebSocketContainer.SSL_TRUSTSTORE_PROPERTY, truststoreFile.getAbsolutePath());
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("wss://localhost:" + getPort() + TesterFirehoseServer.Config.PATH));
    CountDownLatch latch = new CountDownLatch(TesterFirehoseServer.MESSAGE_COUNT);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    System.out.println("Sent Hello message, waiting for data");
    // Ignore the latch result as the message count test below will tell us
    // if the right number of messages arrived
    handler.getLatch().await(TesterFirehoseServer.WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(TesterFirehoseServer.MESSAGE_COUNT, messages.size());
    for (String message : messages) {
        Assert.assertEquals(TesterFirehoseServer.MESSAGE, message);
    }
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) URL(java.net.URL) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) File(java.io.File) Session(javax.websocket.Session) Test(org.junit.Test)

Example 13 with BasicText

use of org.apache.tomcat.websocket.TesterMessageCountClient.BasicText in project tomcat70 by apache.

the class TestWsServerContainer method testBug58232.

@Test
public void testBug58232() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(Bug54807Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    tomcat.start();
    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
    SimpleClient client = new SimpleClient();
    URI uri = new URI("ws://localhost:" + getPort() + "/echoBasic");
    Session session = null;
    try {
        session = wsContainer.connectToServer(client, uri);
        CountDownLatch latch = new CountDownLatch(1);
        BasicText handler = new BasicText(latch);
        session.addMessageHandler(handler);
        session.getBasicRemote().sendText("echoBasic");
        boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
        Assert.assertTrue(latchResult);
        Queue<String> messages = handler.getMessages();
        Assert.assertEquals(1, messages.size());
        for (String message : messages) {
            Assert.assertEquals("echoBasic", message);
        }
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
Also used : TesterServletContext(org.apache.tomcat.unittest.TesterServletContext) Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) SimpleClient(org.apache.tomcat.websocket.pojo.TesterUtil.SimpleClient) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(javax.websocket.Session) Test(org.junit.Test) WebSocketBaseTest(org.apache.tomcat.websocket.WebSocketBaseTest)

Example 14 with BasicText

use of org.apache.tomcat.websocket.TesterMessageCountClient.BasicText in project tomcat70 by apache.

the class TestWsWebSocketContainer method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterEchoServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    // Set this artificially small to trigger
    // https://bz.apache.org/bugzilla/show_bug.cgi?id=57054
    wsContainer.setDefaultMaxBinaryMessageBufferSize(64);
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().build(), new URI("ws://" + getHostName() + ":" + getPort() + TesterEchoServer.Config.PATH_ASYNC));
    CountDownLatch latch = new CountDownLatch(1);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText(MESSAGE_STRING_1);
    boolean latchResult = handler.getLatch().await(10, TimeUnit.SECONDS);
    Assert.assertTrue(latchResult);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(MESSAGE_STRING_1, messages.peek());
    ((WsWebSocketContainer) wsContainer).destroy();
}
Also used : Context(org.apache.catalina.Context) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) Session(javax.websocket.Session) Test(org.junit.Test)

Example 15 with BasicText

use of org.apache.tomcat.websocket.TesterMessageCountClient.BasicText in project tomcat70 by apache.

the class TestWebSocketFrameClient method testConnectToServerEndpoint.

@Test
public void testConnectToServerEndpoint() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    // No file system docBase required
    Context ctx = tomcat.addContext("", null);
    ctx.addApplicationListener(TesterFirehoseServer.Config.class.getName());
    Tomcat.addServlet(ctx, "default", new DefaultServlet());
    ctx.addServletMapping("/", "default");
    tomcat.start();
    WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
    Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + TesterFirehoseServer.Config.PATH));
    CountDownLatch latch = new CountDownLatch(TesterFirehoseServer.MESSAGE_COUNT);
    BasicText handler = new BasicText(latch);
    wsSession.addMessageHandler(handler);
    wsSession.getBasicRemote().sendText("Hello");
    System.out.println("Sent Hello message, waiting for data");
    // Ignore the latch result as the message count test below will tell us
    // if the right number of messages arrived
    handler.getLatch().await(TesterFirehoseServer.WAIT_TIME_MILLIS, TimeUnit.MILLISECONDS);
    Queue<String> messages = handler.getMessages();
    Assert.assertEquals(TesterFirehoseServer.MESSAGE_COUNT, messages.size());
    for (String message : messages) {
        Assert.assertEquals(TesterFirehoseServer.MESSAGE, message);
    }
}
Also used : Context(org.apache.catalina.Context) BasicText(org.apache.tomcat.websocket.TesterMessageCountClient.BasicText) Tomcat(org.apache.catalina.startup.Tomcat) WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) LoginConfig(org.apache.catalina.deploy.LoginConfig) DefaultServlet(org.apache.catalina.servlets.DefaultServlet) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

URI (java.net.URI)21 CountDownLatch (java.util.concurrent.CountDownLatch)21 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)21 Context (org.apache.catalina.Context)18 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)18 Tomcat (org.apache.catalina.startup.Tomcat)18 Test (org.junit.Test)14 Session (jakarta.websocket.Session)11 WebSocketContainer (jakarta.websocket.WebSocketContainer)11 Session (javax.websocket.Session)10 WebSocketContainer (javax.websocket.WebSocketContainer)10 ClientEndpointConfig (jakarta.websocket.ClientEndpointConfig)9 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)8 EndpointConfig (javax.websocket.EndpointConfig)5 ServerEndpointConfig (javax.websocket.server.ServerEndpointConfig)5 TesterProgrammaticEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterProgrammaticEndpoint)5 File (java.io.File)4 SSLContext (javax.net.ssl.SSLContext)4 TesterEndpoint (org.apache.tomcat.websocket.TesterMessageCountClient.TesterEndpoint)4 EndpointConfig (jakarta.websocket.EndpointConfig)3