Skip to content

Commit 124d3a9

Browse files
committed
Merge branch 'develop' of https://github.com/wordpress-mobile/WordPress-Android into feature/viewpager-main-activity
2 parents 152983e + c47d51e commit 124d3a9

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.wordpress.android.util;
2+
3+
import android.test.InstrumentationTestCase;
4+
5+
public class UrlUtilsTest extends InstrumentationTestCase {
6+
public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() {
7+
assertNotNull(UrlUtils.getDomainFromUrl(""));
8+
}
9+
10+
public void testGetDomainFromUrlWithNoHostDoesNotReturnNull() {
11+
assertNotNull(UrlUtils.getDomainFromUrl("wordpress"));
12+
}
13+
14+
public void testGetDomainFromUrlWithHostReturnsHost() {
15+
String url = "http://www.wordpress.com";
16+
String host = UrlUtils.getDomainFromUrl(url);
17+
18+
assertTrue(host.equals("www.wordpress.com"));
19+
}
20+
}

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,20 @@ public static String urlDecode(final String text) {
3232
}
3333
}
3434

35+
/**
36+
*
37+
* @param urlString url to get domain from
38+
* @return domain of uri if available. Empty string otherwise.
39+
*/
3540
public static String getDomainFromUrl(final String urlString) {
36-
if (urlString == null) {
37-
return "";
41+
if (urlString != null) {
42+
Uri uri = Uri.parse(urlString);
43+
if (uri.getHost() != null) {
44+
return uri.getHost();
45+
}
3846
}
39-
Uri uri = Uri.parse(urlString);
40-
return uri.getHost();
47+
48+
return "";
4149
}
4250

4351
/**

0 commit comments

Comments
 (0)