use of javax.websocket.WebSocketContainer in project knox by apache.
the class ProxyInboundClientTest method testBinarymessage.
@Test(timeout = 3000)
public void testBinarymessage() throws IOException, DeploymentException {
final String textMessage = "Echo";
final ByteBuffer binarymessage = ByteBuffer.wrap(textMessage.getBytes());
final AtomicBoolean isTestComplete = new AtomicBoolean(false);
final WebSocketContainer container = ContainerProvider.getWebSocketContainer();
final ProxyInboundClient client = new ProxyInboundClient(new MessageEventCallback() {
/**
* A generic callback, can be left un-implemented
*
* @param message
*/
@Override
public void doCallback(String message) {
}
/**
* Callback when connection is established.
*
* @param session
*/
@Override
public void onConnectionOpen(Object session) {
}
/**
* Callback when connection is closed.
*
* @param reason
*/
@Override
public void onConnectionClose(CloseReason reason) {
isTestComplete.set(true);
}
/**
* Callback when there is an error in connection.
*
* @param cause
*/
@Override
public void onError(Throwable cause) {
isTestComplete.set(true);
}
/**
* Callback when a text message is received.
*
* @param message
* @param session
*/
@Override
public void onMessageText(String message, Object session) {
recievedMessage = message;
isTestComplete.set(true);
}
/**
* Callback when a binary message is received.
*
* @param message
* @param last
* @param session
*/
@Override
public void onMessageBinary(byte[] message, boolean last, Object session) {
recievedBinaryMessage = message;
isTestComplete.set(true);
}
});
Assert.assertThat(client, instanceOf(javax.websocket.Endpoint.class));
Session session = container.connectToServer(client, serverUri);
session.getBasicRemote().sendBinary(binarymessage);
while (!isTestComplete.get()) {
/* just wait for the test to finish */
}
Assert.assertEquals("Binary message does not match", textMessage, new String(recievedBinaryMessage));
}
use of javax.websocket.WebSocketContainer in project knox by apache.
the class ProxyInboundClientTest method testTextMaxBufferLimit.
@Test(timeout = 3000)
public void testTextMaxBufferLimit() throws IOException, DeploymentException {
final String longMessage = RandomStringUtils.random(100000);
final AtomicBoolean isTestComplete = new AtomicBoolean(false);
final WebSocketContainer container = ContainerProvider.getWebSocketContainer();
final ProxyInboundClient client = new ProxyInboundClient(new MessageEventCallback() {
/**
* A generic callback, can be left un-implemented
*
* @param message
*/
@Override
public void doCallback(String message) {
}
/**
* Callback when connection is established.
*
* @param session
*/
@Override
public void onConnectionOpen(Object session) {
}
/**
* Callback when connection is closed.
*
* @param reason
*/
@Override
public void onConnectionClose(CloseReason reason) {
isTestComplete.set(true);
}
/**
* Callback when there is an error in connection.
*
* @param cause
*/
@Override
public void onError(Throwable cause) {
isTestComplete.set(true);
}
/**
* Callback when a text message is received.
*
* @param message
* @param session
*/
@Override
public void onMessageText(String message, Object session) {
recievedMessage = message;
isTestComplete.set(true);
}
/**
* Callback when a binary message is received.
*
* @param message
* @param last
* @param session
*/
@Override
public void onMessageBinary(byte[] message, boolean last, Object session) {
}
});
Assert.assertThat(client, instanceOf(javax.websocket.Endpoint.class));
Session session = container.connectToServer(client, serverUri);
session.getBasicRemote().sendText(longMessage);
while (!isTestComplete.get()) {
/* just wait for the test to finish */
}
Assert.assertEquals(longMessage, recievedMessage);
}
use of javax.websocket.WebSocketContainer in project knox by apache.
the class WebsocketEchoTest method testGatewayRewriteEcho.
/**
* Test websocket rewrite rules proxying through gateway.
*
* @throws Exception
*/
@Test
public void testGatewayRewriteEcho() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
WebsocketClient client = new WebsocketClient();
Session session = container.connectToServer(client, new URI(serverUri.toString() + "gateway/websocket/123foo456bar/channels"));
session.getBasicRemote().sendText("Echo");
client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
assertThat(client.messageQueue.get(0), is("Echo"));
}
use of javax.websocket.WebSocketContainer in project knox by apache.
the class WebsocketEchoTest method testGatewayEcho.
/**
* Test websocket proxying through gateway.
*
* @throws Exception
*/
@Test
public void testGatewayEcho() throws Exception {
WebSocketContainer container = ContainerProvider.getWebSocketContainer();
WebsocketClient client = new WebsocketClient();
Session session = container.connectToServer(client, new URI(serverUri.toString() + "gateway/websocket/ws"));
session.getBasicRemote().sendText("Echo");
client.messageQueue.awaitMessages(1, 1000, TimeUnit.MILLISECONDS);
assertThat(client.messageQueue.get(0), is("Echo"));
}
use of javax.websocket.WebSocketContainer in project wildfly by wildfly.
the class WebSocketSecurityTestCase method makeCall.
@Override
protected void makeCall(final String user, final String pass, final int expectedCode) throws Exception {
AnnotatedClient endpoint = new AnnotatedClient();
endpoint.setCredentials(user, pass);
WebSocketContainer serverContainer = ContainerProvider.getWebSocketContainer();
if (expectedCode == 200) {
connectToServer(serverContainer, endpoint);
Assert.assertEquals("Hello anil", endpoint.getMessage());
} else {
boolean exceptionThrown = false;
try {
connectToServer(serverContainer, endpoint);
} catch (DeploymentException e) {
exceptionThrown = true;
} finally {
Assert.assertTrue("We expected that 'DeploymentException' is thrown as we provided incorrect " + "credentials to ws endpoint.", exceptionThrown);
}
}
}
Aggregations