Search in sources :

Example 16 with Session

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

the class ClientConnectTest method testConnectionRefused.

@Test
public void testConnectionRefused() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    // Intentionally bad port with nothing listening on it
    URI wsUri = new URI("ws://127.0.0.1:1");
    try {
        Future<Session> future = client.connect(wsocket, wsUri);
        // The attempt to get upgrade response future should throw error
        future.get(3, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> ConnectException");
    } catch (ConnectException e) {
        Throwable t = wsocket.errorQueue.remove();
        Assert.assertThat("Error Queue[0]", t, instanceOf(ConnectException.class));
        wsocket.assertNotOpened();
    } catch (ExecutionException e) {
        if (OS.IS_WINDOWS) {
            // On windows, this is a SocketTimeoutException
            assertExpectedError(e, wsocket, SocketTimeoutException.class);
        } else {
            // Expected path - java.net.ConnectException
            assertExpectedError(e, wsocket, ConnectException.class);
        }
    }
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 17 with Session

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

the class ClientConnectTest method testBadUpgrade.

@Test
public void testBadUpgrade() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection connection = server.accept();
    connection.readRequest();
    // Upgrade badly
    connection.respond("HTTP/1.1 101 Upgrade\r\n" + "Sec-WebSocket-Accept: rubbish\r\n" + "\r\n");
    // The attempt to get upgrade response future should throw error
    try {
        future.get(30, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> UpgradeException");
    } catch (ExecutionException e) {
        // Expected Path
        UpgradeException ue = assertExpectedError(e, wsocket, UpgradeException.class);
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI(), notNullValue());
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI().toASCIIString(), is(wsUri.toASCIIString()));
        Assert.assertThat("UpgradeException.responseStatusCode", ue.getResponseStatusCode(), is(101));
    }
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 18 with Session

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

the class ClientConnectTest method testBadHandshake_GetOK.

@Test
public void testBadHandshake_GetOK() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection connection = server.accept();
    connection.readRequest();
    // Send OK to GET but not upgrade
    connection.respond("HTTP/1.1 200 OK\r\n" + "Content-Length: 0\r\n" + "\r\n");
    // The attempt to get upgrade response future should throw error
    try {
        future.get(30, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> UpgradeException");
    } catch (ExecutionException e) {
        // Expected Path
        UpgradeException ue = assertExpectedError(e, wsocket, UpgradeException.class);
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI(), notNullValue());
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI().toASCIIString(), is(wsUri.toASCIIString()));
        Assert.assertThat("UpgradeException.responseStatusCode", ue.getResponseStatusCode(), is(200));
    }
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 19 with Session

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

the class ClientConnectTest method testBadHandshake_SwitchingProtocols_InvalidConnectionHeader.

@Test
public void testBadHandshake_SwitchingProtocols_InvalidConnectionHeader() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection connection = server.accept();
    List<String> requestLines = connection.readRequestLines();
    String key = connection.parseWebSocketKey(requestLines);
    // Send Switching Protocols 101, but invalid 'Connection' header
    StringBuilder resp = new StringBuilder();
    resp.append("HTTP/1.1 101 Switching Protocols\r\n");
    resp.append("Sec-WebSocket-Accept: ").append(AcceptHash.hashKey(key)).append("\r\n");
    resp.append("Connection: close\r\n");
    resp.append("\r\n");
    connection.respond(resp.toString());
    // The attempt to get upgrade response future should throw error
    try {
        future.get(30, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> UpgradeException");
    } catch (ExecutionException e) {
        // Expected Path
        UpgradeException ue = assertExpectedError(e, wsocket, UpgradeException.class);
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI(), notNullValue());
        Assert.assertThat("UpgradeException.requestURI", ue.getRequestURI().toASCIIString(), is(wsUri.toASCIIString()));
        Assert.assertThat("UpgradeException.responseStatusCode", ue.getResponseStatusCode(), is(101));
    }
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 20 with Session

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

the class CookieTest method testViaCookieManager.

@Test
public void testViaCookieManager() throws Exception {
    // Setup client
    CookieManager cookieMgr = new CookieManager();
    client.setCookieStore(cookieMgr.getCookieStore());
    HttpCookie cookie = new HttpCookie("hello", "world");
    cookie.setPath("/");
    cookie.setVersion(0);
    cookie.setMaxAge(100000);
    cookieMgr.getCookieStore().add(server.getWsUri(), cookie);
    cookie = new HttpCookie("foo", "bar is the word");
    cookie.setPath("/");
    cookie.setMaxAge(100000);
    cookieMgr.getCookieStore().add(server.getWsUri(), cookie);
    // Client connects
    CookieTrackingSocket clientSocket = new CookieTrackingSocket();
    Future<Session> clientConnectFuture = client.connect(clientSocket, server.getWsUri());
    // Server accepts connect
    IBlockheadServerConnection serverConn = server.accept();
    // client confirms upgrade and receipt of frame
    String serverCookies = confirmClientUpgradeAndCookies(clientSocket, clientConnectFuture, serverConn);
    assertThat("Cookies seen at server side", serverCookies, containsString("hello=world"));
    assertThat("Cookies seen at server side", serverCookies, containsString("foo=bar is the word"));
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpCookie(java.net.HttpCookie) CookieManager(java.net.CookieManager) Session(org.eclipse.jetty.websocket.api.Session) 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