Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: encode path parameters #558

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions src/main/java/com/twilio/http/Request.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]);

Expand All @@ -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);
}
}

/**
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/com/twilio/Assert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> queryStringToMap(final String queryString) {
Expand Down
21 changes: 15 additions & 6 deletions src/test/java/com/twilio/http/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}