use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class PerMessageDeflateExtensionTest method testPerMessageDeflateDefault.
/**
* Default configuration for permessage-deflate
* @throws Exception on test failure
*/
@Test
public void testPerMessageDeflateDefault() throws Exception {
Assume.assumeTrue("Server has permessage-deflate registered", server.getWebSocketServletFactory().getExtensionFactory().isAvailable("permessage-deflate"));
Assert.assertThat("server scheme", server.getServerUri().getScheme(), is(scheme));
int binBufferSize = (int) (msgSize * 1.5);
WebSocketPolicy serverPolicy = server.getWebSocketServletFactory().getPolicy();
// Ensure binBufferSize is sane (not smaller then other buffers)
binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageSize());
binBufferSize = Math.max(binBufferSize, serverPolicy.getMaxBinaryMessageBufferSize());
binBufferSize = Math.max(binBufferSize, this.inputBufferSize);
serverPolicy.setMaxBinaryMessageSize(binBufferSize);
serverPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
WebSocketClient client = new WebSocketClient(server.getSslContextFactory());
WebSocketPolicy clientPolicy = client.getPolicy();
clientPolicy.setMaxBinaryMessageSize(binBufferSize);
clientPolicy.setMaxBinaryMessageBufferSize(binBufferSize);
if (inputBufferSize > 0) {
clientPolicy.setInputBufferSize(inputBufferSize);
}
try {
client.start();
// Make sure the read times out if there are problems with the implementation
client.setMaxIdleTimeout(TimeUnit.SECONDS.toMillis(15));
CaptureSocket clientSocket = new CaptureSocket();
ClientUpgradeRequest request = new ClientUpgradeRequest();
request.addExtensions("permessage-deflate");
request.setSubProtocols("echo");
Future<Session> fut = client.connect(clientSocket, server.getServerUri(), request);
// Wait for connect
Session session = fut.get(30, TimeUnit.SECONDS);
assertThat("Response.extensions", getNegotiatedExtensionList(session), containsString("permessage-deflate"));
// Create message
byte[] msg = new byte[msgSize];
Random rand = new Random();
rand.setSeed(8080);
rand.nextBytes(msg);
// Calculate sha1
String sha1 = Sha1Sum.calculate(msg);
// Client sends first message
session.getRemote().sendBytes(ByteBuffer.wrap(msg));
clientSocket.messages.awaitEventCount(1, 5, TimeUnit.SECONDS);
String echoMsg = clientSocket.messages.poll();
Assert.assertThat("Echo'd Message", echoMsg, is("binary[sha1=" + sha1 + "]"));
} finally {
client.stop();
}
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class AbstractABCase method initGenerators.
@Before
public void initGenerators() {
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
strictGenerator = new Generator(policy, bufferPool, true);
laxGenerator = new Generator(policy, bufferPool, false);
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class ClosePayloadParserTest method testGameOver.
@Test
public void testGameOver() {
String expectedReason = "Game Over";
byte[] utf = expectedReason.getBytes(StandardCharsets.UTF_8);
ByteBuffer payload = ByteBuffer.allocate(utf.length + 2);
payload.putChar((char) StatusCode.NORMAL);
payload.put(utf, 0, utf.length);
payload.flip();
ByteBuffer buf = ByteBuffer.allocate(24);
// fin + close
buf.put((byte) (0x80 | OpCode.CLOSE));
buf.put((byte) (0x80 | payload.remaining()));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, payload);
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.CLOSE, 1);
CloseInfo close = new CloseInfo(capture.getFrames().poll());
Assert.assertThat("CloseFrame.statusCode", close.getStatusCode(), is(StatusCode.NORMAL));
Assert.assertThat("CloseFrame.data", close.getReason(), is(expectedReason));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleUnmasked64KByteBinaryMessage.
@Test
public void testSingleUnmasked64KByteBinaryMessage() {
int dataSize = 1024 * 64;
ByteBuffer buf = ByteBuffer.allocate((dataSize + 10));
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// 64 Kbytes binary message in a single unmasked frame
buf.put(new byte[] { (byte) 0x82, 0x7F });
// 64bit size
buf.putLong(dataSize);
for (int i = 0; i < dataSize; i++) {
buf.put((byte) 0x77);
}
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.CLIENT);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.BINARY, 1);
Frame bin = capture.getFrames().poll();
Assert.assertThat("BinaryFrame.payloadLength", bin.getPayloadLength(), is(dataSize));
ByteBuffer data = bin.getPayload();
Assert.assertThat("BinaryFrame.payload.length", data.remaining(), is(dataSize));
for (int i = 0; i < dataSize; i++) {
Assert.assertThat("BinaryFrame.payload[" + i + "]", data.get(i), is((byte) 0x77));
}
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class RFC6455ExamplesParserTest method testSingleMaskedPongRequest.
@Test
public void testSingleMaskedPongRequest() {
ByteBuffer buf = ByteBuffer.allocate(16);
// Raw bytes as found in RFC 6455, Section 5.7 - Examples
// Unmasked Pong request
buf.put(new byte[] { (byte) 0x8a, (byte) 0x85, 0x37, (byte) 0xfa, 0x21, 0x3d, 0x7f, (byte) 0x9f, 0x4d, 0x51, 0x58 });
buf.flip();
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
Parser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parse(buf);
capture.assertNoErrors();
capture.assertHasFrame(OpCode.PONG, 1);
WebSocketFrame pong = capture.getFrames().poll();
String actual = BufferUtil.toUTF8String(pong.getPayload());
Assert.assertThat("PongFrame.payload", actual, is("Hello"));
}
Aggregations