#1727 - Prevent premature base encoding in UriTemplate if host is templated.

This commit is contained in:
Oliver Drotbohm
2021-12-10 10:50:05 +01:00
parent 809b5ca770
commit 004c46be3b
2 changed files with 15 additions and 4 deletions

View File

@@ -357,10 +357,14 @@ public class UriTemplate implements Iterable<TemplateVariable>, Serializable {
String head = index == -1 ? template : template.substring(0, index);
String tail = index == -1 ? "" : template.substring(index);
String encodedBase = UriComponentsBuilder.fromUriString(head)
.encode()
.build()
.toUriString();
// Encode head if it's more than just the scheme
String encodedBase = head.endsWith("://") && tail.startsWith("{")
? head
: UriComponentsBuilder.fromUriString(head)
.encode()
.build()
.toUriString();
head = encodedBase.length() > head.length() ? encodedBase : head;

View File

@@ -375,6 +375,13 @@ class UriTemplateUnitTest {
assertThat(template.toString()).isEqualTo("/path/{bar}/foo.zip?type=foo{&foobar}");
}
@Test // #1727
void supportsVariableInHostName() {
assertThatCode(() -> UriTemplate.of("https://{somehost}/somepath"))
.doesNotThrowAnyException();
}
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
assertVariables(template, Arrays.asList(variables));
}