From 5262c4ac36fb54b32357407b856d33968a9fe9ab Mon Sep 17 00:00:00 2001 From: Doug Miller Date: Mon, 27 Apr 2020 13:44:52 -0500 Subject: [PATCH] Encoding path parameters --- src/main/java/com/recurly/v3/BaseClient.java | 11 +++++++++-- src/test/java/com/recurly/v3/BaseClientTest.java | 6 +++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/recurly/v3/BaseClient.java b/src/main/java/com/recurly/v3/BaseClient.java index af0823d..94e2685 100644 --- a/src/main/java/com/recurly/v3/BaseClient.java +++ b/src/main/java/com/recurly/v3/BaseClient.java @@ -2,7 +2,10 @@ import com.recurly.v3.http.HeaderInterceptor; import java.io.IOException; +import java.io.UnsupportedEncodingException; import java.lang.reflect.Type; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.HashMap; import java.util.Map; import java.util.List; @@ -237,8 +240,12 @@ protected String interpolatePath(String path, final HashMap urlP while (m.find()) { final String key = m.group(1).replace("{", "").replace("}", ""); - final String value = urlParams.get(key); - path = path.replace(m.group(1), value); + try { + final String value = URLEncoder.encode(urlParams.get(key), StandardCharsets.UTF_8.toString()); + path = path.replace(m.group(1), value); + } catch (UnsupportedEncodingException ex) { + throw new RecurlyException(ex.getCause()); + } } return path.replaceAll("\\{", "").replaceAll("\\}", ""); diff --git a/src/test/java/com/recurly/v3/BaseClientTest.java b/src/test/java/com/recurly/v3/BaseClientTest.java index 8342e54..4cfc722 100644 --- a/src/test/java/com/recurly/v3/BaseClientTest.java +++ b/src/test/java/com/recurly/v3/BaseClientTest.java @@ -212,11 +212,11 @@ public void testInterpolatePathWithParams() { final MockClient client = new MockClient("apiKey"); final String path = "/accounts/{account_id}/notes/{account_note_id}"; final HashMap urlParams = new HashMap(); - urlParams.put("account_id", "accountId"); - urlParams.put("account_note_id", "noteId"); + urlParams.put("account_id", "accountId/"); + urlParams.put("account_note_id", "noteId,"); final String interpolatedPath = client.interpolatePath(path, urlParams); - assertEquals("/accounts/accountId/notes/noteId", interpolatedPath); + assertEquals("/accounts/accountId%2F/notes/noteId%2C", interpolatedPath); } protected static void setEnv(Map newenv) throws Exception {