Skip to content

Commit

Permalink
fix: paging breaks with + sign on phone number (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkingserious authored Sep 17, 2020
1 parent 4548332 commit 2f5b9d5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,4 @@
<artifactId>oss-parent</artifactId>
<version>7</version>
</parent>
</project>
</project>
16 changes: 13 additions & 3 deletions src/main/java/com/twilio/http/Request.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.twilio.http;

import com.google.common.collect.Range;
import com.google.common.base.Joiner;
import com.twilio.exception.ApiException;
import com.twilio.exception.InvalidRequestException;
import org.joda.time.DateTime;
Expand Down Expand Up @@ -175,9 +176,18 @@ private String buildURL() {
host = joinIgnoreNull(".", product, targetEdge, targetRegion, domain);
}

return new URI(parsedUrl.getProtocol(), parsedUrl.getUserInfo(), host, parsedUrl.getPort(),
parsedUrl.getPath(), parsedUrl.getQuery(), parsedUrl.getRef()).toURL().toString();
} catch (final MalformedURLException | URISyntaxException e) {
String urlPort = parsedUrl.getPort() != -1 ? ":" + parsedUrl.getPort() : null;
String protocol = parsedUrl.getProtocol() + "://";
String[] pathPieces = parsedUrl.getPath().split("/");
for (int i = 0; i < pathPieces.length; i++) {
pathPieces[i] = URLEncoder.encode(pathPieces[i], "UTF-8");
}
String encodedPath = Joiner.on("/").join(pathPieces);
String query = parsedUrl.getQuery() != null ? "?" + parsedUrl.getQuery() : null;
String ref = parsedUrl.getRef() != null ? "#" + parsedUrl.getRef() : null;
String credentials = parsedUrl.getUserInfo() != null ? parsedUrl.getUserInfo() + "@" : null;
return joinIgnoreNull("", protocol, credentials, host, urlPort, encodedPath, query, ref);
} catch (final MalformedURLException | UnsupportedEncodingException e) {
throw new ApiException("Bad URL: " + url, e);
}
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/twilio/http/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,22 @@ public void testConstructURLWithPipe() throws MalformedURLException {
assertUrlsEqual(expected, url);
}

@Test
public void testConstructURLWithMultipleSlashes() throws MalformedURLException {
Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foo|bar/bar|foo");
URL url = r.constructURL();
URL expected = new URL("https://api.twilio.com/2010-04-01/foo%7Cbar/bar%7Cfoo");
assertUrlsEqual(expected, url);
}

@Test
public void testConstructURLWithCredentials() throws MalformedURLException {
Request r = new Request(HttpMethod.GET, "user:pass@" + Domains.API.toString(), "/2010-04-01/foobar");
URL url = r.constructURL();
URL expected = new URL("https://user:[email protected]/2010-04-01/foobar");
assertUrlsEqual(expected, url);
}

@Test
public void testConstructURLWithParam() throws MalformedURLException {
Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
Expand All @@ -72,6 +88,15 @@ public void testConstructURLWithParams() throws MalformedURLException {
assertUrlsEqual(expected, url);
}

@Test
public void testConstructURLWithPlusPrefix() {
Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
r.addQueryParam("To", "+18888888888");
URL url = r.constructURL();
String expected = "https://api.twilio.com/2010-04-01/foobar?To=%2B18888888888";
assertEquals(expected, url.toString());
}

@Test
public void testConstructURLWithMultivaluedParam() throws MalformedURLException {
Request r = new Request(HttpMethod.GET, Domains.API.toString(), "/2010-04-01/foobar");
Expand Down

0 comments on commit 2f5b9d5

Please sign in to comment.