SdkHttpUtils { public static String urlEncode(final String value, final boolean path) { if (value == null) { return ""; } try { String encoded = URLEncoder.encode(value, DEFAULT_ENCODING); Matcher matcher = ENCODED_CHARACTERS_PATTERN.matcher(encoded); StringBuffer buffer = new StringBuffer(encoded.length()); while (matcher.find()) { String replacement = matcher.group(0); if ("+".equals(replacement)) { replacement = "%20"; } else if ("*".equals(replacement)) { replacement = "%2A"; } else if ("%7E".equals(replacement)) { replacement = "~"; } else if (path && "%2F".equals(replacement)) { replacement = "/"; } matcher.appendReplacement(buffer, replacement); } matcher.appendTail(buffer); return buffer.toString(); } catch (UnsupportedEncodingException ex) { throw new RuntimeException(ex); } }  static String urlEncode(final String value, final boolean path); static String urlDecode(final String value); static boolean isUsingNonDefaultPort(URI uri); static boolean usePayloadForQueryParameters(SignableRequest<?> request); static String encodeParameters(SignableRequest<?> request); static String appendUri(String baseUri, String path); static String appendUri(final String baseUri, String path, final boolean escapeDoubleSlash);  }
@Test public void testEncodeNull() { Assert.assertEquals("urlEncode(null) returned something unexpected", "", SdkHttpUtils.urlEncode(null, false)); }
@Test public void testEncodeEmptyString() { Assert.assertEquals("urlEncode(\"\") returned something unexpected", "", SdkHttpUtils.urlEncode("", false)); }
@Test public void testNoEncoding() { String test = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "-_.~"; Assert.assertEquals("urlEncode(\"" + test + "\") returned something " + "unexpected", test, SdkHttpUtils.urlEncode(test, false)); }
@Test public void testNoEncodingPath() { String test = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "-_.~/"; Assert.assertEquals("urlEncode(\"" + test + "\") returned something " + "unexpected", test, SdkHttpUtils.urlEncode(test, true)); }
@Test public void testEncoding() { String test = "\t\n\r " + "!\"#$" + "%&'(" + ")*+," + "/:;<" + "=>?@" + "[\\]^" + "`{|}"; String expected = "%09%0A%0D%20" + "%21%22%23%24" + "%25%26%27%28" + "%29%2A%2B%2C" + "%2F%3A%3B%3C" + "%3D%3E%3F%40" + "%5B%5C%5D%5E" + "%60%7B%7C%7D"; Assert.assertEquals("urlEncode(\"" + test + "\") returned something " + "unexpected", expected, SdkHttpUtils.urlEncode(test, false)); }