use of javax.websocket.Session in project jetty.project by eclipse.
the class CookiesTest method testCookiesAreSentToServer.
@Test
public void testCookiesAreSentToServer() throws Exception {
final String cookieName = "name";
final String cookieValue = "value";
final String cookieString = cookieName + "=" + cookieValue;
startServer(new EchoHandler() {
@Override
public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
List<HttpCookie> cookies = request.getCookies();
assertThat("Cookies", cookies, notNullValue());
assertThat("Cookies", cookies.size(), is(1));
HttpCookie cookie = cookies.get(0);
Assert.assertEquals(cookieName, cookie.getName());
Assert.assertEquals(cookieValue, cookie.getValue());
Map<String, List<String>> headers = request.getHeaders();
// Test case insensitivity
Assert.assertTrue(headers.containsKey("cookie"));
List<String> values = headers.get("Cookie");
Assert.assertNotNull(values);
Assert.assertEquals(1, values.size());
Assert.assertEquals(cookieString, values.get(0));
return super.createWebSocket(request, response);
}
});
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
builder.configurator(new ClientEndpointConfig.Configurator() {
@Override
public void beforeRequest(Map<String, List<String>> headers) {
headers.put("Cookie", Collections.singletonList(cookieString));
}
});
ClientEndpointConfig config = builder.build();
Endpoint endPoint = new Endpoint() {
@Override
public void onOpen(Session session, EndpointConfig config) {
}
};
Session session = container.connectToServer(endPoint, config, URI.create("ws://localhost:" + connector.getLocalPort()));
session.close();
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class MessageReceivingTest method testPartialTextMessage.
/**
* Method tests receiving of text messages by parts.
*
* @throws Exception on exception occur
*/
@Test
public void testPartialTextMessage() throws Exception {
final TestEndpoint echoer = new TestEndpoint(new PartialStringCaptureHandler());
Assert.assertThat(echoer, instanceOf(javax.websocket.Endpoint.class));
// Issue connect using instance of class that extends Endpoint
final Session session = container.connectToServer(echoer, serverUri);
if (LOG.isDebugEnabled())
LOG.debug("Client Connected: {}", session);
session.getBasicRemote().sendText("");
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
echoer.handler.getMessageQueue().awaitMessages(2, 1000, TimeUnit.MILLISECONDS);
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class ExampleClient method run.
private void run() throws DeploymentException, IOException, URISyntaxException, InterruptedException {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
System.out.printf("WebSocketContainer Impl: %s%n", container.getClass().getName());
ExampleSocket socket = new ExampleSocket();
URI uri = new URI("ws://echo.websocket.org/");
Session session = container.connectToServer(socket, uri);
RemoteEndpoint.Basic remote = session.getBasicRemote();
String msg = "Hello world";
System.out.printf("Sending: %s%n", Objects.toString(msg));
remote.sendText(msg);
// give remote 1 second to respond
socket.messageLatch.await(1, TimeUnit.SECONDS);
session.close();
// give remote 1 second to acknowledge response
socket.closeLatch.await(1, TimeUnit.SECONDS);
System.out.println("Socket is closed");
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class EndpointEchoTest method testAbstractEchoClassref.
@Test
public void testAbstractEchoClassref() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
// Issue connect using class reference (class that extends abstract that extends Endpoint)
Session session = container.connectToServer(EchoStringEndpoint.class, serverUri);
if (LOG.isDebugEnabled())
LOG.debug("Client Connected: {}", session);
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
// TODO: figure out echo verification.
// echoer.messageQueue.awaitMessages(1,1000,TimeUnit.MILLISECONDS);
}
use of javax.websocket.Session in project jetty.project by eclipse.
the class EndpointEchoTest method testBasicEchoInstance.
@Test
public void testBasicEchoInstance() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
EndpointEchoClient echoer = new EndpointEchoClient();
Assert.assertThat(echoer, instanceOf(javax.websocket.Endpoint.class));
// Issue connect using instance of class that extends Endpoint
Session session = container.connectToServer(echoer, serverUri);
if (LOG.isDebugEnabled())
LOG.debug("Client Connected: {}", session);
session.getBasicRemote().sendText("Echo");
if (LOG.isDebugEnabled())
LOG.debug("Client Message Sent");
echoer.textCapture.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
}
Aggregations