Search in sources :

Example 1 with MessageTooLargeException

use of org.eclipse.jetty.websocket.api.MessageTooLargeException 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));
}
Also used : WebSocketPolicy(org.eclipse.jetty.websocket.api.WebSocketPolicy) MessageTooLargeException(org.eclipse.jetty.websocket.api.MessageTooLargeException) IncomingFramesCapture(org.eclipse.jetty.websocket.common.test.IncomingFramesCapture) UnitParser(org.eclipse.jetty.websocket.common.test.UnitParser) MaskedByteBuffer(org.eclipse.jetty.websocket.common.util.MaskedByteBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 MessageTooLargeException (org.eclipse.jetty.websocket.api.MessageTooLargeException)1 WebSocketPolicy (org.eclipse.jetty.websocket.api.WebSocketPolicy)1 IncomingFramesCapture (org.eclipse.jetty.websocket.common.test.IncomingFramesCapture)1 UnitParser (org.eclipse.jetty.websocket.common.test.UnitParser)1 MaskedByteBuffer (org.eclipse.jetty.websocket.common.util.MaskedByteBuffer)1 Test (org.junit.Test)1