Search in sources :

Example 31 with IBlockheadServerConnection

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

the class WebSocketClientTest method testBasicEcho_FromClient.

@Test
public void testBasicEcho_FromClient() throws Exception {
    JettyTrackingSocket cliSock = new JettyTrackingSocket();
    client.getPolicy().setIdleTimeout(10000);
    URI wsUri = server.getWsUri();
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols("echo");
    Future<Session> future = client.connect(cliSock, wsUri, request);
    final IBlockheadServerConnection srvSock = server.accept();
    srvSock.upgrade();
    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());
    cliSock.assertWasOpened();
    cliSock.assertNotClosed();
    Collection<WebSocketSession> sessions = client.getOpenSessions();
    Assert.assertThat("client.connectionManager.sessions.size", sessions.size(), is(1));
    RemoteEndpoint remote = cliSock.getSession().getRemote();
    remote.sendStringByFuture("Hello World!");
    if (remote.getBatchMode() == BatchMode.ON)
        remote.flush();
    srvSock.echoMessage(1, 30, TimeUnit.SECONDS);
    // wait for response from server
    cliSock.waitForMessage(30, TimeUnit.SECONDS);
    cliSock.assertMessage("Hello World!");
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) RemoteEndpoint(org.eclipse.jetty.websocket.api.RemoteEndpoint) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) Test(org.junit.Test)

Example 32 with IBlockheadServerConnection

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

the class WebSocketClientTest method testLocalRemoteAddress.

@Test
public void testLocalRemoteAddress() throws Exception {
    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));
    InetSocketAddress local = wsocket.getSession().getLocalAddress();
    InetSocketAddress remote = wsocket.getSession().getRemoteAddress();
    Assert.assertThat("Local Socket Address", local, notNullValue());
    Assert.assertThat("Remote Socket Address", remote, notNullValue());
    // Hard to validate (in a portable unit test) the local address that was used/bound in the low level Jetty Endpoint
    Assert.assertThat("Local Socket Address / Host", local.getAddress().getHostAddress(), notNullValue());
    Assert.assertThat("Local Socket Address / Port", local.getPort(), greaterThan(0));
    Assert.assertThat("Remote Socket Address / Host", remote.getAddress().getHostAddress(), is(wsUri.getHost()));
    Assert.assertThat("Remote Socket Address / Port", remote.getPort(), greaterThan(0));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) 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 33 with IBlockheadServerConnection

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

the class WebSocketClientTest method testBasicEcho_UsingCallback.

@Test
public void testBasicEcho_UsingCallback() throws Exception {
    client.setMaxIdleTimeout(160000);
    JettyTrackingSocket cliSock = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    ClientUpgradeRequest request = new ClientUpgradeRequest();
    request.setSubProtocols("echo");
    Future<Session> future = client.connect(cliSock, wsUri, request);
    final IBlockheadServerConnection srvSock = server.accept();
    srvSock.upgrade();
    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());
    cliSock.assertWasOpened();
    cliSock.assertNotClosed();
    Collection<WebSocketSession> sessions = client.getBeans(WebSocketSession.class);
    Assert.assertThat("client.connectionManager.sessions.size", sessions.size(), is(1));
    FutureWriteCallback callback = new FutureWriteCallback();
    cliSock.getSession().getRemote().sendString("Hello World!", callback);
    callback.get(1, TimeUnit.SECONDS);
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) FutureWriteCallback(org.eclipse.jetty.websocket.common.io.FutureWriteCallback) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) WebSocketSession(org.eclipse.jetty.websocket.common.WebSocketSession) 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