Search in sources :

Example 1 with IBlockheadServerConnection

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

the class WebSocketClientTest method testMessageBiggerThanBufferSize.

@Test
public void testMessageBiggerThanBufferSize() throws Exception {
    int bufferSize = 512;
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    future.get(30, TimeUnit.SECONDS);
    Assert.assertTrue(wsocket.openLatch.await(1, TimeUnit.SECONDS));
    // 1.5 times buffer size
    int length = bufferSize + (bufferSize / 2);
    // FIN + TEXT
    ssocket.write(0x80 | 0x01);
    // No MASK and 2 bytes length
    ssocket.write(0x7E);
    // first length byte
    ssocket.write(length >> 8);
    // second length byte
    ssocket.write(length & 0xFF);
    for (int i = 0; i < length; ++i) {
        ssocket.write('x');
    }
    ssocket.flush();
    Assert.assertTrue(wsocket.dataLatch.await(1000, TimeUnit.SECONDS));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 2 with IBlockheadServerConnection

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

the class WebSocketClientTest method testBasicEcho_FromServer.

@Test
public void testBasicEcho_FromServer() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    Future<Session> future = client.connect(wsocket, server.getWsUri());
    // Server
    final IBlockheadServerConnection srvSock = server.accept();
    srvSock.upgrade();
    // Validate connect
    Session sess = future.get(30, TimeUnit.SECONDS);
    Assert.assertThat("Session", sess, notNullValue());
    Assert.assertThat("Session.open", sess.isOpen(), is(true));
    Assert.assertThat("Session.upgradeRequest", sess.getUpgradeRequest(), notNullValue());
    Assert.assertThat("Session.upgradeResponse", sess.getUpgradeResponse(), notNullValue());
    // Have server send initial message
    srvSock.write(new TextFrame().setPayload("Hello World"));
    // Verify connect
    future.get(30, TimeUnit.SECONDS);
    wsocket.assertWasOpened();
    wsocket.awaitMessage(1, TimeUnit.SECONDS, 2);
    wsocket.assertMessage("Hello World");
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) TextFrame(org.eclipse.jetty.websocket.common.frames.TextFrame) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 3 with IBlockheadServerConnection

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

the class ClientCloseTest method testReadEOF.

@Test
public void testReadEOF() 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
    final String origCloseReason = "Normal Close";
    clientSocket.getSession().close(StatusCode.NORMAL, origCloseReason);
    // server receives close frame
    confirmServerReceivedCloseFrame(serverConn, StatusCode.NORMAL, is(origCloseReason));
    // client should not have received close message (yet)
    clientSocket.assertNoCloseEvent();
    // server shuts down connection (no frame reply)
    serverConn.disconnect();
    // client reads -1 (EOF)
    // client triggers close event on client ws-endpoint
    clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.ABNORMAL), containsString("EOF"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 4 with IBlockheadServerConnection

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

the class ClientCloseTest method testServerNoCloseHandshake.

@Test
public void testServerNoCloseHandshake() 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
    final String origCloseReason = "Normal Close";
    clientSocket.getSession().close(StatusCode.NORMAL, origCloseReason);
    // server receives close frame
    confirmServerReceivedCloseFrame(serverConn, StatusCode.NORMAL, is(origCloseReason));
    // client should not have received close message (yet)
    clientSocket.assertNoCloseEvent();
    // server never sends close frame handshake
    // server sits idle
    // client idle timeout triggers close event on client ws-endpoint
    assertThat("OnError Latch", clientSocket.errorLatch.await(2, TimeUnit.SECONDS), is(true));
    assertThat("OnError", clientSocket.error.get(), instanceOf(SocketTimeoutException.class));
    assertThat("OnError", clientSocket.error.get().getMessage(), containsString("Timeout on Read"));
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) 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 5 with IBlockheadServerConnection

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

the class ClientCloseTest method testWriteException.

@Test
public void testWriteException() 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);
    // setup client endpoint for write failure (test only)
    EndPoint endp = clientSocket.getEndPoint();
    endp.shutdownOutput();
    // client enqueue close frame
    // client write failure
    final String origCloseReason = "Normal Close";
    clientSocket.getSession().close(StatusCode.NORMAL, origCloseReason);
    assertThat("OnError Latch", clientSocket.errorLatch.await(2, TimeUnit.SECONDS), is(true));
    assertThat("OnError", clientSocket.error.get(), instanceOf(EofException.class));
    // client triggers close event on client ws-endpoint
    // assert - close code==1006 (abnormal)
    // assert - close reason message contains (write failure)
    clientSocket.assertReceivedCloseEvent(timeout, is(StatusCode.ABNORMAL), containsString("EOF"));
}
Also used : EofException(org.eclipse.jetty.io.EofException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) EndPoint(org.eclipse.jetty.io.EndPoint) SocketChannelEndPoint(org.eclipse.jetty.io.SocketChannelEndPoint) Matchers.containsString(org.hamcrest.Matchers.containsString) 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

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