use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class LargeAnnotatedTest method testEcho.
@Test
public void testEcho() throws Exception {
WSServer wsb = new WSServer(testdir, "app");
wsb.createWebInf();
wsb.copyEndpoint(LargeEchoConfiguredSocket.class);
try {
wsb.start();
URI uri = wsb.getServerBaseURI();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
// wsb.dump();
WebSocketClient client = new WebSocketClient(bufferPool);
try {
client.getPolicy().setMaxTextMessageSize(128 * 1024);
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<Session> foo = client.connect(clientEcho, uri.resolve("echo/large"));
// wait for connect
foo.get(1, TimeUnit.SECONDS);
// The message size should be bigger than default, but smaller than the limit that LargeEchoSocket specifies
byte[] txt = new byte[100 * 1024];
Arrays.fill(txt, (byte) 'o');
String msg = new String(txt, StandardCharsets.UTF_8);
clientEcho.sendMessage(msg);
Queue<String> msgs = clientEcho.awaitMessages(1);
Assert.assertEquals("Expected message", msg, msgs.poll());
} finally {
client.stop();
}
} finally {
wsb.stop();
}
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class OnMessageReturnTest method testEchoReturn.
@Test
public void testEchoReturn() throws Exception {
WSServer wsb = new WSServer(testdir, "app");
wsb.copyWebInf("empty-web.xml");
wsb.copyClass(EchoReturnEndpoint.class);
try {
wsb.start();
URI uri = wsb.getServerBaseURI();
WebAppContext webapp = wsb.createWebAppContext();
wsb.deployWebapp(webapp);
WebSocketClient client = new WebSocketClient(bufferPool);
try {
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<Session> future = client.connect(clientEcho, uri.resolve("echoreturn"));
// wait for connect
future.get(1, TimeUnit.SECONDS);
clientEcho.sendMessage("Hello World");
Queue<String> msgs = clientEcho.awaitMessages(1);
Assert.assertEquals("Expected message", "Hello World", msgs.poll());
} finally {
client.stop();
}
} finally {
wsb.stop();
}
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class IdleTimeoutTest method assertConnectionTimeout.
private void assertConnectionTimeout(URI uri) throws Exception, IOException, InterruptedException, ExecutionException, TimeoutException {
WebSocketClient client = new WebSocketClient(bufferPool);
try {
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
if (LOG.isDebugEnabled())
LOG.debug("Client Attempting to connnect");
Future<Session> future = client.connect(clientEcho, uri);
// wait for connect
future.get(1, TimeUnit.SECONDS);
if (LOG.isDebugEnabled())
LOG.debug("Client Connected");
// wait 1 second
if (LOG.isDebugEnabled())
LOG.debug("Waiting 1 second");
TimeUnit.SECONDS.sleep(1);
if (LOG.isDebugEnabled())
LOG.debug("Waited 1 second");
if (clientEcho.getClosed() == false) {
// Try to write
clientEcho.sendMessage("You shouldn't be there");
try {
Queue<String> msgs = clientEcho.awaitMessages(1);
assertThat("Should not have received messages echoed back", msgs, is(empty()));
} catch (TimeoutException | InterruptedException e) {
// valid success path
}
}
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class SessionTest method assertResponse.
private void assertResponse(String requestPath, String requestMessage, String expectedResponse) throws Exception {
WebSocketClient client = new WebSocketClient(bufferPool);
try {
client.start();
JettyEchoSocket clientEcho = new JettyEchoSocket();
Future<Session> future = client.connect(clientEcho, serverUri.resolve(requestPath));
// wait for connect
future.get(1, TimeUnit.SECONDS);
clientEcho.sendMessage(requestMessage);
Queue<String> msgs = clientEcho.awaitMessages(1);
Assert.assertThat("Expected message", msgs.poll(), is(expectedResponse));
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.api.Session in project jetty.project by eclipse.
the class WebSocketOverSSLTest method testServerSessionRequestURI.
/**
* Test that server session.upgradeRequest.requestURI reports correctly
* @throws Exception on test failure
*/
@Test
public void testServerSessionRequestURI() throws Exception {
Assert.assertThat("server scheme", server.getServerUri().getScheme(), is("wss"));
WebSocketClient client = new WebSocketClient(server.getSslContextFactory(), null, bufferPool);
try {
client.setConnectTimeout(CONNECT_TIMEOUT);
client.start();
CaptureSocket clientSocket = new CaptureSocket();
URI requestUri = server.getServerUri().resolve("/deep?a=b");
System.err.printf("Request URI: %s%n", requestUri.toASCIIString());
Future<Session> fut = client.connect(clientSocket, requestUri);
// wait for connect
Session session = fut.get(FUTURE_TIMEOUT_SEC, TimeUnit.SECONDS);
// Generate text frame
RemoteEndpoint remote = session.getRemote();
remote.sendString("session.upgradeRequest.requestURI");
if (remote.getBatchMode() == BatchMode.ON)
remote.flush();
// Read frame (hopefully text frame)
clientSocket.messages.awaitEventCount(1, 30, TimeUnit.SECONDS);
EventQueue<String> captured = clientSocket.messages;
String expected = String.format("session.upgradeRequest.requestURI=%s", requestUri.toASCIIString());
Assert.assertThat("session.upgradeRequest.requestURI", captured.poll(), is(expected));
// Shutdown the socket
clientSocket.close();
} finally {
client.stop();
}
}
Aggregations