diff --git a/src/test/java/org/java_websocket/framing/CloseFrameTest.java b/src/test/java/org/java_websocket/framing/CloseFrameTest.java index 3ddd9fa6c..7d2964b02 100644 --- a/src/test/java/org/java_websocket/framing/CloseFrameTest.java +++ b/src/test/java/org/java_websocket/framing/CloseFrameTest.java @@ -28,9 +28,9 @@ import org.java_websocket.enums.Opcode; import org.java_websocket.exceptions.InvalidDataException; import org.junit.Test; +import java.nio.ByteBuffer; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; /** * JUnit Test for the CloseFrame class @@ -241,5 +241,90 @@ public void testIsValid() { } catch (InvalidDataException e) { //fine } + + int test_undefine_code = 2500; + frame.setCode(test_undefine_code); + frame.setReason("Trying to send an illegal close code"); + try { + frame.isValid(); + fail("InvalidDataException should be thrown"); + } catch (InvalidDataException e) { + //fine + } + + test_undefine_code = 5000; + frame.setCode(test_undefine_code); + frame.setReason("Close code must not be send over the wire"); + try { + frame.isValid(); + fail("InvalidDataException should be thrown"); + } catch (InvalidDataException e) { + //fine + } + + test_undefine_code = 1004; + frame.setCode(test_undefine_code); + frame.setReason("Close code must not be send over the wire"); + try { + frame.isValid(); + fail("InvalidDataException should be thrown"); + } catch (InvalidDataException e) { + //fine + } + } + + @Test + public void testEquals() { + CloseFrame frame = new CloseFrame(); + + assertFalse(frame.equals(null)); + assertFalse(frame.equals(new Object())); + + CloseFrame frame1 = frame; + assertTrue(frame.equals(frame1)); + + frame1 = new CloseFrame(); + frame1.setCode(CloseFrame.GOING_AWAY); + assertFalse(frame.equals(frame1)); + + frame1.setCode(CloseFrame.NORMAL); + assertTrue(frame.equals(frame1)); + + frame1.setReason("test"); + assertFalse(frame.equals(frame1)); + + frame1.setReason(""); + assertTrue(frame.equals(frame1)); + + frame1.setFin(false); + assertFalse(frame.equals(frame1)); + + frame1.setFin(true); + frame1.setRSV1(true); + assertFalse(frame.equals(frame1)); + + frame1.setRSV1(false); + frame1.setRSV2(true); + assertFalse(frame.equals(frame1)); + + frame1.setRSV2(false); + frame1.setRSV3(true); + assertFalse(frame.equals(frame1)); + + frame1.setRSV3(false); + assertTrue(frame.equals(frame1)); + } + + @Test + public void testSetPayload() { + CloseFrame frame = new CloseFrame(); + frame.setPayload(ByteBuffer.wrap("".getBytes())); + assertEquals("Must be CloseFrame.NORMAL", CloseFrame.NORMAL, frame.getCloseCode()); + + frame.setPayload(ByteBuffer.wrap("i".getBytes())); + assertEquals("Must be CloseFrame.PROTOCOL_ERROR", CloseFrame.PROTOCOL_ERROR, frame.getCloseCode()); + + frame.setPayload(ByteBuffer.wrap("io".getBytes())); + assertEquals("Uncertain values, if more than two character strings are transferred", 26991, frame.getCloseCode()); } } diff --git a/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java b/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java index fbfaa256e..75ba7a51e 100644 --- a/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java +++ b/src/test/java/org/java_websocket/framing/FramedataImpl1Test.java @@ -102,4 +102,32 @@ public void testAppend() { assertEquals("Fin must be set", true, frame0.isFin()); assertArrayEquals("Payload must be equal", "firstsecond".getBytes(), frame0.getPayloadData().array()); } + + @Test + public void testAppend1() { + FramedataImpl1 frame0 = FramedataImpl1.get(Opcode.BINARY); + frame0.setFin(false); + frame0.setPayload(null); + FramedataImpl1 frame1 = FramedataImpl1.get(Opcode.BINARY); + frame1.setPayload(ByteBuffer.wrap("second".getBytes())); + frame0.append(frame1); + assertEquals("Fin must be set", true, frame0.isFin()); + assertArrayEquals("Payload must be equal", "second".getBytes(), frame0.getPayloadData().array()); + } + + @Test + public void testToString() { + FramedataImpl1 frame = FramedataImpl1.get(Opcode.BINARY); + frame.setPayload(ByteBuffer.wrap("hello".getBytes())); + String excepted = "Framedata{ opcode:BINARY, fin:true, rsv1:false, rsv2:false, rsv3:false, payload length:[pos:0, len:5], payload:hello}"; + assertEquals(excepted, frame.toString()); + + excepted = "Framedata{ opcode:BINARY, fin:true, rsv1:false, rsv2:false, rsv3:false, payload length:[pos:0, len:1001], payload:(too big to display)}"; + StringBuilder rec = new StringBuilder(); + for (int i = 0; i < 1001; i++) { + rec.append("a"); + } + frame.setPayload(ByteBuffer.wrap(rec.toString().getBytes())); + assertEquals(excepted, frame.toString()); + } }