From 26404f7842ff59b98340ce39493aab3f98fff3c4 Mon Sep 17 00:00:00 2001 From: Elise Shanholtz Date: Tue, 7 Jul 2020 15:39:07 -0700 Subject: [PATCH] fix: encode path parameters --- src/main/java/com/twilio/http/Request.java | 23 ++++++++++--------- src/test/java/com/twilio/Assert.java | 9 +++++--- .../java/com/twilio/http/RequestTest.java | 21 ++++++++++++----- 3 files changed, 33 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/twilio/http/Request.java b/src/main/java/com/twilio/http/Request.java index 756208d120..e00bb38fbb 100644 --- a/src/main/java/com/twilio/http/Request.java +++ b/src/main/java/com/twilio/http/Request.java @@ -147,10 +147,12 @@ public URL constructURL() { } private String buildURL() { - if (region != null || edge != null) { - try { - final URL parsedUrl = new URL(url); - final String[] pieces = parsedUrl.getHost().split("\\."); + try { + final URL parsedUrl = new URL(url); + String host = parsedUrl.getHost(); + final String[] pieces = host.split("\\."); + + if (pieces.length > 1) { final String product = pieces[0]; final String domain = joinIgnoreNull(".", pieces[pieces.length - 2], pieces[pieces.length - 1]); @@ -167,15 +169,14 @@ private String buildURL() { if (targetEdge != null && targetRegion == null) targetRegion = DEFAULT_REGION; - final String host = joinIgnoreNull(".", product, targetEdge, targetRegion, domain); - - return new URL(parsedUrl.getProtocol(), host, parsedUrl.getPort(), parsedUrl.getFile()).toString(); - } catch (final MalformedURLException e) { - throw new ApiException("Bad URL: " + url, e); + host = joinIgnoreNull(".", product, targetEdge, targetRegion, domain); } - } - return url; + return new URI(parsedUrl.getProtocol(), parsedUrl.getUserInfo(), host, parsedUrl.getPort(), + parsedUrl.getPath(), parsedUrl.getQuery(), parsedUrl.getRef()).toURL().toString(); + } catch (final MalformedURLException | URISyntaxException e) { + throw new ApiException("Bad URL: " + url, e); + } } /** diff --git a/src/test/java/com/twilio/Assert.java b/src/test/java/com/twilio/Assert.java index 4c8b239a00..6a03b127a5 100644 --- a/src/test/java/com/twilio/Assert.java +++ b/src/test/java/com/twilio/Assert.java @@ -30,15 +30,18 @@ public static void assertUrlsEqual(final String expected, final URL actual) { } public static void assertUrlsEqual(final String expected, final String actual) { - String[] expectedParts = expected.split("\\?"); - String[] actualParts = actual.split("\\?"); + String[] expectedParts = expected.split("[?#]"); + String[] actualParts = actual.split("[?#]"); assertEquals(expectedParts.length, actualParts.length); assertEquals(expectedParts[0], actualParts[0]); - if (expectedParts.length == 2) { + if (expectedParts.length >= 2) { assertQueryStringsEqual(expectedParts[1], actualParts[1]); } + if (expectedParts.length == 3) { + assertEquals(expectedParts[2], actualParts[2]); + } } private static Map queryStringToMap(final String queryString) { diff --git a/src/test/java/com/twilio/http/RequestTest.java b/src/test/java/com/twilio/http/RequestTest.java index fca8b27e1c..139df989d4 100644 --- a/src/test/java/com/twilio/http/RequestTest.java +++ b/src/test/java/com/twilio/http/RequestTest.java @@ -14,6 +14,7 @@ import static com.twilio.Assert.assertQueryStringsEqual; import static com.twilio.Assert.assertUrlsEqual; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -44,6 +45,14 @@ public void testConstructURLURISyntaxException() { fail("ApiException was expected"); } + @Test + public void testConstructURLWithPipe() throws MalformedURLException { + Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foo|bar"); + URL url = r.constructURL(); + URL expected = new URL("https://api.twilio.com/2010-04-01/foo%7Cbar"); + assertUrlsEqual(expected, url); + } + @Test public void testConstructURLWithParam() throws MalformedURLException { Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar"); @@ -196,13 +205,13 @@ public void testRegionAndEdgeInUrl() throws MalformedURLException { } @Test - public void testRegionInConstructor() throws MalformedURLException { - final Request request = new Request(HttpMethod.GET, Domains.ACCOUNTS.toString(), "/path/to/something.json?foo=12.34", "region"); + public void testRegionInConstructor() { + final Request request = new Request(HttpMethod.GET, Domains.ACCOUNTS.toString(), "/path/to/something.json?foo=12.34#bar", "region"); - assertUrlsEqual(new URL("https://accounts.region.twilio.com/path/to/something.json?foo=12.34"), request.constructURL()); + assertUrlsEqual("https://accounts.region.twilio.com/path/to/something.json?foo=12.34#bar", request.constructURL()); request.setEdge("edge"); - assertUrlsEqual(new URL("https://accounts.edge.region.twilio.com/path/to/something.json?foo=12.34"), request.constructURL()); + assertUrlsEqual("https://accounts.edge.region.twilio.com/path/to/something.json?foo=12.34#bar", request.constructURL()); } @Test @@ -240,7 +249,7 @@ public void testRequiresAuthentication() { public void testEquals() { Request request = new Request(HttpMethod.DELETE, "/uri"); request.setAuth("username", "password"); - assertFalse(request.equals(new Object())); - assertFalse(request.equals(null)); + assertNotEquals(request, new Object()); + assertNotEquals(null, request); } }