|
4 | 4 | import org.junit.runner.RunWith;
|
5 | 5 | import org.junit.runners.JUnit4;
|
6 | 6 |
|
7 |
| -import java.net.MalformedURLException; |
8 |
| -import java.net.URISyntaxException; |
9 |
| -import java.net.URL; |
| 7 | +import java.net.URI; |
10 | 8 |
|
11 | 9 | import static org.hamcrest.CoreMatchers.is;
|
12 | 10 | import static org.hamcrest.CoreMatchers.not;
|
|
15 | 13 | @RunWith(JUnit4.class)
|
16 | 14 | public class UrlTest {
|
17 | 15 |
|
| 16 | + private URI parse(String uri) { |
| 17 | + return Url.parse(URI.create(uri)).uri; |
| 18 | + } |
| 19 | + |
| 20 | + private String extractId(String uri) { |
| 21 | + return Url.parse(URI.create(uri)).id; |
| 22 | + } |
| 23 | + |
18 | 24 | @Test
|
19 |
| - public void parse() throws URISyntaxException { |
20 |
| - assertThat(Url.parse("http://username:password@host:8080/directory/file?query#ref").toString(), |
| 25 | + public void parse() { |
| 26 | + assertThat(parse("http://username:password@host:8080/directory/file?query#ref").toString(), |
21 | 27 | is("http://username:password@host:8080/directory/file?query#ref"));
|
22 | 28 | }
|
23 | 29 |
|
24 | 30 | @Test
|
25 |
| - public void parseRelativePath() throws URISyntaxException { |
26 |
| - URL url = Url.parse("https://woot.com/test"); |
27 |
| - assertThat(url.getProtocol(), is("https")); |
28 |
| - assertThat(url.getHost(), is("woot.com")); |
29 |
| - assertThat(url.getPath(), is("/test")); |
| 31 | + public void parseRelativePath() { |
| 32 | + URI uri = parse("https://woot.com/test"); |
| 33 | + assertThat(uri.getScheme(), is("https")); |
| 34 | + assertThat(uri.getHost(), is("woot.com")); |
| 35 | + assertThat(uri.getPath(), is("/test")); |
30 | 36 | }
|
31 | 37 |
|
32 | 38 | @Test
|
33 |
| - public void parseNoProtocol() throws URISyntaxException { |
34 |
| - URL url = Url.parse("//localhost:3000"); |
35 |
| - assertThat(url.getProtocol(), is("https")); |
36 |
| - assertThat(url.getHost(), is("localhost")); |
37 |
| - assertThat(url.getPort(), is(3000)); |
| 39 | + public void parseNoProtocol() { |
| 40 | + URI uri = parse("//localhost:3000"); |
| 41 | + assertThat(uri.getScheme(), is("https")); |
| 42 | + assertThat(uri.getHost(), is("localhost")); |
| 43 | + assertThat(uri.getPort(), is(3000)); |
38 | 44 | }
|
39 | 45 |
|
40 | 46 | @Test
|
41 |
| - public void parseNamespace() throws URISyntaxException { |
42 |
| - assertThat(Url.parse("http://woot.com/woot").getPath(), is("/woot")); |
43 |
| - assertThat(Url.parse("http://google.com").getPath(), is("/")); |
44 |
| - assertThat(Url.parse("http://google.com/").getPath(), is("/")); |
| 47 | + public void parseNamespace() { |
| 48 | + assertThat(parse("http://woot.com/woot").getPath(), is("/woot")); |
| 49 | + assertThat(parse("http://google.com").getPath(), is("/")); |
| 50 | + assertThat(parse("http://google.com/").getPath(), is("/")); |
45 | 51 | }
|
46 | 52 |
|
47 | 53 | @Test
|
48 |
| - public void parseDefaultPort() throws URISyntaxException { |
49 |
| - assertThat(Url.parse("http://google.com/").toString(), is("http://google.com:80/")); |
50 |
| - assertThat(Url.parse("https://google.com/").toString(), is("https://google.com:443/")); |
| 54 | + public void parseDefaultPort() { |
| 55 | + assertThat(parse("http://google.com/").toString(), is("http://google.com:80/")); |
| 56 | + assertThat(parse("https://google.com/").toString(), is("https://google.com:443/")); |
51 | 57 | }
|
52 | 58 |
|
53 | 59 | @Test
|
54 |
| - public void extractId() throws MalformedURLException { |
55 |
| - String id1 = Url.extractId("http://google.com:80/"); |
56 |
| - String id2 = Url.extractId("http://google.com/"); |
57 |
| - String id3 = Url.extractId("https://google.com/"); |
| 60 | + public void testWsProtocol() { |
| 61 | + URI uri = parse("ws://woot.com/test"); |
| 62 | + assertThat(uri.getScheme(), is("ws")); |
| 63 | + assertThat(uri.getHost(), is("woot.com")); |
| 64 | + assertThat(uri.getPort(), is(80)); |
| 65 | + assertThat(uri.getPath(), is("/test")); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testWssProtocol() { |
| 70 | + URI uri = parse("wss://woot.com/test"); |
| 71 | + assertThat(uri.getScheme(), is("wss")); |
| 72 | + assertThat(uri.getHost(), is("woot.com")); |
| 73 | + assertThat(uri.getPort(), is(443)); |
| 74 | + assertThat(uri.getPath(), is("/test")); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void extractId() { |
| 79 | + String id1 = extractId("http://google.com:80/"); |
| 80 | + String id2 = extractId("http://google.com/"); |
| 81 | + String id3 = extractId("https://google.com/"); |
58 | 82 | assertThat(id1, is(id2));
|
59 | 83 | assertThat(id1, is(not(id3)));
|
60 | 84 | assertThat(id2, is(not(id3)));
|
61 | 85 | }
|
62 | 86 |
|
63 | 87 | @Test
|
64 |
| - public void ipv6() throws URISyntaxException, MalformedURLException { |
| 88 | + public void ipv6() { |
65 | 89 | String url = "http://[::1]";
|
66 |
| - URL parsed = Url.parse(url); |
67 |
| - assertThat(parsed.getProtocol(), is("http")); |
| 90 | + URI parsed = parse(url); |
| 91 | + assertThat(parsed.getScheme(), is("http")); |
68 | 92 | assertThat(parsed.getHost(), is("[::1]"));
|
69 | 93 | assertThat(parsed.getPort(), is(80));
|
70 |
| - assertThat(Url.extractId(url), is("http://[::1]:80")); |
| 94 | + assertThat(extractId(url), is("http://[::1]:80")); |
71 | 95 | }
|
| 96 | + |
72 | 97 | }
|
0 commit comments