use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.
the class WebSocketServerSessionTest method testDisconnect.
@Test
public void testDisconnect() throws Exception {
URI uri = server.getServerUri().resolve("/test/disconnect");
try (IBlockheadClient client = new BlockheadClient(uri)) {
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
client.write(new TextFrame().setPayload("harsh-disconnect"));
client.awaitDisconnect(1, TimeUnit.SECONDS);
}
}
use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.
the class WebSocketServletRFCTest method testEcho.
/**
* Test the requirement of issuing socket and receiving echo response
* @throws Exception on test failure
*/
@Test
public void testEcho() throws Exception {
BlockheadClient client = new BlockheadClient(server.getServerUri());
try {
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
// Generate text frame
String msg = "this is an echo ... cho ... ho ... o";
client.write(new TextFrame().setPayload(msg));
// Read frame (hopefully text frame)
EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
WebSocketFrame tf = frames.poll();
Assert.assertThat("Text Frame.status code", tf.getPayloadAsUTF8(), is(msg));
} finally {
client.close();
}
}
use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.
the class WebSocketServletRFCTest method testLowercaseUpgrade.
/**
* Test http://tools.ietf.org/html/rfc6455#section-4.1 where server side upgrade handling is supposed to be case insensitive.
* <p>
* This test will simulate a client requesting upgrade with all lowercase headers.
* @throws Exception on test failure
*/
@Test
public void testLowercaseUpgrade() throws Exception {
BlockheadClient client = new BlockheadClient(server.getServerUri());
try {
client.connect();
StringBuilder req = new StringBuilder();
req.append("GET ").append(client.getRequestPath()).append(" HTTP/1.1\r\n");
req.append("Host: ").append(client.getRequestHost()).append("\r\n");
req.append("Upgrade: websocket\r\n");
req.append("connection: upgrade\r\n");
req.append("sec-websocket-key: ").append(client.getRequestWebSocketKey()).append("\r\n");
req.append("sec-websocket-origin: ").append(client.getRequestWebSocketOrigin()).append("\r\n");
req.append("sec-websocket-protocol: echo\r\n");
req.append("sec-websocket-version: 13\r\n");
req.append("\r\n");
client.writeRaw(req.toString());
client.expectUpgradeResponse();
// Generate text frame
String msg = "this is an echo ... cho ... ho ... o";
client.write(new TextFrame().setPayload(msg));
// Read frame (hopefully text frame)
EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
WebSocketFrame tf = frames.poll();
Assert.assertThat("Text Frame.status code", tf.getPayloadAsUTF8(), is(msg));
} finally {
client.close();
}
}
use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.
the class WebSocketServletRFCTest method testTextNotUTF8.
@Test
public void testTextNotUTF8() throws Exception {
try (StacklessLogging stackless = new StacklessLogging(Parser.class);
BlockheadClient client = new BlockheadClient(server.getServerUri())) {
client.setProtocols("other");
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
byte[] buf = new byte[] { (byte) 0xC2, (byte) 0xC3 };
WebSocketFrame txt = new TextFrame().setPayload(ByteBuffer.wrap(buf));
txt.setMask(Hex.asByteArray("11223344"));
ByteBuffer bbHeader = generator.generateHeaderBytes(txt);
client.writeRaw(bbHeader);
client.writeRaw(txt.getPayload());
EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
WebSocketFrame frame = frames.poll();
Assert.assertThat("frames[0].opcode", frame.getOpCode(), is(OpCode.CLOSE));
CloseInfo close = new CloseInfo(frame);
Assert.assertThat("Close Status Code", close.getStatusCode(), is(StatusCode.BAD_PAYLOAD));
}
}
use of org.eclipse.jetty.websocket.common.test.BlockheadClient in project jetty.project by eclipse.
the class WebSocketServletRFCTest method testBinaryAggregate.
/**
* Test that aggregation of binary frames into a single message occurs
* @throws Exception on test failure
*/
@Test
public void testBinaryAggregate() throws Exception {
BlockheadClient client = new BlockheadClient(server.getServerUri());
try {
client.connect();
client.sendStandardRequest();
client.expectUpgradeResponse();
// Generate binary frames
byte[] buf1 = new byte[128];
byte[] buf2 = new byte[128];
byte[] buf3 = new byte[128];
Arrays.fill(buf1, (byte) 0xAA);
Arrays.fill(buf2, (byte) 0xBB);
Arrays.fill(buf3, (byte) 0xCC);
WebSocketFrame bin;
bin = new BinaryFrame().setPayload(buf1).setFin(false);
// write buf1 (fin=false)
client.write(bin);
bin = new ContinuationFrame().setPayload(buf2).setFin(false);
// write buf2 (fin=false)
client.write(bin);
bin = new ContinuationFrame().setPayload(buf3).setFin(true);
// write buf3 (fin=true)
client.write(bin);
// Read frame echo'd back (hopefully a single binary frame)
EventQueue<WebSocketFrame> frames = client.readFrames(1, 30, TimeUnit.SECONDS);
Frame binmsg = frames.poll();
int expectedSize = buf1.length + buf2.length + buf3.length;
Assert.assertThat("BinaryFrame.payloadLength", binmsg.getPayloadLength(), is(expectedSize));
int aaCount = 0;
int bbCount = 0;
int ccCount = 0;
ByteBuffer echod = binmsg.getPayload();
while (echod.remaining() >= 1) {
byte b = echod.get();
switch(b) {
case (byte) 0xAA:
aaCount++;
break;
case (byte) 0xBB:
bbCount++;
break;
case (byte) 0xCC:
ccCount++;
break;
default:
Assert.fail(String.format("Encountered invalid byte 0x%02X", (byte) (0xFF & b)));
}
}
Assert.assertThat("Echoed data count for 0xAA", aaCount, is(buf1.length));
Assert.assertThat("Echoed data count for 0xBB", bbCount, is(buf2.length));
Assert.assertThat("Echoed data count for 0xCC", ccCount, is(buf3.length));
} finally {
client.close();
}
}
Aggregations