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);
}
}
}
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));
}
}
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));
}
}
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));
}
}
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"));
}
Aggregations