Skip to content

Commit bb2d893

Browse files
committed
Merge branch 'develop' into feature/site-settings-review
2 parents 619e359 + 47cc553 commit bb2d893

File tree

3 files changed

+73
-8
lines changed

3 files changed

+73
-8
lines changed

WordPressUtils/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ dependencies {
2020
exclude group: 'commons-logging'
2121
}
2222
compile 'com.mcxiaoke.volley:library:1.0.18'
23-
compile 'com.android.support:support-v13:23.0.0'
23+
compile 'com.android.support:support-v13:23.0.1'
2424
}
2525

2626
android {
@@ -29,7 +29,7 @@ android {
2929
publishNonDefault true
3030

3131
compileSdkVersion 23
32-
buildToolsVersion '23.0.0'
32+
buildToolsVersion '23.0.1'
3333

3434
defaultConfig {
3535
versionName "1.5.0"

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

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

33
import android.test.InstrumentationTestCase;
44

5+
import java.util.HashMap;
6+
import java.util.Map;
7+
58
public class UrlUtilsTest extends InstrumentationTestCase {
69
public void testGetDomainFromUrlWithEmptyStringDoesNotReturnNull() {
710
assertNotNull(UrlUtils.getDomainFromUrl(""));
@@ -17,4 +20,55 @@ public void testGetDomainFromUrlWithHostReturnsHost() {
1720

1821
assertTrue(host.equals("www.wordpress.com"));
1922
}
23+
24+
public void testAppendUrlParameter1() {
25+
String url = UrlUtils.appendUrlParameter("http://wp.com/test", "preview", "true");
26+
assertEquals("http://wp.com/test?preview=true", url);
27+
}
28+
29+
public void testAppendUrlParameter2() {
30+
String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony", "preview", "true");
31+
assertEquals("http://wp.com/test?q=pony&preview=true", url);
32+
}
33+
34+
public void testAppendUrlParameter3() {
35+
String url = UrlUtils.appendUrlParameter("http://wp.com/test?q=pony#unicorn", "preview", "true");
36+
assertEquals("http://wp.com/test?q=pony&preview=true#unicorn", url);
37+
}
38+
39+
public void testAppendUrlParameter4() {
40+
String url = UrlUtils.appendUrlParameter("/relative/test", "preview", "true");
41+
assertEquals("/relative/test?preview=true", url);
42+
}
43+
44+
public void testAppendUrlParameter5() {
45+
String url = UrlUtils.appendUrlParameter("/relative/", "preview", "true");
46+
assertEquals("/relative/?preview=true", url);
47+
}
48+
49+
public void testAppendUrlParameter6() {
50+
String url = UrlUtils.appendUrlParameter("http://wp.com/test/", "preview", "true");
51+
assertEquals("http://wp.com/test/?preview=true", url);
52+
}
53+
54+
public void testAppendUrlParameter7() {
55+
String url = UrlUtils.appendUrlParameter("http://wp.com/test/?q=pony", "preview", "true");
56+
assertEquals("http://wp.com/test/?q=pony&preview=true", url);
57+
}
58+
59+
public void testAppendUrlParameters1() {
60+
Map<String, String> params = new HashMap<>();
61+
params.put("w", "200");
62+
params.put("h", "300");
63+
String url = UrlUtils.appendUrlParameters("http://wp.com/test", params);
64+
assertEquals("http://wp.com/test?h=300&w=200", url);
65+
}
66+
67+
public void testAppendUrlParameters2() {
68+
Map<String, String> params = new HashMap<>();
69+
params.put("h", "300");
70+
params.put("w", "200");
71+
String url = UrlUtils.appendUrlParameters("/relative/test", params);
72+
assertEquals("/relative/test?h=300&w=200", url);
73+
}
2074
}

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

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@
1010
import java.io.UnsupportedEncodingException;
1111
import java.net.IDN;
1212
import java.net.URI;
13-
import java.net.URL;
1413
import java.net.URLDecoder;
1514
import java.net.URLEncoder;
1615
import java.nio.charset.Charset;
16+
import java.util.HashMap;
17+
import java.util.Map;
1718

1819
public class UrlUtils {
1920
public static String urlEncode(final String text) {
@@ -144,11 +145,7 @@ public static String removeQuery(final String urlString) {
144145
if (urlString == null) {
145146
return null;
146147
}
147-
int pos = urlString.indexOf("?");
148-
if (pos == -1) {
149-
return urlString;
150-
}
151-
return urlString.substring(0, pos);
148+
return Uri.parse(urlString).buildUpon().clearQuery().toString();
152149
}
153150

154151
/**
@@ -214,4 +211,18 @@ public static boolean isImageUrl(String url) {
214211
return cleanedUrl.endsWith("jpg") || cleanedUrl.endsWith("jpeg") ||
215212
cleanedUrl.endsWith("gif") || cleanedUrl.endsWith("png");
216213
}
214+
215+
public static String appendUrlParameter(String url, String paramName, String paramValue) {
216+
Map<String, String> parameters = new HashMap<>();
217+
parameters.put(paramName, paramValue);
218+
return appendUrlParameters(url, parameters);
219+
}
220+
221+
public static String appendUrlParameters(String url, Map<String, String> parameters) {
222+
Uri.Builder uriBuilder = Uri.parse(url).buildUpon();
223+
for (Map.Entry<String, String> parameter : parameters.entrySet()) {
224+
uriBuilder.appendQueryParameter(parameter.getKey(), parameter.getValue());
225+
}
226+
return uriBuilder.build().toString();
227+
}
217228
}

0 commit comments

Comments
 (0)