예제 #1
0
  /**
   * Similar to the server side 6.2.3 testcase. Lots of small 1 byte UTF8 Text frames, representing
   * 1 overall text message.
   */
  @Test
  public void testParseCase6_2_3() {
    String utf8 = "Hello-\uC2B5@\uC39F\uC3A4\uC3BC\uC3A0\uC3A1-UTF-8!!";
    byte msg[] = StringUtil.getUtf8Bytes(utf8);

    List<WebSocketFrame> send = new ArrayList<>();
    int len = msg.length;
    byte opcode = OpCode.TEXT;
    byte mini[];
    for (int i = 0; i < len; i++) {
      WebSocketFrame frame = new WebSocketFrame(opcode);
      mini = new byte[1];
      mini[0] = msg[i];
      frame.setPayload(mini);
      boolean isLast = (i >= (len - 1));
      frame.setFin(isLast);
      send.add(frame);
      opcode = OpCode.CONTINUATION;
    }
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());

    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuf);

    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, len);
    capture.assertHasFrame(OpCode.CLOSE, 1);
  }
예제 #2
0
  /** Similar to the server side 5.6 testcase. pong, then text, then close frames. */
  @Test
  public void testParseCase5_6() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(WebSocketFrame.pong().setPayload("ping"));
    send.add(WebSocketFrame.text("hello, world"));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());

    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parser.parse(completeBuf);

    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, 1);
    capture.assertHasFrame(OpCode.CLOSE, 1);
    capture.assertHasFrame(OpCode.PONG, 1);
  }
예제 #3
0
  /**
   * Similar to the server side 5.18 testcase. Text message fragmented as 2 frames, both as
   * opcode=TEXT
   */
  @Test
  public void testParseCase5_18() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new WebSocketFrame(OpCode.TEXT).setPayload("fragment1").setFin(false));
    send.add(
        new WebSocketFrame(OpCode.TEXT)
            .setPayload("fragment2")
            .setFin(true)); // bad frame, must be continuation
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());

    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parseQuietly(parser, completeBuf);

    capture.assertErrorCount(1);
    capture.assertHasFrame(OpCode.TEXT, 1); // fragment 1
  }
예제 #4
0
  /**
   * Similar to the server side 5.19 testcase. text message, send in 5 frames/fragments, with 2
   * pings in the mix.
   */
  @Test
  public void testParseCase5_19() {
    List<WebSocketFrame> send = new ArrayList<>();
    send.add(new WebSocketFrame(OpCode.TEXT).setPayload("f1").setFin(false));
    send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f2").setFin(false));
    send.add(new WebSocketFrame(OpCode.PING).setPayload("pong-1"));
    send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f3").setFin(false));
    send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f4").setFin(false));
    send.add(new WebSocketFrame(OpCode.PING).setPayload("pong-2"));
    send.add(new WebSocketFrame(OpCode.CONTINUATION).setPayload(",f5").setFin(true));
    send.add(new CloseInfo(StatusCode.NORMAL).asFrame());

    ByteBuffer completeBuf = UnitGenerator.generate(send);
    UnitParser parser = new UnitParser();
    IncomingFramesCapture capture = new IncomingFramesCapture();
    parser.setIncomingFramesHandler(capture);
    parseQuietly(parser, completeBuf);

    capture.assertErrorCount(0);
    capture.assertHasFrame(OpCode.TEXT, 5);
    capture.assertHasFrame(OpCode.CLOSE, 1);
    capture.assertHasFrame(OpCode.PING, 2);
  }