use of javax.websocket.Session in project undertow by undertow-io.
the class AnnotatedEndpointTest method testCloseReason.
@Test
public void testCloseReason() throws Exception {
MessageEndpoint.reset();
Session session = deployment.connectToServer(AnnotatedClientEndpoint.class, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/ws/chat/Bob"));
Assert.assertEquals("hi Bob (protocol=foo)", AnnotatedClientEndpoint.message());
session.close(new CloseReason(CloseReason.CloseCodes.VIOLATED_POLICY, "Foo!"));
Assert.assertEquals("CLOSED", AnnotatedClientEndpoint.message());
CloseReason cr = MessageEndpoint.getReason();
Assert.assertEquals(CloseReason.CloseCodes.VIOLATED_POLICY.getCode(), cr.getCloseCode().getCode());
Assert.assertEquals("Foo!", cr.getReasonPhrase());
}
use of javax.websocket.Session in project tomee by apache.
the class ClasspathAsWebappTest method run.
@Test
public void run() throws MalformedURLException {
MyInitializer.found = null;
MyBean.VALUE = null;
try (final Container container = new Container(new Configuration().http(NetworkUtil.getNextAvailablePort()).property("openejb.container.additional.exclude", "org.apache.tomee.embedded.").property("openejb.additional.include", "tomee-").user("tomee", "tomeepwd").loginConfig(new LoginConfigBuilder().basic()).securityConstaint(new SecurityConstaintBuilder().addAuthRole("**").authConstraint(true).addCollection("api", "/api/resource2/"))).deployPathsAsWebapp(JarLocation.jarLocation(MyInitializer.class)).inject(this)) {
// Servlet (initializer, servlet)
assertNotNull(MyInitializer.found);
final Iterator<Class<?>> it = MyInitializer.found.iterator();
while (it.hasNext()) {
// ThreadStackRule defines one for instance
final Class<?> next = it.next();
if (next.getEnclosingClass() != null && !Modifier.isStatic(next.getModifiers())) {
it.remove();
}
}
assertEquals(1, MyInitializer.found.size());
assertEquals(Task1.class, MyInitializer.found.iterator().next());
try {
assertEquals("Servlet!", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/s")));
} catch (final IOException e) {
fail(e.getMessage());
}
try {
assertEquals("WebServlet", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/w")));
} catch (final IOException e) {
fail(e.getMessage());
}
// JSP
try {
assertEquals("JSP", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/?test=JSP")).trim());
} catch (final IOException e) {
fail(e.getMessage());
}
// CDI
assertNotNull(bean);
assertNull(bean.value());
MyBean.VALUE = "cdi";
assertEquals("cdi", bean.value());
// EJB
MyBean.VALUE = "ejb";
assertEquals("ejb", anEjb.run());
// JAXRS
try {
assertEquals("jaxrs", IO.slurp(new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/api/resource")));
} catch (final IOException e) {
fail(e.getMessage());
}
// JAXRS + servlet security
try {
final URL url = new URL("http://localhost:" + container.getConfiguration().getHttpPort() + "/api/resource2/");
final HttpURLConnection c = HttpURLConnection.class.cast(url.openConnection());
c.setRequestProperty("Authorization", "Basic " + printBase64Binary("tomee:tomeepwd".getBytes()));
assertEquals("tomee", IO.slurp(c.getInputStream()));
c.disconnect();
} catch (final IOException e) {
fail(e.getMessage());
}
try {
assertEquals("tomee", IO.slurp(new URL("http://tomee:tomeepwd@localhost:" + container.getConfiguration().getHttpPort() + "/api/resource2/")));
fail("should have been not authorized");
} catch (final IOException e) {
// ok
}
// WebSocket
final WebSocketContainer webSocketContainer = ContainerProvider.getWebSocketContainer();
try {
WebSocketClient.message = null;
final WebSocketClient webSocketClient = new WebSocketClient();
final Session session = webSocketContainer.connectToServer(webSocketClient, new URI("ws://localhost:" + container.getConfiguration().getHttpPort() + "/ws"));
webSocketClient.latch.await(20, TimeUnit.SECONDS);
session.close();
assertEquals("websocket", WebSocketClient.message);
} catch (final Exception e) {
fail(e.getMessage());
}
}
}
use of javax.websocket.Session in project tomcat by apache.
the class TestShutdown method testShutdownBufferedMessages.
@Test
public void testShutdownBufferedMessages() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(EchoBufferedConfig.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
ClientEndpointConfig clientEndpointConfig = ClientEndpointConfig.Builder.create().build();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, clientEndpointConfig, new URI("ws://localhost:" + getPort() + "/test"));
CountDownLatch latch = new CountDownLatch(1);
BasicText handler = new BasicText(latch);
wsSession.addMessageHandler(handler);
wsSession.getBasicRemote().sendText("Hello");
int count = 0;
while (count < 10 && EchoBufferedEndpoint.messageCount.get() == 0) {
Thread.sleep(200);
count++;
}
Assert.assertNotEquals("Message not received by server", EchoBufferedEndpoint.messageCount.get(), 0);
tomcat.stop();
Assert.assertTrue("Latch expired waiting for message", latch.await(10, TimeUnit.SECONDS));
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWsRemoteEndpointImplServer method testClientDropsConnection.
/*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=58624
*
* This test requires three breakpoints to be set. Two in this file (marked
* A & B with comments) and one (C) at the start of
* WsRemoteEndpointImplServer.doWrite().
*
* With the breakpoints in place, run this test.
* Once breakpoints A & B are reached, progress the thread at breakpoint A
* one line to close the connection.
* Once breakpoint C is reached, allow the thread at breakpoint B to
* continue.
* Then allow the thread at breakpoint C to continue.
*
* In the failure mode, the thread at breakpoint B will not progress past
* the call to sendObject(). If the issue is fixed, the thread at breakpoint
* B will continue past sendObject() and terminate with a TimeoutException.
*/
@Test
public void testClientDropsConnection() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(Bug58624Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
tomcat.start();
SimpleClient client = new SimpleClient();
URI uri = new URI("ws://localhost:" + getPort() + Bug58624Config.PATH);
Session session = wsContainer.connectToServer(client, uri);
// Break point A required on following line
session.close();
}
use of javax.websocket.Session in project tomcat by apache.
the class TestWsSubprotocols method testWsSubprotocols.
@Test
public void testWsSubprotocols() throws Exception {
Tomcat tomcat = getTomcatInstance();
// No file system docBase required
Context ctx = tomcat.addContext("", null);
ctx.addApplicationListener(Config.class.getName());
Tomcat.addServlet(ctx, "default", new DefaultServlet());
ctx.addServletMappingDecoded("/", "default");
tomcat.start();
WebSocketContainer wsContainer = ContainerProvider.getWebSocketContainer();
tomcat.start();
Session wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().preferredSubprotocols(Arrays.asList("sp3")).build(), new URI("ws://localhost:" + getPort() + SubProtocolsEndpoint.PATH_BASIC));
Assert.assertTrue(wsSession.isOpen());
if (wsSession.getNegotiatedSubprotocol() != null) {
Assert.assertTrue(wsSession.getNegotiatedSubprotocol().isEmpty());
}
wsSession.close();
SubProtocolsEndpoint.recycle();
wsSession = wsContainer.connectToServer(TesterProgrammaticEndpoint.class, ClientEndpointConfig.Builder.create().preferredSubprotocols(Arrays.asList("sp2")).build(), new URI("ws://localhost:" + getPort() + SubProtocolsEndpoint.PATH_BASIC));
Assert.assertTrue(wsSession.isOpen());
Assert.assertEquals("sp2", wsSession.getNegotiatedSubprotocol());
// Client thread might move faster than server. Wait for upto 5s for the
// subProtocols to be set
int count = 0;
while (count < 50 && SubProtocolsEndpoint.subprotocols == null) {
count++;
Thread.sleep(100);
}
Assert.assertNotNull(SubProtocolsEndpoint.subprotocols);
Assert.assertArrayEquals(new String[] { "sp1", "sp2" }, SubProtocolsEndpoint.subprotocols.toArray(new String[2]));
wsSession.close();
SubProtocolsEndpoint.recycle();
}
Aggregations