Skip to content

Commit

Permalink
Improve HttpConfig.sanitizeUri
Browse files Browse the repository at this point in the history
  • Loading branch information
SMadani committed Jan 20, 2025
1 parent 9ce3f4d commit 1c1fdd8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/main/java/com/vonage/client/HttpConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,11 @@ public static class Builder {
public Builder() {}

private String sanitizeUri(String uri) {
if (uri != null && uri.endsWith("/")) {
return uri.substring(0, uri.length() - 1);
String sanitized = Objects.requireNonNull(uri, "URI must not be null");
if (sanitized.endsWith("/")) {
sanitized = sanitized.substring(0, sanitized.length() - 1);
}
return uri;
return URI.create(sanitized).toString();
}

/**
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/com/vonage/client/HttpConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@ public void testAllBaseUri() {
config.getRegionalBaseUri(ApiRegion.API_EU)
);
assertEquals("https://api-ap.example.com", config.getRegionalBaseUri(ApiRegion.API_AP).toString());

assertThrows(NullPointerException.class, () ->
HttpConfig.builder().baseUri((String) null).build()
);
assertThrows(IllegalArgumentException.class, () ->
HttpConfig.builder().baseUri("*&^%$£;@not-a_'u(R)L").build()
);
}

@Test
Expand Down

0 comments on commit 1c1fdd8

Please sign in to comment.