JCLOUDS-217: Only decode strings that are actually encoded.

* Update HttpRequestTests to account for change in urlDecode.
* Related to JCLOUDS-200
This commit is contained in:
Diwaker Gupta 2013-08-01 09:44:59 -07:00 committed by Andrew Phillips
parent e6765cc5db
commit 12f29fd8a9
3 changed files with 15 additions and 26 deletions

View File

@ -115,8 +115,13 @@ public class Strings2 {
public static String urlDecode(@Nullable String in) { public static String urlDecode(@Nullable String in) {
if (in == null) if (in == null)
return null; return null;
String input = in.toString();
// Don't double decode
if (!isUrlEncoded(input)) {
return input;
}
try { try {
return URLDecoder.decode(in, "UTF-8"); return URLDecoder.decode(input, "UTF-8");
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new IllegalStateException("Bad encoding on input: " + in, e); throw new IllegalStateException("Bad encoding on input: " + in, e);
} }

View File

@ -67,45 +67,24 @@ public class HttpRequestTest {
.builder().method("GET").endpoint("http://foo").payload(payload).build()); .builder().method("GET").endpoint("http://foo").payload(payload).build());
} }
// the following caused issues for the fgcp provider // note that + ends up encoded as %2B (plus) and %2F converts back into slash
// (see RequestAuthenticator#addQueryParamsToRequest)
// base64 symbols should be url encoded in query param
// note that + ends up encoded as %20 (space), not %2B (plus)
public void testAddingBase64EncodedQueryParamCausingPlusToUrlEncodedSpaceConversion() {
String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
URI uri = URI
.create("http://goo.com:443?header1=valueWithUrlEncoded%2BPlus");
HttpRequest request = HttpRequest.builder().method("GET").endpoint(uri)
// addQueryParam invocation causes %2B's in prev. params to
// convert to %20.
.addQueryParam("header2", base64Chars).build();
assertEquals(
request.getRequestLine(),
"GET http://goo.com:443?header1=valueWithUrlEncoded%20Plus&header2=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%20/%3D HTTP/1.1");
}
// note that + ends up encoded as %20 (space) in the first param, %2B (plus)
// in the last param and %2F converts back into slash
public void testAddBase64AndUrlEncodedQueryParams() { public void testAddBase64AndUrlEncodedQueryParams() {
String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B%2F%3D"; String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B%2F%3D";
URI uri = URI.create("http://goo.com:443?header1=" + base64Chars); URI uri = URI.create("http://goo.com:443?header1=" + base64Chars);
HttpRequest request = HttpRequest.builder() HttpRequest request = HttpRequest.builder()
.method("GET") .method("GET")
.endpoint(uri) .endpoint(uri)
// the addition of another param causes %2B's in prev. params to
// convert to %20.
.addQueryParam("header2", base64Chars) .addQueryParam("header2", base64Chars)
.build(); .build();
assertEquals( assertEquals(
request.getRequestLine(), request.getRequestLine(),
"GET http://goo.com:443?header1=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%20/%3D&header2=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B/%3D HTTP/1.1"); "GET http://goo.com:443?header1=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B/%3D&header2=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B/%3D HTTP/1.1");
} }
// base64 symbols with newline separator should be url encoded in query param // base64 symbols with newline separator should be url encoded in query param
public void testAddBase64EncodedQueryParamWithNewlines() { public void testAddBase64EncodedQueryParamWithNewlines() {
String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789%2B/="; String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789+/=";
URI uri = URI.create("http://goo.com:443?header1=value1"); URI uri = URI.create("http://goo.com:443?header1=value1");
HttpRequest request = HttpRequest.builder().method("GET").endpoint(uri) HttpRequest request = HttpRequest.builder().method("GET").endpoint(uri)
.addQueryParam("header2", base64Chars).build(); .addQueryParam("header2", base64Chars).build();

View File

@ -41,6 +41,11 @@ public class Strings2Test {
assertEquals(urlEncode("/read-tests/ tep", '/'), "/read-tests/%20tep"); assertEquals(urlEncode("/read-tests/ tep", '/'), "/read-tests/%20tep");
} }
public void testNoDoubleDecode() {
assertEquals(urlDecode("foo%20bar%2Bbaz"), "foo bar+baz");
assertEquals(urlDecode("foo bar+baz"), "foo bar+baz");
}
public void testReplaceTokens() { public void testReplaceTokens() {
assertEquals(Strings2.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world"); assertEquals(Strings2.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world");
} }