Search in sources :

Example 56 with Session

use of org.eclipse.jetty.websocket.api.Session 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)

Example 57 with Session

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

the class SlowServerTest method testServerSlowToRead.

@Test
@Slow
public void testServerSlowToRead() throws Exception {
    JettyTrackingSocket tsocket = new JettyTrackingSocket();
    client.setMasker(new ZeroMasker());
    client.setMaxIdleTimeout(60000);
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(tsocket, wsUri);
    IBlockheadServerConnection sconnection = server.accept();
    sconnection.setSoTimeout(60000);
    sconnection.upgrade();
    // Confirm connected
    future.get(30, TimeUnit.SECONDS);
    tsocket.waitForConnected(30, TimeUnit.SECONDS);
    int messageCount = 10;
    // Setup slow server read thread
    ServerReadThread reader = new ServerReadThread(sconnection, messageCount);
    // slow it down
    reader.setSlowness(100);
    reader.start();
    // Have client write as quickly as it can.
    ClientWriteThread writer = new ClientWriteThread(tsocket.getSession());
    writer.setMessageCount(messageCount);
    writer.setMessage("Hello");
    // disable slowness
    writer.setSlowness(-1);
    writer.start();
    writer.join();
    // Verify receive
    reader.waitForExpectedMessageCount(10, TimeUnit.SECONDS);
    Assert.assertThat("Frame Receive Count", reader.getFrameCount(), is(messageCount));
    // Close
    tsocket.getSession().close(StatusCode.NORMAL, "Done");
    Assert.assertTrue("Client Socket Closed", tsocket.closeLatch.await(10, TimeUnit.SECONDS));
    tsocket.assertCloseCode(StatusCode.NORMAL);
    // stop reading
    reader.cancel();
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ZeroMasker(org.eclipse.jetty.websocket.client.masks.ZeroMasker) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 58 with Session

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

the class SlowServerTest method testServerSlowToSend.

@Test
@Slow
public void testServerSlowToSend() throws Exception {
    JettyTrackingSocket clientSocket = new JettyTrackingSocket();
    client.setMasker(new ZeroMasker());
    client.setMaxIdleTimeout(60000);
    URI wsUri = server.getWsUri();
    Future<Session> clientConnectFuture = client.connect(clientSocket, wsUri);
    IBlockheadServerConnection serverConn = server.accept();
    serverConn.setSoTimeout(60000);
    serverConn.upgrade();
    // Confirm connected
    clientConnectFuture.get(30, TimeUnit.SECONDS);
    clientSocket.waitForConnected(30, TimeUnit.SECONDS);
    // Have server write slowly.
    int messageCount = 1000;
    ServerWriteThread writer = new ServerWriteThread(serverConn);
    writer.setMessageCount(messageCount);
    writer.setMessage("Hello");
    writer.setSlowness(10);
    writer.start();
    writer.join();
    // Verify receive
    Assert.assertThat("Message Receive Count", clientSocket.messageQueue.size(), is(messageCount));
    // Close
    serverConn.close(StatusCode.NORMAL);
    Assert.assertTrue("Client Socket Closed", clientSocket.closeLatch.await(10, TimeUnit.SECONDS));
    clientSocket.assertCloseCode(StatusCode.NORMAL);
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ZeroMasker(org.eclipse.jetty.websocket.client.masks.ZeroMasker) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test) Slow(org.eclipse.jetty.toolchain.test.annotation.Slow)

Example 59 with Session

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

the class WebSocketClientTest method testMaxMessageSize.

/**
     * Ensure that <code>@WebSocket(maxTextMessageSize = 100*1024)</code> behaves as expected.
     * 
     * @throws Exception
     *             on test failure
     */
@Test
public void testMaxMessageSize() throws Exception {
    MaxMessageSocket wsocket = new MaxMessageSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    ssocket.upgrade();
    wsocket.awaitConnect(1, TimeUnit.SECONDS);
    Session sess = future.get(30, TimeUnit.SECONDS);
    Assert.assertThat("Session", sess, notNullValue());
    Assert.assertThat("Session.open", sess.isOpen(), is(true));
    // Create string that is larger than default size of 64k
    // but smaller than maxMessageSize of 100k
    byte[] buf = new byte[80 * 1024];
    Arrays.fill(buf, (byte) 'x');
    String msg = StringUtil.toUTF8String(buf, 0, buf.length);
    wsocket.getSession().getRemote().sendStringByFuture(msg);
    ssocket.echoMessage(1, 2, TimeUnit.SECONDS);
    // wait for response from server
    wsocket.waitForMessage(1, TimeUnit.SECONDS);
    wsocket.assertMessage(msg);
    Assert.assertTrue(wsocket.dataLatch.await(2, TimeUnit.SECONDS));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 60 with Session

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

the class WebSocketClientTest method testParameterMap.

@Test
public void testParameterMap() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri().resolve("/test?snack=cashews&amount=handful&brand=off");
    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));
    Session session = wsocket.getSession();
    UpgradeRequest req = session.getUpgradeRequest();
    Assert.assertThat("Upgrade Request", req, notNullValue());
    Map<String, List<String>> parameterMap = req.getParameterMap();
    Assert.assertThat("Parameter Map", parameterMap, notNullValue());
    Assert.assertThat("Parameter[snack]", parameterMap.get("snack"), is(Arrays.asList(new String[] { "cashews" })));
    Assert.assertThat("Parameter[amount]", parameterMap.get("amount"), is(Arrays.asList(new String[] { "handful" })));
    Assert.assertThat("Parameter[brand]", parameterMap.get("brand"), is(Arrays.asList(new String[] { "off" })));
    Assert.assertThat("Parameter[cost]", parameterMap.get("cost"), nullValue());
}
Also used : UpgradeRequest(org.eclipse.jetty.websocket.api.UpgradeRequest) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) List(java.util.List) URI(java.net.URI) 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