Search in sources :

Example 51 with Session

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

the class CookieTest method testViaServletUpgradeRequest.

@Test
public void testViaServletUpgradeRequest() throws Exception {
    // Setup client
    HttpCookie cookie = new HttpCookie("hello", "world");
    cookie.setPath("/");
    cookie.setMaxAge(100000);
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setCookies(Collections.singletonList(cookie));
    // Client connects
    CookieTrackingSocket clientSocket = new CookieTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri(), request);
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    // client confirms upgrade and receipt of frame
    String serverCookies = confirmClientUpgradeAndCookies(clientSocket, clientConnectFuture, serverConn);
    Assert.assertThat("Cookies seen at server side", serverCookies, containsString("hello=world"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpCookie(java.net.HttpCookie) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 52 with Session

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

the class BadNetworkTest method testAbruptServerClose.

@Test
public void testAbruptServerClose() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    // Validate that we are connected
    future.get(30, TimeUnit.SECONDS);
    wsocket.waitForConnected(30, TimeUnit.SECONDS);
    // Have server disconnect abruptly
    ssocket.disconnect();
    // Wait for close (as response to idle timeout)
    wsocket.waitForClose(10, TimeUnit.SECONDS);
    // Client Socket should see a close event, with status NO_CLOSE
    // This event is automatically supplied by the underlying WebSocketClientConnection
    // in the situation of a bad network connection.
    wsocket.assertCloseCode(StatusCode.NO_CLOSE);
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 53 with Session

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

the class BadNetworkTest method testAbruptClientClose.

@Test
public void testAbruptClientClose() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    // Validate that we are connected
    future.get(30, TimeUnit.SECONDS);
    wsocket.waitForConnected(30, TimeUnit.SECONDS);
    // Have client disconnect abruptly
    Session session = wsocket.getSession();
    session.disconnect();
    // Client Socket should see close
    wsocket.waitForClose(10, TimeUnit.SECONDS);
    // Client Socket should see a close event, with status NO_CLOSE
    // This event is automatically supplied by the underlying WebSocketClientConnection
    // in the situation of a bad network connection.
    wsocket.assertCloseCode(StatusCode.NO_CLOSE);
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 54 with Session

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

the class ClientCloseTest method testHalfClose.

@Test
public void testHalfClose() throws Exception {
    // Set client timeout
    final int timeout = 1000;
    client.setMaxIdleTimeout(timeout);
    // Client connects
    CloseTrackingSocket clientSocket = new CloseTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    serverConn.upgrade();
    // client confirms connection via echo
    confirmConnection(clientSocket, clientConnectFuture, serverConn);
    // client sends close frame (code 1000, normal)
    final String origCloseReason = "Normal Close";
    clientSocket.getSession().close(StatusCode.NORMAL, origCloseReason);
    // server receives close frame
    confirmServerReceivedCloseFrame(serverConn, StatusCode.NORMAL, is(origCloseReason));
    // server sends 2 messages
    serverConn.write(new TextFrame().setPayload("Hello"));
    serverConn.write(new TextFrame().setPayload("World"));
    // server sends close frame (code 1000, no reason)
    CloseInfo sclose = new CloseInfo(StatusCode.NORMAL, "From Server");
    serverConn.write(sclose.asFrame());
    // client receives 2 messages
    clientSocket.messageQueue.awaitEventCount(2, 1, TimeUnit.SECONDS);
    // Verify received messages
    String recvMsg = clientSocket.messageQueue.poll();
    assertThat("Received message 1", recvMsg, is("Hello"));
    recvMsg = clientSocket.messageQueue.poll();
    assertThat("Received message 2", recvMsg, is("World"));
    // Verify that there are no errors
    assertThat("Error events", clientSocket.error.get(), nullValue());
    // client close event on ws-endpoint
    clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.NORMAL), containsString("From Server"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Matchers.containsString(org.hamcrest.Matchers.containsString) CloseInfo(org.eclipse.jetty.websocket.common.CloseInfo) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 55 with Session

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

the class ClientCloseTest method testStopLifecycle.

@Test(timeout = 5000L)
public void testStopLifecycle() throws Exception {
    // Set client timeout
    final int timeout = 1000;
    client.setMaxIdleTimeout(timeout);
    int clientCount = 3;
    CloseTrackingSocket[] clientSockets = new CloseTrackingSocket[clientCount];
    IBlockheadServerConnection[] serverConns = new IBlockheadServerConnection[clientCount];
    // Connect Multiple Clients
    for (int i = 0; i < clientCount; i++) {
        // Client Request Upgrade
        clientSockets[i] = new CloseTrackingSocket();
        Future<Session> clientConnectFuture = client.connect(clientSockets[i], server.getWsUri());
        // Server accepts connection
        serverConns[i] = server.accept();
        serverConns[i].upgrade();
        // client confirms connection via echo
        confirmConnection(clientSockets[i], clientConnectFuture, serverConns[i]);
    }
    // client lifecycle stop
    client.stop();
    // clients send close frames (code 1001, shutdown)
    for (int i = 0; i < clientCount; i++) {
        // server receives close frame
        confirmServerReceivedCloseFrame(serverConns[i], StatusCode.SHUTDOWN, containsString("Shutdown"));
    }
    // clients disconnect
    for (int i = 0; i < clientCount; i++) {
        clientSockets[i].assertReceivedCloseEvent(timeout, is(StatusCode.SHUTDOWN), containsString("Shutdown"));
    }
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) 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