Skip to content

Commit 6eb1dde

Browse files
committed
Add several testcases to improve the code coverage.
1 parent 866ec85 commit 6eb1dde

File tree

4 files changed

+170
-1
lines changed

4 files changed

+170
-1
lines changed

‎src/test/java/org/java_websocket/AllTests.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
@RunWith(Suite.class)
3333
@Suite.SuiteClasses({
3434
org.java_websocket.util.ByteBufferUtilsTest.class,
35+
org.java_websocket.util.Base64Test.class,
3536
org.java_websocket.client.AllClientTests.class,
3637
org.java_websocket.drafts.AllDraftTests.class,
3738
org.java_websocket.issues.AllIssueTests.class,

‎src/test/java/org/java_websocket/client/AllClientTests.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@
3232
@RunWith(Suite.class)
3333
@Suite.SuiteClasses({
3434
org.java_websocket.client.AttachmentTest.class,
35-
org.java_websocket.client.SchemaCheckTest.class
35+
org.java_websocket.client.SchemaCheckTest.class,
36+
org.java_websocket.client.HeadersTest.class
3637
})
3738
/**
3839
* Start all tests for the client
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright (c) 2010-2020 Nathan Rajlich
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use,
8+
* copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the
10+
* Software is furnished to do so, subject to the following
11+
* conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be
14+
* included in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
* OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
26+
packageorg.java_websocket.client;
27+
28+
importorg.java_websocket.handshake.ServerHandshake;
29+
importorg.junit.Test;
30+
31+
importjava.net.URI;
32+
importjava.net.URISyntaxException;
33+
importjava.util.HashMap;
34+
importjava.util.Map;
35+
36+
importstaticorg.junit.Assert.assertEquals;
37+
importstaticorg.junit.Assert.assertNull;
38+
39+
publicclassHeadersTest{
40+
41+
@Test
42+
publicvoidtestHttpHeaders() throwsURISyntaxException{
43+
Map<String, String> httpHeaders = newHashMap<String, String>();
44+
httpHeaders.put("Cache-Control", "only-if-cached");
45+
httpHeaders.put("Keep-Alive", "1000");
46+
47+
WebSocketClientclient = newWebSocketClient(newURI( "ws://localhost"), httpHeaders){
48+
@Override
49+
publicvoidonOpen( ServerHandshakehandshakedata ){
50+
51+
}
52+
53+
@Override
54+
publicvoidonMessage( Stringmessage ){
55+
56+
}
57+
58+
@Override
59+
publicvoidonClose( intcode, Stringreason, booleanremote ){
60+
61+
}
62+
63+
@Override
64+
publicvoidonError( Exceptionex ){
65+
66+
}
67+
};
68+
69+
assertEquals("only-if-cached", client.removeHeader("Cache-Control"));
70+
assertEquals("1000", client.removeHeader("Keep-Alive"));
71+
}
72+
73+
@Test
74+
publicvoidtest_Add_RemoveHeaders() throwsURISyntaxException{
75+
Map<String, String> httpHeaders = null;
76+
WebSocketClientclient = newWebSocketClient(newURI( "ws://localhost"), httpHeaders){
77+
@Override
78+
publicvoidonOpen( ServerHandshakehandshakedata ){
79+
80+
}
81+
82+
@Override
83+
publicvoidonMessage( Stringmessage ){
84+
85+
}
86+
87+
@Override
88+
publicvoidonClose( intcode, Stringreason, booleanremote ){
89+
90+
}
91+
92+
@Override
93+
publicvoidonError( Exceptionex ){
94+
95+
}
96+
};
97+
client.addHeader("Cache-Control", "only-if-cached");
98+
assertEquals("only-if-cached", client.removeHeader("Cache-Control"));
99+
assertNull(client.removeHeader("Cache-Control"));
100+
101+
client.addHeader("Cache-Control", "only-if-cached");
102+
client.clearHeaders();
103+
assertNull(client.removeHeader("Cache-Control"));
104+
}
105+
106+
@Test
107+
publicvoidtestGetURI() throwsURISyntaxException{
108+
WebSocketClientclient = newWebSocketClient(newURI( "ws://localhost")){
109+
@Override
110+
publicvoidonOpen( ServerHandshakehandshakedata ){
111+
112+
}
113+
114+
@Override
115+
publicvoidonMessage( Stringmessage ){
116+
117+
}
118+
119+
@Override
120+
publicvoidonClose( intcode, Stringreason, booleanremote ){
121+
122+
}
123+
124+
@Override
125+
publicvoidonError( Exceptionex ){
126+
127+
}
128+
};
129+
StringactualURI = client.getURI().getScheme() + "://" + client.getURI().getHost();
130+
131+
assertEquals("ws://localhost", actualURI);
132+
}
133+
}

‎src/test/java/org/java_websocket/util/Base64Test.java‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ public void testEncodeBytes() throws IOException{
4141
Assert.assertEquals("", Base64.encodeBytes(newbyte[0]));
4242
Assert.assertEquals("QHE=",
4343
Base64.encodeBytes(newbyte[]{49, 121, 64, 113, -63, 43, -24, 62, 4, 48}, 2, 2, 0));
44+
Assert.assertEquals("H4sIAAAAAAAAADMEALfv3IMBAAAA",
45+
Base64.encodeBytes(newbyte[]{49, 121, 64, 113, -63, 43, -24, 62, 4, 48}, 0, 1, 6));
46+
Assert.assertEquals("H4sIAAAAAAAAAHMoBABQHKKWAgAAAA==",
47+
Base64.encodeBytes(newbyte[]{49, 121, 64, 113, -63, 43, -24, 62, 4, 48}, 2, 2, 18));
48+
Assert.assertEquals("F63=",
49+
Base64.encodeBytes(newbyte[]{49, 121, 64, 113, 63, 43, -24, 62, 4, 48}, 2, 2, 32));
50+
Assert.assertEquals("6sg7---------6Bc0-0F699L-V----==",
51+
Base64.encodeBytes(newbyte[]{49, 121, 64, 113, 63, 43, -24, 62, 4, 48}, 2, 2, 34));
4452
}
4553

4654
@Test
@@ -90,4 +98,30 @@ public void testEncodeBytesToBytes2() throws IOException{
9098
thrown.expect(IllegalArgumentException.class);
9199
Base64.encodeBytesToBytes(newbyte[]{83, 10, 91, 67, 42, -1, 107, 62, 91, 67}, 8, 6, 26);
92100
}
101+
102+
@Test
103+
publicvoidtestEncodeBytesToBytes3() throwsIOException{
104+
byte[] src = newbyte[]{
105+
113, 42, 123, 99, 10, -33, 75, 30, 88, 99,
106+
113, 42, 123, 99, 10, -33, 75, 31, 88, 99,
107+
113, 42, 123, 99, 10, -33, 75, 32, 88, 99,
108+
113, 42, 123, 99, 10, -33, 75, 33, 88, 99,
109+
113, 42, 123, 99, 10, -33, 75, 34, 88, 99,
110+
113, 42, 123, 99, 10, -33, 75, 35, 88, 99,
111+
55, 60
112+
};
113+
byte[] excepted = newbyte[]{
114+
99, 83, 112, 55, 89, 119, 114, 102, 83, 120,
115+
53, 89, 89, 51, 69, 113, 101, 50, 77, 75, 51,
116+
48, 115, 102, 87, 71, 78, 120, 75, 110, 116, 106,
117+
67, 116, 57, 76, 73, 70, 104, 106, 99, 83, 112,
118+
55, 89, 119, 114, 102, 83, 121, 70, 89, 89,
119+
51, 69, 113, 101, 50, 77, 75, 51, 48, 115,
120+
105, 87, 71, 78, 120, 75, 110, 116, 106, 67,
121+
116, 57, 76, 10, 73, 49, 104, 106, 78, 122,
122+
119, 61
123+
};
124+
125+
Assert.assertArrayEquals(excepted, Base64.encodeBytesToBytes(src, 0, 62, 8));
126+
}
93127
}

0 commit comments

Comments
(0)