* Test that aggregation of binary frames into a single message occurs
*/
@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);
client.write(bin); // write buf1 (fin=false)
bin = new ContinuationFrame().setPayload(buf2).setFin(false);
client.write(bin); // write buf2 (fin=false)
bin = new ContinuationFrame().setPayload(buf3).setFin(true);
client.write(bin); // write buf3 (fin=true)
// Read frame echo'd back (hopefully a single binary frame)
EventQueue<WebSocketFrame> frames = client.readFrames(1,1000,TimeUnit.MILLISECONDS);
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();
}
}