use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class TextPayloadParserTest method testFrameTooLargeDueToPolicy.
@Test
public void testFrameTooLargeDueToPolicy() throws Exception {
WebSocketPolicy policy = new WebSocketPolicy(WebSocketBehavior.SERVER);
// Artificially small buffer/payload
// read buffer
policy.setInputBufferSize(1024);
// streaming buffer (not used in this test)
policy.setMaxTextMessageBufferSize(1024);
// actual maximum text message size policy
policy.setMaxTextMessageSize(1024);
byte[] utf = new byte[2048];
Arrays.fill(utf, (byte) 'a');
Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 8);
// text frame, fin = true
buf.put((byte) 0x81);
// 0x7E == 126 (a 2 byte payload length)
buf.put((byte) (0x80 | 0x7E));
buf.putShort((short) utf.length);
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
buf.flip();
UnitParser parser = new UnitParser(policy);
IncomingFramesCapture capture = new IncomingFramesCapture();
parser.setIncomingFramesHandler(capture);
parser.parseQuietly(buf);
capture.assertHasErrors(MessageTooLargeException.class, 1);
capture.assertHasNoFrames();
MessageTooLargeException err = (MessageTooLargeException) capture.getErrors().poll();
Assert.assertThat("Error.closeCode", err.getStatusCode(), is(StatusCode.MESSAGE_TOO_LARGE));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class TextPayloadParserTest method testMediumMaskedText.
@Test
public void testMediumMaskedText() throws Exception {
StringBuffer sb = new StringBuffer();
;
for (int i = 0; i < 14; i++) {
sb.append("Hello Medium World ");
}
sb.append(". The end.");
String expectedText = sb.toString();
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
Assert.assertThat("Must be a medium length payload", utf.length, allOf(greaterThan(0x7E), lessThan(0xFFFF)));
ByteBuffer buf = ByteBuffer.allocate(utf.length + 10);
buf.put((byte) 0x81);
// 0x7E == 126 (a 2 byte payload length)
buf.put((byte) (0x80 | 0x7E));
buf.putShort((short) utf.length);
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
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.TEXT, 1);
WebSocketFrame txt = capture.getFrames().poll();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class TextPayloadParserTest method testShortMaskedUtf8Text.
@Test
public void testShortMaskedUtf8Text() throws Exception {
String expectedText = "Hello World";
byte[] utf = expectedText.getBytes(StandardCharsets.UTF_8);
ByteBuffer buf = ByteBuffer.allocate(24);
buf.put((byte) 0x81);
buf.put((byte) (0x80 | utf.length));
MaskedByteBuffer.putMask(buf);
MaskedByteBuffer.putPayload(buf, utf);
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.TEXT, 1);
WebSocketFrame txt = capture.getFrames().poll();
Assert.assertThat("TextFrame.data", txt.getPayloadAsUTF8(), is(expectedText));
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class OnPartialTest method toEventDriver.
public EventDriver toEventDriver(Object websocket) throws Throwable {
WebSocketPolicy policy = WebSocketPolicy.newServerPolicy();
policy.setInputBufferSize(1024);
policy.setMaxBinaryMessageBufferSize(1024);
policy.setMaxTextMessageBufferSize(1024);
// Create EventDriver
EventDriverImpl driverImpl = new JsrServerEndpointImpl();
Class<?> endpoint = websocket.getClass();
ServerEndpoint anno = endpoint.getAnnotation(ServerEndpoint.class);
Assert.assertThat("Endpoint: " + endpoint + " should be annotated with @ServerEndpoint", anno, notNullValue());
WebSocketContainerScope containerScope = new SimpleContainerScope(policy);
// Event Driver Factory
EventDriverFactory factory = new EventDriverFactory(containerScope);
factory.addImplementation(new JsrServerEndpointImpl());
ServerEndpointConfig config = new BasicServerEndpointConfig(containerScope, endpoint, "/");
AnnotatedServerEndpointMetadata metadata = new AnnotatedServerEndpointMetadata(containerScope, endpoint, config);
AnnotatedEndpointScanner<ServerEndpoint, ServerEndpointConfig> scanner = new AnnotatedEndpointScanner<>(metadata);
scanner.scan();
EndpointInstance ei = new EndpointInstance(websocket, config, metadata);
EventDriver driver = driverImpl.create(ei, policy);
Assert.assertThat("EventDriver", driver, notNullValue());
// Create Local JsrSession
String id = testname.getMethodName();
URI requestURI = URI.create("ws://localhost/" + id);
DummyConnection connection = new DummyConnection();
ClientContainer container = new ClientContainer();
container.start();
@SuppressWarnings("resource") JsrSession session = new JsrSession(container, id, requestURI, driver, connection);
session.setPolicy(policy);
session.start();
session.open();
return driver;
}
use of org.eclipse.jetty.websocket.api.WebSocketPolicy in project jetty.project by eclipse.
the class JsrSessionTest method initSession.
@Before
public void initSession() {
container = new ClientContainer();
String id = JsrSessionTest.class.getSimpleName();
URI requestURI = URI.create("ws://localhost/" + id);
WebSocketPolicy policy = WebSocketPolicy.newClientPolicy();
ClientEndpointConfig config = new EmptyClientEndpointConfig();
DummyEndpoint websocket = new DummyEndpoint();
SimpleEndpointMetadata metadata = new SimpleEndpointMetadata(websocket.getClass());
// Executor executor = null;
EndpointInstance ei = new EndpointInstance(websocket, config, metadata);
EventDriver driver = new JsrEndpointEventDriver(policy, ei);
DummyConnection connection = new DummyConnection();
session = new JsrSession(container, id, requestURI, driver, connection);
}
Aggregations