Search in sources :

Example 1 with UpgradeException

use of org.eclipse.jetty.websocket.api.UpgradeException 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 2 with UpgradeException

use of org.eclipse.jetty.websocket.api.UpgradeException 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 3 with UpgradeException

use of org.eclipse.jetty.websocket.api.UpgradeException 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 4 with UpgradeException

use of org.eclipse.jetty.websocket.api.UpgradeException 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 5 with UpgradeException

use of org.eclipse.jetty.websocket.api.UpgradeException in project incubator-pulsar by apache.

the class ProxyPublishConsumeTest method emptySubcriptionConsumerTest.

@Test(timeOut = 10000)
public void emptySubcriptionConsumerTest() throws Exception {
    // Empty subcription name
    final String consumerUri = "ws://localhost:" + port + "/ws/consumer/persistent/my-property/use/my-ns/my-topic2/?subscriptionType=Exclusive";
    URI consumeUri = URI.create(consumerUri);
    WebSocketClient consumeClient1 = new WebSocketClient();
    SimpleConsumerSocket consumeSocket1 = new SimpleConsumerSocket();
    try {
        consumeClient1.start();
        ClientUpgradeRequest consumeRequest1 = new ClientUpgradeRequest();
        Future<Session> consumerFuture1 = consumeClient1.connect(consumeSocket1, consumeUri, consumeRequest1);
        consumerFuture1.get();
        Assert.fail("should fail: empty subscription");
    } catch (Exception e) {
        // Expected
        Assert.assertTrue(e.getCause() instanceof UpgradeException);
        Assert.assertEquals(((UpgradeException) e.getCause()).getResponseStatusCode(), HttpServletResponse.SC_BAD_REQUEST);
    } finally {
        stopWebSocketClient(consumeClient1);
    }
}
Also used : UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) ClientUpgradeRequest(org.eclipse.jetty.websocket.client.ClientUpgradeRequest) WebSocketClient(org.eclipse.jetty.websocket.client.WebSocketClient) URI(java.net.URI) UpgradeException(org.eclipse.jetty.websocket.api.UpgradeException) Session(org.eclipse.jetty.websocket.api.Session) Test(org.testng.annotations.Test)

Aggregations

UpgradeException (org.eclipse.jetty.websocket.api.UpgradeException)12 URI (java.net.URI)11 Session (org.eclipse.jetty.websocket.api.Session)10 ExecutionException (java.util.concurrent.ExecutionException)7 IBlockheadServerConnection (org.eclipse.jetty.websocket.common.test.IBlockheadServerConnection)6 Test (org.junit.Test)6 ClientUpgradeRequest (org.eclipse.jetty.websocket.client.ClientUpgradeRequest)5 WebSocketClient (org.eclipse.jetty.websocket.client.WebSocketClient)4 Test (org.testng.annotations.Test)4 EOFException (java.io.EOFException)1 IOException (java.io.IOException)1 ProtocolException (java.net.ProtocolException)1 SocketException (java.net.SocketException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Future (java.util.concurrent.Future)1 TimeoutException (java.util.concurrent.TimeoutException)1 BacklogQuota (org.apache.pulsar.common.policies.data.BacklogQuota)1 HttpResponse (org.eclipse.jetty.client.HttpResponse)1 ContentResponse (org.eclipse.jetty.client.api.ContentResponse)1 Response (org.eclipse.jetty.client.api.Response)1