use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.
the class ConfiguratorTest method testEndpointHandshakeInfo.
@Test
public void testEndpointHandshakeInfo() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
EndpointEchoClient echoer = new EndpointEchoClient();
// Build Config
ClientEndpointConfig.Builder cfgbldr = ClientEndpointConfig.Builder.create();
TrackingConfigurator configurator = new TrackingConfigurator();
cfgbldr.configurator(configurator);
ClientEndpointConfig config = cfgbldr.build();
// Connect
Session session = container.connectToServer(echoer, config, serverUri);
// Send Simple Message
session.getBasicRemote().sendText("Echo");
// Wait for echo
echoer.textCapture.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
// Validate client side configurator use
Assert.assertThat("configurator.request", configurator.request, notNullValue());
Assert.assertThat("configurator.response", configurator.response, notNullValue());
}
use of javax.websocket.WebSocketContainer in project jetty.project by eclipse.
the class CookiesTest method testCookiesAreSentToClient.
@Test
public void testCookiesAreSentToClient() throws Exception {
final String cookieName = "name";
final String cookieValue = "value";
final String cookieDomain = "domain";
final String cookiePath = "/path";
startServer(new EchoHandler() {
@Override
public Object createWebSocket(ServletUpgradeRequest request, ServletUpgradeResponse response) {
String cookieString = cookieName + "=" + cookieValue + ";Domain=" + cookieDomain + ";Path=" + cookiePath;
response.getHeaders().put("Set-Cookie", Collections.singletonList(cookieString));
return super.createWebSocket(request, response);
}
});
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig.Builder builder = ClientEndpointConfig.Builder.create();
builder.configurator(new ClientEndpointConfig.Configurator() {
@Override
public void afterResponse(HandshakeResponse response) {
Map<String, List<String>> headers = response.getHeaders();
// Test case insensitivity
Assert.assertTrue(headers.containsKey("set-cookie"));
List<String> values = headers.get("Set-Cookie");
Assert.assertNotNull(values);
Assert.assertEquals(1, values.size());
List<HttpCookie> cookies = HttpCookie.parse(values.get(0));
Assert.assertEquals(1, cookies.size());
HttpCookie cookie = cookies.get(0);
Assert.assertEquals(cookieName, cookie.getName());
Assert.assertEquals(cookieValue, cookie.getValue());
Assert.assertEquals(cookieDomain, cookie.getDomain());
Assert.assertEquals(cookiePath, cookie.getPath());
}
});
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.WebSocketContainer 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.WebSocketContainer in project jetty.project by eclipse.
the class MisbehavingClassTest method testAnnotatedRuntimeOnOpen.
@SuppressWarnings("Duplicates")
@Test
public void testAnnotatedRuntimeOnOpen() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
AnnotatedRuntimeOnOpen socket = new AnnotatedRuntimeOnOpen();
try (StacklessLogging logging = new StacklessLogging(AnnotatedRuntimeOnOpen.class, WebSocketSession.class)) {
// expecting IOException during onOpen
expectedException.expect(IOException.class);
expectedException.expectCause(instanceOf(RuntimeException.class));
container.connectToServer(socket, serverUri);
expectedException.reportMissingExceptionWithMessage("Should have failed .connectToServer()");
assertThat("Close should have occurred", socket.closeLatch.await(1, TimeUnit.SECONDS), is(true));
Throwable cause = socket.errors.pop();
assertThat("Error", cause, instanceOf(ArrayIndexOutOfBoundsException.class));
}
}
use of javax.websocket.WebSocketContainer 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");
}
Aggregations