Search in sources :

Example 21 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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 22 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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 23 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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 24 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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)

Example 25 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection in project jetty.project by eclipse.

the class ClientConnectTest method testUpgradeWithAuthorizationHeader.

@Test
public void testUpgradeWithAuthorizationHeader() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest();
    // actual value for this test is irrelevant, its important that this
    // header actually be sent with a value (the value specified)
    upgradeRequest.setHeader("Authorization", "Bogus SHA1");
    Future<Session> future = client.connect(wsocket, wsUri, upgradeRequest);
    IBlockheadServerConnection connection = server.accept();
    List<String> requestLines = connection.upgrade();
    Session sess = future.get(30, TimeUnit.SECONDS);
    sess.close();
    String authLine = requestLines.stream().filter((line) -> line.startsWith("Authorization:")).findFirst().get();
    assertThat("Request Container Authorization", authLine, is("Authorization: Bogus SHA1"));
    assertThat("Connect.UpgradeRequest", wsocket.connectUpgradeRequest, notNullValue());
    assertThat("Connect.UpgradeResponse", wsocket.connectUpgradeResponse, notNullValue());
}
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)

Aggregations

IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)33 Test (org.junit.Test)33 Session (org.eclipse.jetty.websocket.api.Session)32 URI (java.net.URI)23 WebSocketSession (org.eclipse.jetty.websocket.common.WebSocketSession)15 ExecutionException (java.util.concurrent.ExecutionException)7 EndPoint (org.eclipse.jetty.io.EndPoint)7 SocketChannelEndPoint (org.eclipse.jetty.io.SocketChannelEndPoint)7 UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)6 Matchers.containsString (org.hamcrest.Matchers.containsString)6 Slow (org.eclipse.jetty.toolchain.test.annotation.Slow)3 RemoteEndpoint (org.eclipse.jetty.websocket.api.RemoteEndpoint)3 HttpCookie (java.net.HttpCookie)2 SocketTimeoutException (java.net.SocketTimeoutException)2 ByteBuffer (java.nio.ByteBuffer)2 ZeroMasker (org.eclipse.jetty.websocket.client.masks.ZeroMasker)2 TextFrame (org.eclipse.jetty.websocket.common.frames.TextFrame)2 Ignore (org.junit.Ignore)2 ConnectException (java.net.ConnectException)1 CookieManager (java.net.CookieManager)1