Skip to content

Commit

Permalink
Use appropriate schema in any case TooTallNate#1026
Browse files Browse the repository at this point in the history
add unit test for schema check
  • Loading branch information
yindex authored and yindex committed May 23, 2020
1 parent b56ad28 commit af28db4
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:https://localhost:80",
"http:https://localhost:81",
"http:https://localhost",
"https://localhost:443",
"https://localhost:444",
"https://localhost",
"any:https://localhost",
"any:https://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:https://localhost",
"ws:https://localhost:80",
"ws:https://localhost:81",
"wss:https://localhost",
"wss:https://localhost:443",
"wss:https://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 af28db4

Please sign in to comment.