Skip to content

Commit 0cdc639

Browse files
committed
Add utility methods to check if a URL is wpcom.
Add utility methods that check if it's safe to add the Authentication token to the request.
1 parent 5055b8a commit 0cdc639

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

WordPressUtils/src/androidTest/java/org/wordpress/android/util/UrlUtilsTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import android.test.InstrumentationTestCase;
44

5+
import java.net.MalformedURLException;
6+
import java.net.URL;
57
import java.util.HashMap;
68
import java.util.Map;
79

@@ -75,4 +77,32 @@ public void testAppendUrlParameters2() {
7577
assertTrue("failed test on url: " + url, false);
7678
}
7779
}
80+
81+
public void testHttps1() {
82+
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
83+
}
84+
85+
public void testHttps2() {
86+
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com#.b.com/test")));
87+
}
88+
89+
public void testHttps3() {
90+
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
91+
}
92+
93+
public void testHttps4() {
94+
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com")));
95+
}
96+
97+
public void testHttps5() {
98+
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com/test#test")));
99+
}
100+
101+
private URL buildURL(String address) {
102+
URL url = null;
103+
try {
104+
url = new URL(address);
105+
} catch (MalformedURLException e) {}
106+
return url;
107+
}
78108
}

WordPressUtils/src/main/java/org/wordpress/android/util/UrlUtils.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.io.UnsupportedEncodingException;
1111
import java.net.IDN;
1212
import java.net.URI;
13+
import java.net.URL;
1314
import java.net.URLDecoder;
1415
import java.net.URLEncoder;
1516
import java.nio.charset.Charset;
@@ -172,6 +173,17 @@ public static boolean isHttps(final String urlString) {
172173
return (urlString != null && urlString.startsWith("https:"));
173174
}
174175

176+
public static boolean isHttps(URL url) {
177+
return url != null && "https".equals(url.getProtocol());
178+
}
179+
180+
public static boolean isHttps(URI uri) {
181+
if (uri == null) return false;
182+
183+
String protocol = uri.getScheme();
184+
return protocol != null && protocol.equals("https");
185+
}
186+
175187
/**
176188
* returns https: version of passed http: url
177189
*/

0 commit comments

Comments
 (0)