Search in sources :

Example 46 with Session

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

the class ClientConnectTest method testConnectionNotAccepted.

@Test
public void testConnectionNotAccepted() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    try {
        future.get(3, TimeUnit.SECONDS);
        Assert.fail("Should have Timed Out");
    } catch (ExecutionException e) {
        assertExpectedError(e, wsocket, UpgradeException.class);
        // Possible Passing Path (active session wait timeout)
        wsocket.assertNotOpened();
    } catch (TimeoutException e) {
        // Possible Passing Path (concurrency timeout)
        wsocket.assertNotOpened();
    }
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) ExecutionException(java.util.concurrent.ExecutionException) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException) Test(org.junit.Test)

Example 47 with Session

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

the class ClientConnectTest method testBadHandshake_GetOK_WithSecWebSocketAccept.

@Test
public void testBadHandshake_GetOK_WithSecWebSocketAccept() 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 OK to GET but not upgrade
    StringBuilder resp = new StringBuilder();
    // intentionally 200 (not 101)
    resp.append("HTTP/1.1 200 OK\r\n");
    // Include a value accept key
    resp.append("Sec-WebSocket-Accept: ").append(AcceptHash.hashKey(key)).append("\r\n");
    resp.append("Content-Length: 0\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(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 48 with Session

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

the class ClientConnectTest method testAltConnect.

@Test
public void testAltConnect() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    HttpClient httpClient = new HttpClient();
    httpClient.start();
    WebSocketUpgradeRequest req = new WebSocketUpgradeRequest(new WebSocketClient(), httpClient, wsUri, wsocket);
    req.header("X-Foo", "Req");
    CompletableFuture<Session> sess = req.sendAsync();
    sess.thenAccept((s) -> {
        System.out.printf("Session: %s%n", s);
        s.close();
        assertThat("Connect.UpgradeRequest", wsocket.connectUpgradeRequest, notNullValue());
        assertThat("Connect.UpgradeResponse", wsocket.connectUpgradeResponse, notNullValue());
    });
    IBlockheadServerConnection connection = server.accept();
    connection.upgrade();
}
Also used : HttpClient(org.eclipse.jetty.client.HttpClient) IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) URI(java.net.URI) Session(org.eclipse.jetty.websocket.api.Session) Test(org.junit.Test)

Example 49 with Session

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

the class ClientConnectTest method testConnectionTimeout_Concurrent.

@Test(expected = TimeoutException.class)
public void testConnectionTimeout_Concurrent() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection ssocket = server.accept();
    Assert.assertNotNull(ssocket);
    // The attempt to get upgrade response future should throw error
    try {
        future.get(3, TimeUnit.SECONDS);
        Assert.fail("Expected ExecutionException -> TimeoutException");
    } catch (ExecutionException e) {
        // Expected path - java.net.ConnectException ?
        assertExpectedError(e, wsocket, ConnectException.class);
    }
}
Also used : IBlockheadServerConnection(org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection) 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 50 with Session

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

the class ClientConnectTest method testBadHandshake_SwitchingProtocols_NoConnectionHeader.

@Test
public void testBadHandshake_SwitchingProtocols_NoConnectionHeader() 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 no '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");
    // Intentionally leave out Connection header
    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)

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