Skip to content

Commit

Permalink
Use appropriate schema in any case #1026
Browse files Browse the repository at this point in the history
add unit test for schema check
  • Loading branch information
yindex authored and 宰祥顺 committed May 23, 2020
1 parent 433935a commit 38a7aed
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
17 changes: 7 additions & 10 deletions src/main/java/org/java_websocket/client/WebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,17 +534,14 @@ protected void onSetSSLParameters(SSLParameters sslParameters) {
*/
private int getPort() {
int port = uri.getPort();
if( port == -1 ) {
String scheme = uri.getScheme();
if( "wss".equals( scheme ) ) {
return WebSocketImpl.DEFAULT_WSS_PORT;
} else if( "ws".equals( scheme ) ) {
return WebSocketImpl.DEFAULT_PORT;
} else {
throw new IllegalArgumentException( "unknown scheme: " + scheme );
}
String scheme = uri.getScheme();
if( "wss".equals( scheme ) ) {
return port == -1 ? WebSocketImpl.DEFAULT_WSS_PORT : port;
} else if( "ws".equals( scheme ) ) {
return port == -1 ? WebSocketImpl.DEFAULT_PORT : port;
} else {
throw new IllegalArgumentException( "unknown scheme: " + scheme );
}
return port;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/org/java_websocket/client/AllClientTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@

@RunWith(Suite.class)
@Suite.SuiteClasses({
org.java_websocket.client.AttachmentTest.class
org.java_websocket.client.AttachmentTest.class,
org.java_websocket.client.SchemaCheckTest.class
})
/**
* Start all tests for the client
Expand Down
86 changes: 86 additions & 0 deletions src/test/java/org/java_websocket/client/SchemaCheckTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package org.java_websocket.client;

import org.java_websocket.handshake.ServerHandshake;
import org.junit.Test;

import java.net.URI;
import java.net.URISyntaxException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class SchemaCheckTest {

@Test
public void testSchemaCheck() throws URISyntaxException {
final String []invalidCase = {
"http://localhost:80",
"http://localhost:81",
"http://localhost",
"https://localhost:443",
"https://localhost:444",
"https://localhost",
"any://localhost",
"any://localhost:82",
};
final Exception[] exs = new Exception[invalidCase.length];
for (int i = 0; i < invalidCase.length; i++) {
final int finalI = i;
new WebSocketClient(new URI(invalidCase[finalI])) {
@Override
public void onOpen(ServerHandshake handshakedata) {

}

@Override
public void onMessage(String message) {

}

@Override
public void onClose(int code, String reason, boolean remote) {

}

@Override
public void onError(Exception ex) {
exs[finalI] = ex;
}
}.run();
}
for (Exception exception : exs) {
assertTrue(exception instanceof IllegalArgumentException);
}
final String []validCase = {
"ws://localhost",
"ws://localhost:80",
"ws://localhost:81",
"wss://localhost",
"wss://localhost:443",
"wss://localhost:444"
};
for (String s : validCase) {
new WebSocketClient(new URI(s)) {
@Override
public void onOpen(ServerHandshake handshakedata) {

}

@Override
public void onMessage(String message) {

}

@Override
public void onClose(int code, String reason, boolean remote) {

}

@Override
public void onError(Exception ex) {
assertFalse(ex instanceof IllegalArgumentException);
}
}.run();
}
}
}

0 comments on commit 38a7aed

Please sign in to comment.