Search in sources :

Example 11 with IBlockheadServerConnection

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

the class ClientConnectTest method testBadHandshake.

@Test
public void testBadHandshake() throws Exception {
    JettyTrackingSocket wsocket = new JettyTrackingSocket();
    URI wsUri = server.getWsUri();
    Future<Session> future = client.connect(wsocket, wsUri);
    IBlockheadServerConnection connection = server.accept();
    connection.readRequest();
    // no upgrade, just fail with a 404 error
    connection.respond("HTTP/1.1 404 NOT FOUND\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(404));
    }
}
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 12 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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 13 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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 14 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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 15 with IBlockheadServerConnection

use of org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection 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

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