From aaa164179cdf10a4076d0270e5b05bf8917028a5 Mon Sep 17 00:00:00 2001 From: Timur Alperovich Date: Fri, 21 Aug 2015 15:12:05 -0700 Subject: [PATCH] JCLOUDS-217: Percent encoding changes. jclouds should not check if the string is encoded, but rather expect that all strings would be encoded prior to transmission. As part of that change, we must make sure that no code relies on such behavior within jclouds. This commit adds a blobstore test to check encoding pattern for blobs. It also removes the encoding check in the Strings2 class and the related test. --- .../BaseContainerIntegrationTest.java | 27 ++++++++++++++++++- core/src/main/java/org/jclouds/http/Uris.java | 1 - .../main/java/org/jclouds/util/Strings2.java | 9 +++---- .../java/org/jclouds/util/Strings2Test.java | 6 ----- 4 files changed, 29 insertions(+), 14 deletions(-) diff --git a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseContainerIntegrationTest.java b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseContainerIntegrationTest.java index ce719e05c4..9f8383b43b 100644 --- a/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseContainerIntegrationTest.java +++ b/blobstore/src/test/java/org/jclouds/blobstore/integration/internal/BaseContainerIntegrationTest.java @@ -47,6 +47,7 @@ import org.jclouds.blobstore.domain.ContainerAccess; import org.jclouds.blobstore.domain.PageSet; import org.jclouds.blobstore.domain.StorageMetadata; import org.jclouds.blobstore.options.ListContainerOptions; +import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import com.google.common.collect.ImmutableMap; @@ -561,7 +562,31 @@ public class BaseContainerIntegrationTest extends BaseBlobStoreIntegrationTest { } } - private void checkEqualNames(ImmutableSet expectedSet, PageSet results) { + @DataProvider + public Object[][] getBlobsToEscape() { + ImmutableSet testNames = ImmutableSet.of("%20", "%20 ", " %20", " "); + Object[][] result = new Object[1][1]; + result[0][0] = testNames; + return result; + } + + @Test(dataProvider = "getBlobsToEscape", groups = {"integration", "live"}) + public void testBlobNameEscaping(Set blobNames) throws InterruptedException { + final String containerName = getContainerName(); + BlobStore blobStore = view.getBlobStore(); + try { + for (String name : blobNames) { + Blob blob = blobStore.blobBuilder(name).payload(ByteSource.wrap("test".getBytes())).contentLength(4) + .build(); + blobStore.putBlob(containerName, blob); + } + checkEqualNames(blobNames, blobStore.list(containerName)); + } finally { + returnContainer(containerName); + } + } + + private void checkEqualNames(Set expectedSet, PageSet results) { Set names = new HashSet(); for (StorageMetadata sm : results) { names.add(sm.getName()); diff --git a/core/src/main/java/org/jclouds/http/Uris.java b/core/src/main/java/org/jclouds/http/Uris.java index eaef6636ce..abab52b096 100644 --- a/core/src/main/java/org/jclouds/http/Uris.java +++ b/core/src/main/java/org/jclouds/http/Uris.java @@ -144,7 +144,6 @@ public final class Uris { } public UriBuilder appendPath(String path) { - path = urlDecode(checkNotNull(path, "path")); if (this.path == null) { path(path); } else { diff --git a/core/src/main/java/org/jclouds/util/Strings2.java b/core/src/main/java/org/jclouds/util/Strings2.java index 2081c3d6d4..e58e0a2d31 100644 --- a/core/src/main/java/org/jclouds/util/Strings2.java +++ b/core/src/main/java/org/jclouds/util/Strings2.java @@ -53,8 +53,6 @@ public class Strings2 { } public static String urlEncode(String in, Iterable skipEncode) { - if (isUrlEncoded(in)) - return in; try { String returnVal = URLEncoder.encode(in, "UTF-8"); returnVal = returnVal.replace("+", "%20"); @@ -109,11 +107,10 @@ public class Strings2 { public static String urlDecode(@Nullable String in) { if (in == null) return null; - String input = in.toString(); - // Don't double decode - if (!isUrlEncoded(input)) { - return input; + if (!isUrlEncoded(in)) { + return in; } + String input = in.toString(); try { return URLDecoder.decode(input, "UTF-8"); } catch (UnsupportedEncodingException e) { diff --git a/core/src/test/java/org/jclouds/util/Strings2Test.java b/core/src/test/java/org/jclouds/util/Strings2Test.java index 84fc776546..125e0eb4fa 100644 --- a/core/src/test/java/org/jclouds/util/Strings2Test.java +++ b/core/src/test/java/org/jclouds/util/Strings2Test.java @@ -32,12 +32,6 @@ public class Strings2Test { assert !Strings2.isUrlEncoded("/read-tests/ tep"); } - public void testNoDoubleEncode() { - assertEquals(urlEncode("/read-tests/%73%6f%6d%65%20%66%69%6c%65", '/'), - "/read-tests/%73%6f%6d%65%20%66%69%6c%65"); - 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");