add tests for Util

This commit is contained in:
Naoyuki Kanezawa
2013-04-24 00:51:23 +09:00
parent e3f5e95eea
commit 789f94e11a
4 changed files with 81 additions and 18 deletions

View File

@@ -15,12 +15,8 @@ public class Util {
StringBuilder str = new StringBuilder();
for (Map.Entry<String, String> entry : obj.entrySet()) {
if (str.length() > 0) str.append("&");
try {
str.append(URLEncoder.encode(entry.getKey(), "UTF-8")).append("=")
.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
str.append(encodeURIComponent(entry.getKey())).append("=")
.append(encodeURIComponent(entry.getValue()));
}
return str.toString();
}
@@ -30,14 +26,31 @@ public class Util {
String[] pairs = qs.split("&");
for (String _pair : pairs) {
String[] pair = _pair.split("=");
try {
qry.put(URLDecoder.decode(pair[0], "UTF-8"),
pair.length > 0 ? URLDecoder.decode(pair[1], "UTF-8") : "");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
qry.put(decodeURIComponent(pair[0]),
pair.length > 0 ? decodeURIComponent(pair[1]) : "");
}
return qry;
}
public static String encodeURIComponent(String str) {
try {
return URLEncoder.encode(str, "UTF-8")
.replace("+", "%20")
.replace("%21", "!")
.replace("%27", "'")
.replace("%28", "(")
.replace("%29", ")")
.replace("%7E", "~");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
public static String decodeURIComponent(String str) {
try {
return URLDecoder.decode(str, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}