use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class ClientConnectTest method testUpgradeWithAuthorizationHeader.
@Test
public void testUpgradeWithAuthorizationHeader() throws Exception {
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri();
ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest();
// actual value for this test is irrelevant, its important that this
// header actually be sent with a value (the value specified)
upgradeRequest.setHeader("Authorization", "Bogus SHA1");
Future<Session> future = client.connect(wsocket, wsUri, upgradeRequest);
IBlockheadServerConnection connection = server.accept();
List<String> requestLines = connection.upgrade();
Session sess = future.get(30, TimeUnit.SECONDS);
sess.close();
String authLine = requestLines.stream().filter((line) -> line.startsWith("Authorization:")).findFirst().get();
assertThat("Request Container Authorization", authLine, is("Authorization: Bogus SHA1"));
assertThat("Connect.UpgradeRequest", wsocket.connectUpgradeRequest, notNullValue());
assertThat("Connect.UpgradeResponse", wsocket.connectUpgradeResponse, notNullValue());
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class SlowServerTest method testServerSlowToRead.
@Test
@Slow
public void testServerSlowToRead() throws Exception {
JettyTrackingSocket tsocket = new JettyTrackingSocket();
client.setMasker(new ZeroMasker());
client.setMaxIdleTimeout(60000);
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(tsocket, wsUri);
IBlockheadServerConnection sconnection = server.accept();
sconnection.setSoTimeout(60000);
sconnection.upgrade();
// Confirm connected
future.get(30, TimeUnit.SECONDS);
tsocket.waitForConnected(30, TimeUnit.SECONDS);
int messageCount = 10;
// Setup slow server read thread
ServerReadThread reader = new ServerReadThread(sconnection, messageCount);
// slow it down
reader.setSlowness(100);
reader.start();
// Have client write as quickly as it can.
ClientWriteThread writer = new ClientWriteThread(tsocket.getSession());
writer.setMessageCount(messageCount);
writer.setMessage("Hello");
// disable slowness
writer.setSlowness(-1);
writer.start();
writer.join();
// Verify receive
reader.waitForExpectedMessageCount(10, TimeUnit.SECONDS);
Assert.assertThat("Frame Receive Count", reader.getFrameCount(), is(messageCount));
// Close
tsocket.getSession().close(StatusCode.NORMAL, "Done");
Assert.assertTrue("Client Socket Closed", tsocket.closeLatch.await(10, TimeUnit.SECONDS));
tsocket.assertCloseCode(StatusCode.NORMAL);
// stop reading
reader.cancel();
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class SlowServerTest method testServerSlowToSend.
@Test
@Slow
public void testServerSlowToSend() throws Exception {
JettyTrackingSocket clientSocket = new JettyTrackingSocket();
client.setMasker(new ZeroMasker());
client.setMaxIdleTimeout(60000);
URI wsUri = server.getWsUri();
Future<Session> clientConnectFuture = client.connect(clientSocket, wsUri);
IBlockheadServerConnection serverConn = server.accept();
serverConn.setSoTimeout(60000);
serverConn.upgrade();
// Confirm connected
clientConnectFuture.get(30, TimeUnit.SECONDS);
clientSocket.waitForConnected(30, TimeUnit.SECONDS);
// Have server write slowly.
int messageCount = 1000;
ServerWriteThread writer = new ServerWriteThread(serverConn);
writer.setMessageCount(messageCount);
writer.setMessage("Hello");
writer.setSlowness(10);
writer.start();
writer.join();
// Verify receive
Assert.assertThat("Message Receive Count", clientSocket.messageQueue.size(), is(messageCount));
// Close
serverConn.close(StatusCode.NORMAL);
Assert.assertTrue("Client Socket Closed", clientSocket.closeLatch.await(10, TimeUnit.SECONDS));
clientSocket.assertCloseCode(StatusCode.NORMAL);
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class WebSocketClientTest method testMaxMessageSize.
/**
* Ensure that <code>@WebSocket(maxTextMessageSize = 100*1024)</code> behaves as expected.
*
* @throws Exception
* on test failure
*/
@Test
public void testMaxMessageSize() throws Exception {
MaxMessageSocket wsocket = new MaxMessageSocket();
URI wsUri = server.getWsUri();
Future<Session> future = client.connect(wsocket, wsUri);
IBlockheadServerConnection ssocket = server.accept();
ssocket.upgrade();
wsocket.awaitConnect(1, TimeUnit.SECONDS);
Session sess = future.get(30, TimeUnit.SECONDS);
Assert.assertThat("Session", sess, notNullValue());
Assert.assertThat("Session.open", sess.isOpen(), is(true));
// Create string that is larger than default size of 64k
// but smaller than maxMessageSize of 100k
byte[] buf = new byte[80 * 1024];
Arrays.fill(buf, (byte) 'x');
String msg = StringUtil.toUTF8String(buf, 0, buf.length);
wsocket.getSession().getRemote().sendStringByFuture(msg);
ssocket.echoMessage(1, 2, TimeUnit.SECONDS);
// wait for response from server
wsocket.waitForMessage(1, TimeUnit.SECONDS);
wsocket.assertMessage(msg);
Assert.assertTrue(wsocket.dataLatch.await(2, TimeUnit.SECONDS));
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class WebSocketClientTest method testParameterMap.
@Test
public void testParameterMap() throws Exception {
JettyTrackingSocket wsocket = new JettyTrackingSocket();
URI wsUri = server.getWsUri().resolve("/test?snack=cashews&amount=handful&brand=off");
Future<Session> future = client.connect(wsocket, wsUri);
IBlockheadServerConnection ssocket = server.accept();
ssocket.upgrade();
future.get(30, TimeUnit.SECONDS);
Assert.assertTrue(wsocket.openLatch.await(1, TimeUnit.SECONDS));
Session session = wsocket.getSession();
UpgradeRequest req = session.getUpgradeRequest();
Assert.assertThat("Upgrade Request", req, notNullValue());
Map<String, List<String>> parameterMap = req.getParameterMap();
Assert.assertThat("Parameter Map", parameterMap, notNullValue());
Assert.assertThat("Parameter[snack]", parameterMap.get("snack"), is(Arrays.asList(new String[] { "cashews" })));
Assert.assertThat("Parameter[amount]", parameterMap.get("amount"), is(Arrays.asList(new String[] { "handful" })));
Assert.assertThat("Parameter[brand]", parameterMap.get("brand"), is(Arrays.asList(new String[] { "off" })));
Assert.assertThat("Parameter[cost]", parameterMap.get("cost"), nullValue());
}
Aggregations