fix not to throw URISyntaxException

This commit is contained in:
Naoyuki Kanezawa
2015-06-28 02:00:54 +09:00
parent acd7426a48
commit f8c723289e
3 changed files with 25 additions and 22 deletions

View File

@@ -5,7 +5,6 @@ import com.github.nkzawa.socketio.parser.Parser;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
@@ -42,7 +41,7 @@ public class IO {
return socket(new URI(uri), opts);
}
public static Socket socket(URI uri) throws URISyntaxException {
public static Socket socket(URI uri) {
return socket(uri, null);
}
@@ -54,18 +53,18 @@ public class IO {
* @return {@link Socket} instance.
* @throws URISyntaxException
*/
public static Socket socket(URI uri, Options opts) throws URISyntaxException {
public static Socket socket(URI uri, Options opts) {
if (opts == null) {
opts = new Options();
}
URL parsed;
URL parsed = Url.parse(uri);
URI source;
try {
parsed = Url.parse(uri);
} catch (MalformedURLException e) {
throw new URISyntaxException(uri.toString(), e.getMessage());
source = parsed.toURI();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
URI source = parsed.toURI();
Manager io;
if (opts.forceNew || !opts.multiplex) {

View File

@@ -13,11 +13,11 @@ public class Url {
private Url() {}
public static URL parse(String uri) throws URISyntaxException, MalformedURLException {
public static URL parse(String uri) throws URISyntaxException {
return parse(new URI(uri));
}
public static URL parse(URI uri) throws MalformedURLException {
public static URL parse(URI uri) {
String protocol = uri.getScheme();
if (protocol == null || !protocol.matches("^https?|wss?$")) {
protocol = "https";
@@ -40,13 +40,17 @@ public class Url {
String userInfo = uri.getRawUserInfo();
String query = uri.getRawQuery();
String fragment = uri.getRawFragment();
return new URL(protocol + "://"
+ (userInfo != null ? userInfo + "@" : "")
+ uri.getHost()
+ (port != -1 ? ":" + port : "")
+ path
+ (query != null ? "?" + query : "")
+ (fragment != null ? "#" + fragment : ""));
try {
return new URL(protocol + "://"
+ (userInfo != null ? userInfo + "@" : "")
+ uri.getHost()
+ (port != -1 ? ":" + port : "")
+ path
+ (query != null ? "?" + query : "")
+ (fragment != null ? "#" + fragment : ""));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
}
public static String extractId(String url) throws MalformedURLException {

View File

@@ -16,13 +16,13 @@ import static org.junit.Assert.assertThat;
public class UrlTest {
@Test
public void parse() throws MalformedURLException, URISyntaxException {
public void parse() throws URISyntaxException {
assertThat(Url.parse("http://username:password@host:8080/directory/file?query#ref").toString(),
is("http://username:password@host:8080/directory/file?query#ref"));
}
@Test
public void parseRelativePath() throws MalformedURLException, URISyntaxException {
public void parseRelativePath() throws URISyntaxException {
URL url = Url.parse("https://woot.com/test");
assertThat(url.getProtocol(), is("https"));
assertThat(url.getHost(), is("woot.com"));
@@ -30,7 +30,7 @@ public class UrlTest {
}
@Test
public void parseNoProtocol() throws MalformedURLException, URISyntaxException {
public void parseNoProtocol() throws URISyntaxException {
URL url = Url.parse("//localhost:3000");
assertThat(url.getProtocol(), is("https"));
assertThat(url.getHost(), is("localhost"));
@@ -38,14 +38,14 @@ public class UrlTest {
}
@Test
public void parseNamespace() throws MalformedURLException, URISyntaxException {
public void parseNamespace() throws URISyntaxException {
assertThat(Url.parse("http://woot.com/woot").getPath(), is("/woot"));
assertThat(Url.parse("http://google.com").getPath(), is("/"));
assertThat(Url.parse("http://google.com/").getPath(), is("/"));
}
@Test
public void parseDefaultPort() throws MalformedURLException, URISyntaxException {
public void parseDefaultPort() throws URISyntaxException {
assertThat(Url.parse("http://google.com/").toString(), is("http://google.com:80/"));
assertThat(Url.parse("https://google.com/").toString(), is("https://google.com:443/"));
}