Search in sources :

Example 6 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project jetty.project by eclipse.

the class JsrBatchModeTest method testBatchModeOff.

@Test
public void testBatchModeOff() throws Exception {
    ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
    final CountDownLatch latch = new CountDownLatch(1);
    EndpointAdapter endpoint = new EndpointAdapter() {

        @Override
        public void onMessage(String message) {
            latch.countDown();
        }
    };
    try (Session session = client.connectToServer(endpoint, config, uri)) {
        RemoteEndpoint.Async remote = session.getAsyncRemote();
        remote.setBatchingAllowed(false);
        Future<Void> future = remote.sendText("batch_mode_off");
        // The write is immediate.
        future.get(1, TimeUnit.SECONDS);
        // Wait for the echo.
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : RemoteEndpoint(javax.websocket.RemoteEndpoint) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

Example 7 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project jetty.project by eclipse.

the class JsrBatchModeTest method testBatchModeAuto.

@Test
public void testBatchModeAuto() throws Exception {
    ClientEndpointConfig config = ClientEndpointConfig.Builder.create().build();
    URI uri = URI.create("ws://localhost:" + connector.getLocalPort());
    final CountDownLatch latch = new CountDownLatch(1);
    EndpointAdapter endpoint = new EndpointAdapter() {

        @Override
        public void onMessage(String message) {
            latch.countDown();
        }
    };
    try (Session session = client.connectToServer(endpoint, config, uri)) {
        RemoteEndpoint.Async remote = session.getAsyncRemote();
        Future<Void> future = remote.sendText("batch_mode_auto");
        // The write is immediate, as per the specification.
        future.get(1, TimeUnit.SECONDS);
        // Wait for the echo.
        Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    }
}
Also used : RemoteEndpoint(javax.websocket.RemoteEndpoint) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) Session(javax.websocket.Session) Test(org.junit.Test)

Example 8 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project jetty.project by eclipse.

the class ConfiguratorTest method testEndpointHandshakeInfo.

@Test
public void testEndpointHandshakeInfo() throws Exception {
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    EndpointEchoClient echoer = new EndpointEchoClient();
    // Build Config
    ClientEndpointConfig.Builder cfgbldr = ClientEndpointConfig.Builder.create();
    TrackingConfigurator configurator = new TrackingConfigurator();
    cfgbldr.configurator(configurator);
    ClientEndpointConfig config = cfgbldr.build();
    // Connect
    Session session = container.connectToServer(echoer, config, serverUri);
    // Send Simple Message
    session.getBasicRemote().sendText("Echo");
    // Wait for echo
    echoer.textCapture.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
    // Validate client side configurator use
    Assert.assertThat("configurator.request", configurator.request, notNullValue());
    Assert.assertThat("configurator.response", configurator.response, notNullValue());
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 9 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project jetty.project by eclipse.

the class CookiesTest method testCookiesAreSentToClient.

@Test
public void testCookiesAreSentToClient() throws Exception {
    final String cookieName = "name";
    final String cookieValue = "value";
    final String cookieDomain = "domain";
    final String cookiePath = "/path";
    startServer(new EchoHandler() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
            String cookieString = cookieName + "=" + cookieValue + ";Domain=" + cookieDomain + ";Path=" + cookiePath;
            response.getHeaders().put("Set-Cookie", Collections.singletonList(cookieString));
            return super.createWebSocket(request, response);
        }
    });
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    builder.configurator(new ClientEndpointConfig.Configurator() {

        @Override
        public void afterResponse(HandshakeResponse response) {
            Map<String, List<String>> headers = response.getHeaders();
            // Test case insensitivity
            Assert.assertTrue(headers.containsKey("set-cookie"));
            List<String> values = headers.get("Set-Cookie");
            Assert.assertNotNull(values);
            Assert.assertEquals(1, values.size());
            List<HttpCookie> cookies = HttpCookie.parse(values.get(0));
            Assert.assertEquals(1, cookies.size());
            HttpCookie cookie = cookies.get(0);
            Assert.assertEquals(cookieName, cookie.getName());
            Assert.assertEquals(cookieValue, cookie.getValue());
            Assert.assertEquals(cookieDomain, cookie.getDomain());
            Assert.assertEquals(cookiePath, cookie.getPath());
        }
    });
    ClientEndpointConfig config = builder.build();
    Endpoint endPoint = new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    };
    Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) HandshakeResponse(javax.websocket.HandshakeResponse) Endpoint(javax.websocket.Endpoint) List(java.util.List) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Map(java.util.Map) HttpCookie(java.net.HttpCookie) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Example 10 with ClientEndpointConfig

use of javax.websocket.ClientEndpointConfig in project jetty.project by eclipse.

the class CookiesTest method testCookiesAreSentToServer.

@Test
public void testCookiesAreSentToServer() throws Exception {
    final String cookieName = "name";
    final String cookieValue = "value";
    final String cookieString = cookieName + "=" + cookieValue;
    startServer(new EchoHandler() {

        @Override
        public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
            List<HttpCookie> cookies = request.getCookies();
            assertThat("Cookies", cookies, notNullValue());
            assertThat("Cookies", cookies.size(), is(1));
            HttpCookie cookie = cookies.get(0);
            Assert.assertEquals(cookieName, cookie.getName());
            Assert.assertEquals(cookieValue, cookie.getValue());
            Map<String, List<String>> headers = request.getHeaders();
            // Test case insensitivity
            Assert.assertTrue(headers.containsKey("cookie"));
            List<String> values = headers.get("Cookie");
            Assert.assertNotNull(values);
            Assert.assertEquals(1, values.size());
            Assert.assertEquals(cookieString, values.get(0));
            return super.createWebSocket(request, response);
        }
    });
    WebSocketContainer container = ContainerProvider.getWebSocketContainer();
    ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
    builder.configurator(new ClientEndpointConfig.Configurator() {

        @Override
        public void beforeRequest(Map<String, List<String>> headers) {
            headers.put("Cookie", Collections.singletonList(cookieString));
        }
    });
    ClientEndpointConfig config = builder.build();
    Endpoint endPoint = new Endpoint() {

        @Override
        public void onOpen(Session session, EndpointConfig config) {
        }
    };
    Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
    session.close();
}
Also used : WebSocketContainer(javax.websocket.WebSocketContainer) ServletUpgradeResponse(org.eclipse.jetty.websocket.servlet.ServletUpgradeResponse) Endpoint(javax.websocket.Endpoint) List(java.util.List) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) HttpCookie(java.net.HttpCookie) Map(java.util.Map) ServletUpgradeRequest(org.eclipse.jetty.websocket.servlet.ServletUpgradeRequest) EndpointConfig(javax.websocket.EndpointConfig) ClientEndpointConfig(javax.websocket.ClientEndpointConfig) Session(javax.websocket.Session) Test(org.junit.Test)

Aggregations

ClientEndpointConfig (javax.websocket.ClientEndpointConfig)44 Test (org.junit.Test)26 URI (java.net.URI)25 Session (javax.websocket.Session)19 Endpoint (javax.websocket.Endpoint)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 WebSocketContainer (javax.websocket.WebSocketContainer)12 ClientEndpoint (javax.websocket.ClientEndpoint)8 Context (org.apache.catalina.Context)7 DefaultServlet (org.apache.catalina.servlets.DefaultServlet)7 Tomcat (org.apache.catalina.startup.Tomcat)7 ArrayList (java.util.ArrayList)6 EndpointConfig (javax.websocket.EndpointConfig)6 Extension (javax.websocket.Extension)6 BasicText (org.apache.tomcat.websocket.TesterMessageCountClient.BasicText)6 AnnotatedClientEndpointMetadata (org.eclipse.jetty.websocket.jsr356.client.AnnotatedClientEndpointMetadata)5 List (java.util.List)4 ServerEndpoint (javax.websocket.server.ServerEndpoint)4 AnnotatedEndpointScanner (org.eclipse.jetty.websocket.jsr356.annotations.AnnotatedEndpointScanner)4 EmptyClientEndpointConfig (org.eclipse.jetty.websocket.jsr356.client.EmptyClientEndpointConfig)4