Skip to content

Commit a89d4ae

Browse files
authored
Merge pull request #1025 from yindex/master
2 parents 0e31ddd + af28db4 commit a89d4ae

File tree

3 files changed

+95
-11
lines changed

3 files changed

+95
-11
lines changed

src/main/java/org/java_websocket/client/WebSocketClient.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -534,17 +534,14 @@ protected void onSetSSLParameters(SSLParameters sslParameters) {
534534
*/
535535
private int getPort() {
536536
int port = uri.getPort();
537-
if( port == -1 ) {
538-
String scheme = uri.getScheme();
539-
if( "wss".equals( scheme ) ) {
540-
return WebSocketImpl.DEFAULT_WSS_PORT;
541-
} else if( "ws".equals( scheme ) ) {
542-
return WebSocketImpl.DEFAULT_PORT;
543-
} else {
544-
throw new IllegalArgumentException( "unknown scheme: " + scheme );
545-
}
537+
String scheme = uri.getScheme();
538+
if( "wss".equals( scheme ) ) {
539+
return port == -1 ? WebSocketImpl.DEFAULT_WSS_PORT : port;
540+
} else if( "ws".equals( scheme ) ) {
541+
return port == -1 ? WebSocketImpl.DEFAULT_PORT : port;
542+
} else {
543+
throw new IllegalArgumentException( "unknown scheme: " + scheme );
546544
}
547-
return port;
548545
}
549546

550547
/**

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131

3232
@RunWith(Suite.class)
3333
@Suite.SuiteClasses({
34-
org.java_websocket.client.AttachmentTest.class
34+
org.java_websocket.client.AttachmentTest.class,
35+
org.java_websocket.client.SchemaCheckTest.class
3536
})
3637
/**
3738
* Start all tests for the client
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package org.java_websocket.client;
2+
3+
import org.java_websocket.handshake.ServerHandshake;
4+
import org.junit.Test;
5+
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
8+
9+
import static org.junit.Assert.assertFalse;
10+
import static org.junit.Assert.assertTrue;
11+
12+
public class SchemaCheckTest {
13+
14+
@Test
15+
public void testSchemaCheck() throws URISyntaxException {
16+
final String []invalidCase = {
17+
"http://localhost:80",
18+
"http://localhost:81",
19+
"http://localhost",
20+
"https://localhost:443",
21+
"https://localhost:444",
22+
"https://localhost",
23+
"any://localhost",
24+
"any://localhost:82",
25+
};
26+
final Exception[] exs = new Exception[invalidCase.length];
27+
for (int i = 0; i < invalidCase.length; i++) {
28+
final int finalI = i;
29+
new WebSocketClient(new URI(invalidCase[finalI])) {
30+
@Override
31+
public void onOpen(ServerHandshake handshakedata) {
32+
33+
}
34+
35+
@Override
36+
public void onMessage(String message) {
37+
38+
}
39+
40+
@Override
41+
public void onClose(int code, String reason, boolean remote) {
42+
43+
}
44+
45+
@Override
46+
public void onError(Exception ex) {
47+
exs[finalI] = ex;
48+
}
49+
}.run();
50+
}
51+
for (Exception exception : exs) {
52+
assertTrue(exception instanceof IllegalArgumentException);
53+
}
54+
final String []validCase = {
55+
"ws://localhost",
56+
"ws://localhost:80",
57+
"ws://localhost:81",
58+
"wss://localhost",
59+
"wss://localhost:443",
60+
"wss://localhost:444"
61+
};
62+
for (String s : validCase) {
63+
new WebSocketClient(new URI(s)) {
64+
@Override
65+
public void onOpen(ServerHandshake handshakedata) {
66+
67+
}
68+
69+
@Override
70+
public void onMessage(String message) {
71+
72+
}
73+
74+
@Override
75+
public void onClose(int code, String reason, boolean remote) {
76+
77+
}
78+
79+
@Override
80+
public void onError(Exception ex) {
81+
assertFalse(ex instanceof IllegalArgumentException);
82+
}
83+
}.run();
84+
}
85+
}
86+
}

0 commit comments

Comments
 (0)