Skip to content

Commit

Permalink
Add utility methods to check if a URL is wpcom.
Browse files Browse the repository at this point in the history
Add utility methods  that check if it's safe to add the Authentication token to the request.
  • Loading branch information
daniloercoli committed Dec 21, 2015
1 parent 821c32c commit c21f969
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import android.test.InstrumentationTestCase;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -75,4 +77,32 @@ public void testAppendUrlParameters2() {
assertTrue("failed test on url: " + url, false);
}
}

public void testHttps1() {
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
}

public void testHttps2() {
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com#.b.com/test")));
}

public void testHttps3() {
assertFalse(UrlUtils.isHttps(buildURL("http://wordpress.com/xmlrpc.php")));
}

public void testHttps4() {
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com")));
}

public void testHttps5() {
assertTrue(UrlUtils.isHttps(buildURL("https://wordpress.com/test#test")));
}

private URL buildURL(String address) {
URL url = null;
try {
url = new URL(address);
} catch (MalformedURLException e) {}
return url;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.io.UnsupportedEncodingException;
import java.net.IDN;
import java.net.URI;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -172,6 +173,17 @@ public static boolean isHttps(final String urlString) {
return (urlString != null && urlString.startsWith("https:"));
}

public static boolean isHttps(URL url) {
return url != null && "https".equals(url.getProtocol());
}

public static boolean isHttps(URI uri) {
if (uri == null) return false;

String protocol = uri.getScheme();
return protocol != null && protocol.equals("https");
}

/**
* returns https: version of passed http: url
*/
Expand Down

0 comments on commit c21f969

Please sign in to comment.