use of io.undertow.websockets.jsr.ServerWebSocketContainer in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testBinaryWithByteBuffer.
@org.junit.Test
public void testBinaryWithByteBuffer() throws Exception {
final byte[] payload = "payload".getBytes();
final AtomicReference<Throwable> cause = new AtomicReference<>();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult<?> latch = new FutureResult<>();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {
@Override
public void onMessage(ByteBuffer message) {
ByteBuffer buf = ByteBuffer.allocate(message.remaining());
buf.put(message);
buf.flip();
session.getAsyncRemote().sendBinary(buf);
}
});
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + NetworkUtils.formatPossibleIpv6Address(DefaultServer.getHostAddress("default")) + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
// FIXME UNDERTOW-1862 assertEquals(DONE, latch.getIoFuture().await());
latch.getIoFuture().await();
assertNull(cause.get());
client.destroy();
}
use of io.undertow.websockets.jsr.ServerWebSocketContainer in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testCloseFrame.
@org.junit.Test
public void testCloseFrame() throws Exception {
final int code = 1000;
final String reasonText = "TEST";
final AtomicReference<CloseReason> reason = new AtomicReference<>();
ByteBuffer payload = ByteBuffer.allocate(reasonText.length() + 2);
payload.putShort((short) code);
payload.put(reasonText.getBytes(StandardCharsets.UTF_8));
payload.flip();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult<?> latch = new FutureResult<>();
final CountDownLatch clientLatch = new CountDownLatch(1);
final AtomicInteger closeCount = new AtomicInteger();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
}
@Override
public void onClose(Session session, CloseReason closeReason) {
closeCount.incrementAndGet();
reason.set(closeReason);
clientLatch.countDown();
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new CloseWebSocketFrame(code, reasonText), new FrameChecker(CloseWebSocketFrame.class, payload.array(), latch));
// FIXME UNDERTOW-1862 assertEquals(DONE, latch.getIoFuture().await());
latch.getIoFuture().await();
clientLatch.await();
assertEquals(code, reason.get().getCloseCode().getCode());
assertEquals(reasonText, reason.get().getReasonPhrase());
assertEquals(1, closeCount.get());
client.destroy();
}
use of io.undertow.websockets.jsr.ServerWebSocketContainer in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testCloseFrameWithoutReasonBody.
/**
* Section 5.5.1 of RFC 6455 says the reason body is optional
*/
@org.junit.Test
public void testCloseFrameWithoutReasonBody() throws Exception {
final int code = 1000;
final AtomicReference<CloseReason> reason = new AtomicReference<>();
ByteBuffer payload = ByteBuffer.allocate(2);
payload.putShort((short) code);
payload.flip();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult<?> latch = new FutureResult<>();
final CountDownLatch clientLatch = new CountDownLatch(1);
final AtomicInteger closeCount = new AtomicInteger();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
}
@Override
public void onClose(Session session, CloseReason closeReason) {
closeCount.incrementAndGet();
reason.set(closeReason);
clientLatch.countDown();
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new CloseWebSocketFrame(code, null), new FrameChecker(CloseWebSocketFrame.class, payload.array(), latch));
assertEquals(DONE, latch.getIoFuture().await(10, TimeUnit.SECONDS));
clientLatch.await();
assertEquals(code, reason.get().getCloseCode().getCode());
assertEquals("", reason.get().getReasonPhrase());
assertEquals(1, closeCount.get());
client.destroy();
}
use of io.undertow.websockets.jsr.ServerWebSocketContainer in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testErrorHandling.
// FIXME UNDERTOW-1862 @Test
public void testErrorHandling() throws Exception {
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(ProgramaticErrorEndpoint.class, "/").configurator(new InstanceConfigurator(new ProgramaticErrorEndpoint())).build());
deployServlet(builder);
AnnotatedClientEndpoint c = new AnnotatedClientEndpoint();
Session session = ContainerProvider.getWebSocketContainer().connectToServer(c, new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
assertEquals("hi", ProgramaticErrorEndpoint.getMessage());
session.getAsyncRemote().sendText("app-error");
assertEquals("app-error", ProgramaticErrorEndpoint.getMessage());
assertEquals("ERROR: java.lang.RuntimeException", ProgramaticErrorEndpoint.getMessage());
assertTrue(c.isOpen());
session.getBasicRemote().sendText("io-error");
assertEquals("io-error", ProgramaticErrorEndpoint.getMessage());
assertEquals("ERROR: java.lang.RuntimeException", ProgramaticErrorEndpoint.getMessage());
assertTrue(c.isOpen());
((UndertowSession) session).forceClose();
assertEquals("CLOSED", ProgramaticErrorEndpoint.getMessage());
}
use of io.undertow.websockets.jsr.ServerWebSocketContainer in project undertow by undertow-io.
the class JsrWebSocketServer07Test method testTextByCompletion.
@org.junit.Test
public void testTextByCompletion() throws Exception {
final byte[] payload = "payload".getBytes();
final AtomicReference<SendResult> sendResult = new AtomicReference<>();
final AtomicBoolean connected = new AtomicBoolean(false);
final FutureResult<?> latch = new FutureResult<>();
final FutureResult<Void> latch2 = new FutureResult<>();
class TestEndPoint extends Endpoint {
@Override
public void onOpen(final Session session, EndpointConfig config) {
connected.set(true);
session.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String message) {
session.getAsyncRemote().sendText(message, result -> {
sendResult.set(result);
if (result.getException() != null) {
latch2.setException(new IOException(result.getException()));
} else {
latch2.setResult(null);
}
});
}
});
}
}
ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorkerSupplier(), DefaultServer.getBufferPool(), Collections.emptyList(), false, false);
builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
deployServlet(builder);
WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
client.connect();
client.send(new TextWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(TextWebSocketFrame.class, payload, latch));
// FIXME UNDERTOW-1862 assertEquals(DONE, latch.getIoFuture().await());
latch.getIoFuture().await();
assertEquals(DONE, latch2.getIoFuture().await());
SendResult result = sendResult.get();
Assert.assertNotNull(result);
assertNull(result.getException());
client.destroy();
}
Aggregations