Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 87 additions & 2 deletions src/test/java/org/java_websocket/framing/CloseFrameTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
}
28 changes: 28 additions & 0 deletions src/test/java/org/java_websocket/framing/FramedataImpl1Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}