Search in sources :

Example 21 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class AltFilterTest method testEcho.

@Test
public void testEcho() throws Exception {
    WSServer wsb = new WSServer(testdir, "app");
    wsb.copyWebInf("alt-filter-web.xml");
    // the endpoint (extends javax.websocket.Endpoint)
    wsb.copyClass(BasicEchoSocket.class);
    try {
        wsb.start();
        URI uri = wsb.getServerBaseURI();
        WebAppContext webapp = wsb.createWebAppContext();
        wsb.deployWebapp(webapp);
        FilterHolder filterWebXml = webapp.getServletHandler().getFilter("wsuf-test");
        assertThat("Filter[wsuf-test]", filterWebXml, notNullValue());
        FilterHolder filterSCI = webapp.getServletHandler().getFilter("Jetty_WebSocketUpgradeFilter");
        assertThat("Filter[Jetty_WebSocketUpgradeFilter]", filterSCI, nullValue());
        WebSocketClient client = new WebSocketClient(bufferPool);
        try {
            client.start();
            JettyEchoSocket clientEcho = new JettyEchoSocket();
            Future<Session> future = client.connect(clientEcho, uri.resolve("echo"));
            // wait for connect
            future.get(1, TimeUnit.SECONDS);
            clientEcho.sendMessage("Hello Echo");
            Queue<String> msgs = clientEcho.awaitMessages(1);
            Assert.assertEquals("Expected message", "Hello Echo", msgs.poll());
        } finally {
            client.stop();
        }
    } finally {
        wsb.stop();
    }
}
Also used : WebAppContext(org.eclipse.jetty.webapp.WebAppContext) FilterHolder(org.eclipse.jetty.servlet.FilterHolder) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 22 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class AnnotatedServerEndpointTest method assertResponse.

private void assertResponse(String message, String... expectedTexts) throws Exception {
    WebSocketClient client = new WebSocketClient(bufferPool);
    try {
        client.start();
        JettyEchoSocket clientEcho = new JettyEchoSocket();
        URI uri = server.getServerBaseURI().resolve("echo");
        ClientUpgradeRequest req = new ClientUpgradeRequest();
        req.setSubProtocols("echo");
        Future<Session> foo = client.connect(clientEcho, uri, req);
        // wait for connect
        foo.get(1, TimeUnit.SECONDS);
        clientEcho.sendMessage(message);
        Queue<String> msgs = clientEcho.awaitMessages(1);
        String response = msgs.poll();
        for (String expected : expectedTexts) {
            Assert.assertThat("Expected message", response, containsString(expected));
        }
    } finally {
        client.stop();
    }
}
Also used : ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session)

Example 23 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class WebSocketOverSSLTest method testEcho.

/**
     * Test the requirement of issuing socket and receiving echo response
     * @throws Exception on test failure
     */
@Test
public void testEcho() throws Exception {
    Assert.assertThat("server scheme", server.getServerUri().getScheme(), is("wss"));
    WebSocketClient client = new WebSocketClient(server.getSslContextFactory(), null, bufferPool);
    try {
        client.start();
        CaptureSocket clientSocket = new CaptureSocket();
        URI requestUri = server.getServerUri();
        System.err.printf("Request URI: %s%n", requestUri.toASCIIString());
        Future<Session> fut = client.connect(clientSocket, requestUri);
        // wait for connect
        Session session = fut.get(FUTURE_TIMEOUT_SEC, TimeUnit.SECONDS);
        // Generate text frame
        String msg = "this is an echo ... cho ... ho ... o";
        RemoteEndpoint remote = session.getRemote();
        remote.sendString(msg);
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
        // Read frame (hopefully text frame)
        clientSocket.messages.awaitEventCount(1, 30, TimeUnit.SECONDS);
        EventQueue<String> captured = clientSocket.messages;
        Assert.assertThat("Text Message", captured.poll(), is(msg));
        // Shutdown the socket
        clientSocket.close();
    } finally {
        client.stop();
    }
}
Also used : CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 24 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class WebSocketOverSSLTest method testServerSessionIsSecure.

/**
     * Test that server session reports as secure
     * @throws Exception on test failure
     */
@Test
public void testServerSessionIsSecure() throws Exception {
    Assert.assertThat("server scheme", server.getServerUri().getScheme(), is("wss"));
    WebSocketClient client = new WebSocketClient(server.getSslContextFactory(), null, bufferPool);
    try {
        client.setConnectTimeout(CONNECT_TIMEOUT);
        client.start();
        CaptureSocket clientSocket = new CaptureSocket();
        URI requestUri = server.getServerUri();
        System.err.printf("Request URI: %s%n", requestUri.toASCIIString());
        Future<Session> fut = client.connect(clientSocket, requestUri);
        // wait for connect
        Session session = fut.get(FUTURE_TIMEOUT_SEC, TimeUnit.SECONDS);
        // Generate text frame
        RemoteEndpoint remote = session.getRemote();
        remote.sendString("session.isSecure");
        if (remote.getBatchMode() == BatchMode.ON)
            remote.flush();
        // Read frame (hopefully text frame)
        clientSocket.messages.awaitEventCount(1, 30, TimeUnit.SECONDS);
        EventQueue<String> captured = clientSocket.messages;
        Assert.assertThat("Server.session.isSecure", captured.poll(), is("session.isSecure=true"));
        // Shutdown the socket
        clientSocket.close();
    } finally {
        client.stop();
    }
}
Also used : CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 25 with Session

use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.

the class PerMessageDeflateExtensionTest method testPerMessageDeflateDefault.

/**
     * Default configuration for permessage-deflate
     * @throws Exception on test failure
     */
@Test
public void testPerMessageDeflateDefault() throws Exception {
    Assume.assumeTrue("Server has permessage-deflate registered", server.getWebSocketServletFactory().getExtensionFactory().isAvailable("permessage-deflate"));
    Assert.assertThat("server scheme", server.getServerUri().getScheme(), is(scheme));
    int binBufferSize = (int) (msgSize * 1.5);
    WebSocketPolicy serverPolicy = server.getWebSocketServletFactory().getPolicy();
    // Ensure binBufferSize is sane (not smaller then other buffers)
    binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageSize());
    binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageBufferSize());
    binBufferSize = Math.max(binBufferSize, this.inputBufferSize);
    serverPolicy.setMaxBinaryMessageSize(binBufferSize);
    serverPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
    WebSocketClient client = new WebSocketClient(server.getSslContextFactory());
    WebSocketPolicy clientPolicy = client.getPolicy();
    clientPolicy.setMaxBinaryMessageSize(binBufferSize);
    clientPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
    if (inputBufferSize > 0) {
        clientPolicy.setInputBufferSize(inputBufferSize);
    }
    try {
        client.start();
        // Make sure the read times out if there are problems with the implementation
        client.setMaxIdleTimeout(TimeUnit.SECONDS.toMillis(15));
        CaptureSocket clientSocket = new CaptureSocket();
        ClientUpgradeRequest request = new ClientUpgradeRequest();
        request.addExtensions("permessage-deflate");
        request.setSubProtocols("echo");
        Future<Session> fut = client.connect(clientSocket, server.getServerUri(), request);
        // Wait for connect
        Session session = fut.get(30, TimeUnit.SECONDS);
        assertThat("Response.extensions", getNegotiatedExtensionList(session), containsString("permessage-deflate"));
        // Create message
        byte[] msg = new byte[msgSize];
        Random rand = new Random();
        rand.setSeed(8080);
        rand.nextBytes(msg);
        // Calculate sha1
        String sha1 = Sha1Sum.calculate(msg);
        // Client sends first message
        session.getRemote().sendBytes(ByteBuffer.wrap(msg));
        clientSocket.messages.awaitEventCount(1, 5, TimeUnit.SECONDS);
        String echoMsg = clientSocket.messages.poll();
        Assert.assertThat("Echo'd Message", echoMsg, is("binary[sha1=" + sha1 + "]"));
    } finally {
        client.stop();
    }
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) Random(java.util.Random) CaptureSocket(org.eclipse.jetty.websocket.server.helper.CaptureSocket) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Aggregations

Session (org.eclipse.jetty.websocket.api.Session)74 Test (org.junit.Test)57 URI (java.net.URI)46 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)32 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)21 ExecutionException (java.util.concurrent.ExecutionException)12 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)9 HashMap (java.util.HashMap)8 Matchers.containsString (org.hamcrest.Matchers.containsString)8 Envelope (com.kixeye.chassis.transport.dto.Envelope)7 MessageSerDe (com.kixeye.chassis.transport.serde.MessageSerDe)7 ProtobufMessageSerDe (com.kixeye.chassis.transport.serde.converter.ProtobufMessageSerDe)7 QueuingWebSocketListener (com.kixeye.chassis.transport.websocket.QueuingWebSocketListener)7 WebSocketMessageRegistry (com.kixeye.chassis.transport.websocket.WebSocketMessageRegistry)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)7 MapPropertySource (org.springframework.core.env.MapPropertySource)7