WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/io/mola/galimatias/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,38 @@ public int defaultPort() {
return defaultPort(scheme);
}

public Object[] unSerializedOrigin() {
Object[] origin = new Object[3];
String schemeValue = this.scheme();
String hostValue = (host == null ? "" : host.toString());
Integer portValue = null;

int currentPort = this.port();

if (
!("http".equals(schemeValue) && currentPort == 80) &&
!("https".equals(schemeValue) && currentPort == 443) &&
-1 < currentPort
) {
portValue = currentPort;
}

origin[0] = schemeValue;
origin[1] = hostValue;
origin[2] = portValue;

return origin;
}

public String origin() {
Object[] originValue = this.unSerializedOrigin();
String schemeValue = originValue[0].toString() + "://";
String hostValue =originValue[1].toString();
String portValue = originValue[2] == null ? "" : ":" + originValue[2].toString();

return schemeValue + hostValue + portValue;
}

public String path() {
return path;
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/io/mola/galimatias/URL2Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public void testOmittedHost() throws Exception {
assertEquals("path", url.host().toString()); // Android will parse this as ""
assertEquals("/", url.file()); // Android will parse this as "/path"
assertEquals("/", url.path());
assertEquals("http://path", url.origin());
}

public void testNoHost() throws Exception {
Expand All @@ -112,13 +113,15 @@ public void testNoHost() throws Exception {
assertEquals("/", url.path()); // Android will be "/path"
assertEquals(null, url.query());
assertEquals(null, url.fragment());
assertEquals("http://path", url.origin());
}

public void testNoPath() throws Exception {
URL url = URL.parse("http://host");
assertEquals("host", url.host().toString());
assertEquals("/", url.file()); // Android will be ""
assertEquals("/", url.path()); // Android will be ""
assertEquals("http://host", url.origin());
}

public void testEmptyHostAndNoPath() throws Exception {
Expand Down Expand Up @@ -156,6 +159,7 @@ public void testUserNoPassword() throws Exception {
assertEquals("user@host", url.authority());
assertEquals("user", url.userInfo());
assertEquals("host", url.host().toString());
assertEquals("http://host", url.origin());
}

public void testUserNoPasswordExplicitPort() throws Exception {
Expand All @@ -164,6 +168,7 @@ public void testUserNoPasswordExplicitPort() throws Exception {
assertEquals("user", url.userInfo());
assertEquals("host", url.host().toString());
assertEquals(8080, url.port());
assertEquals("http://host:8080", url.origin());
}

public void testUserPasswordHostPort() throws Exception {
Expand All @@ -172,6 +177,7 @@ public void testUserPasswordHostPort() throws Exception {
assertEquals("user:password", url.userInfo());
assertEquals("host", url.host().toString());
assertEquals(8080, url.port());
assertEquals("http://host:8080", url.origin());
}

public void testUserPasswordEmptyHostPort() throws Exception {
Expand All @@ -192,61 +198,70 @@ public void testPathOnly() throws Exception {
URL url = URL.parse("http://host/path");
assertEquals("/path", url.file());
assertEquals("/path", url.path());
assertEquals("http://host", url.origin());
}

public void testQueryOnly() throws Exception {
URL url = URL.parse("http://host?query");
assertEquals("/?query", url.file());
assertEquals("/", url.path());
assertEquals("query", url.query());
assertEquals("http://host", url.origin());
}

public void testFragmentOnly() throws Exception {
URL url = URL.parse("http://host#fragment");
assertEquals("/", url.file());
assertEquals("/", url.path());
assertEquals("fragment", url.fragment());
assertEquals("http://host", url.origin());
}

public void testAtSignInPath() throws Exception {
URL url = URL.parse("http://host/file@foo");
assertEquals("/file@foo", url.file());
assertEquals("/file@foo", url.path());
assertEquals("", url.userInfo()); // Null on Android
assertEquals("http://host", url.origin());
}

public void testColonInPath() throws Exception {
URL url = URL.parse("http://host/file:colon");
assertEquals("/file:colon", url.file());
assertEquals("/file:colon", url.path());
assertEquals("http://host", url.origin());
}

public void testSlashInQuery() throws Exception {
URL url = URL.parse("http://host/file?query/path");
assertEquals("/file?query/path", url.file());
assertEquals("/file", url.path());
assertEquals("query/path", url.query());
assertEquals("http://host", url.origin());
}

public void testQuestionMarkInQuery() throws Exception {
URL url = URL.parse("http://host/file?query?another");
assertEquals("/file?query?another", url.file());
assertEquals("/file", url.path());
assertEquals("query?another", url.query());
assertEquals("http://host", url.origin());
}

public void testAtSignInQuery() throws Exception {
URL url = URL.parse("http://host/file?query@at");
assertEquals("/file?query@at", url.file());
assertEquals("/file", url.path());
assertEquals("query@at", url.query());
assertEquals("http://host", url.origin());
}

public void testColonInQuery() throws Exception {
URL url = URL.parse("http://host/file?query:colon");
assertEquals("/file?query:colon", url.file());
assertEquals("/file", url.path());
assertEquals("query:colon", url.query());
assertEquals("http://host", url.origin());
}

public void testQuestionMarkInFragment() throws Exception {
Expand All @@ -255,6 +270,7 @@ public void testQuestionMarkInFragment() throws Exception {
assertEquals("/file", url.path());
assertEquals(null, url.query());
assertEquals("fragment?query", url.fragment());
assertEquals("http://host", url.origin());
}

public void testColonInFragment() throws Exception {
Expand All @@ -263,13 +279,15 @@ public void testColonInFragment() throws Exception {
assertEquals("/file", url.path());
assertEquals(80, url.port());
assertEquals("fragment:80", url.fragment());
assertEquals("http://host", url.origin());
}

public void testSlashInFragment() throws Exception {
URL url = URL.parse("http://host/file#fragment/path");
assertEquals("/file", url.file());
assertEquals("/file", url.path());
assertEquals("fragment/path", url.fragment());
assertEquals("http://host", url.origin());
}

/* TODO
Expand All @@ -286,11 +304,13 @@ public void testHashInFragment() throws Exception {
assertEquals("/file", url.file());
assertEquals("/file", url.path());
assertEquals("fragment#another", url.fragment());
assertEquals("http://host", url.origin());
}

public void testEmptyPort() throws Exception {
URL url = URL.parse("http://host:/");
assertEquals(80, url.port());
assertEquals("http://host", url.origin());
}

public void testNonNumericPort() throws Exception {
Expand Down Expand Up @@ -325,6 +345,7 @@ public void testRelativePathOnQuery() throws Exception {
assertEquals("/another", url.path());
assertEquals(null, url.query());
assertEquals(null, url.fragment());
assertEquals("http://host", url.origin());
}

public void testRelativeFragmentOnQuery() throws Exception {
Expand All @@ -335,6 +356,7 @@ public void testRelativeFragmentOnQuery() throws Exception {
assertEquals("/file", url.path());
assertEquals("query/x", url.query());
assertEquals("another", url.fragment());
assertEquals("http://host", url.origin());
}

public void testPathContainsRelativeParts() throws Exception {
Expand Down Expand Up @@ -501,6 +523,7 @@ public void testSquareBracketsInUserInfo() throws Exception {
URL url = URL.parse("http://user:[::1]@host");
assertEquals("user:[::1]", url.userInfo());
assertEquals("host", url.host().toString());
assertEquals("http://host", url.origin());
}

/*TODO
Expand Down Expand Up @@ -529,13 +552,15 @@ public void testFileUrlExtraLeadingSlashes() throws Exception {
assertEquals(null, url.authority()); // RI and galimatias return null, Android returns ""
assertEquals("//foo", url.path());
assertEquals("file:////foo", url.toString());
assertEquals("file://", url.origin());
}

public void testFileUrlWithAuthority() throws Exception {
URL url = URL.parse("file://x/foo");
assertEquals("x", url.authority());
assertEquals("/foo", url.path());
assertEquals("file://x/foo", url.toString());
assertEquals("file://x", url.origin());
}

/**
Expand All @@ -548,13 +573,25 @@ public void testEmptyAuthority() throws Exception {
assertEquals("foo", url.authority()); // Android will be ""
assertEquals("/", url.path()); // Android will be "/foo"
assertEquals("http://foo/", url.toString()); // RI drops '//', android will be "http:///foo"
assertEquals("http://foo", url.origin()); // Android returns "http:///foo:80"
}

public void testHttpUrlExtraLeadingSlashes() throws Exception {
URL url = URL.parse("http:////foo");
assertEquals("foo", url.authority()); // RI returns null, Android "//"
assertEquals("/", url.path()); // Android returns "//foo"
assertEquals("http://foo/", url.toString()); // Android returns "http:////foo"
assertEquals("http://foo", url.origin()); // Android returns "http:////foo:80"
}

public void testHttpsUrl() throws Exception {
URL url = URL.parse("https://foo/path");
assertEquals("https://foo", url.origin());
}

public void testHttpsUrlWithPort() throws Exception {
URL url = URL.parse("https://foo:4000/path");
assertEquals("https://foo:4000", url.origin());
}

public void testFileUrlRelativePath() throws Exception {
Expand All @@ -566,6 +603,7 @@ public void testFileUrlDottedPath() throws Exception {
URL url = URL.parse("file:../a/b");
assertEquals("/a/b", url.path()); // Android will be "../a/b"
assertEquals("file:///a/b", url.toString()); // Android will be "file:../a/b"
assertEquals("file://", url.origin());
}

public void testParsingDotAsHostname() throws Exception {
Expand Down Expand Up @@ -644,6 +682,7 @@ public void testEmptyAuthorityWithPath() throws Exception {
URL url = URL.parse("http:///path");
assertEquals("path", url.authority()); // Android will be ""
assertEquals("/", url.path()); // Android will be "/path"
assertEquals("http://path", url.origin());
}

public void testEmptyAuthorityWithQuery() throws Exception {
Expand Down Expand Up @@ -707,6 +746,7 @@ public void testUnderscore() throws Exception {
assertEquals("a_b.c.d.net", url.authority());
// The RFC's don't permit underscores in hostnames, but URL accepts them (unlike URI).
assertEquals("a_b.c.d.net", url.host().toString());
assertEquals("http://a_b.c.d.net", url.origin());
}

/*TODO
Expand Down