Additional unit tests for operations on empty UriTemplate
See gh-32432
(cherry picked from commit 54a6d89da7)
This commit is contained in:
parent
5dfec09edd
commit
274fba47f3
|
|
@ -34,7 +34,7 @@ import static org.assertj.core.api.Assertions.assertThatNoException;
|
|||
* @author Juergen Hoeller
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class UriTemplateTests {
|
||||
class UriTemplateTests {
|
||||
|
||||
@Test
|
||||
void emptyPathDoesNotThrowException() {
|
||||
|
|
@ -47,70 +47,84 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void getVariableNames() throws Exception {
|
||||
void getVariableNames() {
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
List<String> variableNames = template.getVariableNames();
|
||||
assertThat(variableNames).as("Invalid variable names").isEqualTo(Arrays.asList("hotel", "booking"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandVarArgs() throws Exception {
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
URI result = template.expand("1", "42");
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42"));
|
||||
}
|
||||
|
||||
@Test // SPR-9712
|
||||
public void expandVarArgsWithArrayValue() throws Exception {
|
||||
UriTemplate template = new UriTemplate("/sum?numbers={numbers}");
|
||||
URI result = template.expand(new int[] {1, 2, 3});
|
||||
assertThat(result).isEqualTo(new URI("/sum?numbers=1,2,3"));
|
||||
void getVariableNamesFromEmpty() {
|
||||
UriTemplate template = new UriTemplate("");
|
||||
List<String> variableNames = template.getVariableNames();
|
||||
assertThat(variableNames).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandVarArgsNotEnoughVariables() throws Exception {
|
||||
void expandVarArgs() {
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
URI result = template.expand("1", "42");
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expandVarArgsFromEmpty() {
|
||||
UriTemplate template = new UriTemplate("");
|
||||
URI result = template.expand();
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create(""));
|
||||
}
|
||||
|
||||
@Test // SPR-9712
|
||||
void expandVarArgsWithArrayValue() {
|
||||
UriTemplate template = new UriTemplate("/sum?numbers={numbers}");
|
||||
URI result = template.expand(new int[] {1, 2, 3});
|
||||
assertThat(result).isEqualTo(URI.create("/sum?numbers=1,2,3"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void expandVarArgsNotEnoughVariables() {
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> template.expand("1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMap() throws Exception {
|
||||
void expandMap() {
|
||||
Map<String, String> uriVariables = new HashMap<>(2);
|
||||
uriVariables.put("booking", "42");
|
||||
uriVariables.put("hotel", "1");
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
URI result = template.expand(uriVariables);
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42"));
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMapDuplicateVariables() throws Exception {
|
||||
void expandMapDuplicateVariables() {
|
||||
UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
|
||||
assertThat(template.getVariableNames()).isEqualTo(Arrays.asList("c", "c", "c"));
|
||||
URI result = template.expand(Collections.singletonMap("c", "cheeseburger"));
|
||||
assertThat(result).isEqualTo(new URI("/order/cheeseburger/cheeseburger/cheeseburger"));
|
||||
assertThat(result).isEqualTo(URI.create("/order/cheeseburger/cheeseburger/cheeseburger"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMapNonString() throws Exception {
|
||||
void expandMapNonString() {
|
||||
Map<String, Integer> uriVariables = new HashMap<>(2);
|
||||
uriVariables.put("booking", 42);
|
||||
uriVariables.put("hotel", 1);
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
URI result = template.expand(uriVariables);
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotels/1/bookings/42"));
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMapEncoded() throws Exception {
|
||||
void expandMapEncoded() {
|
||||
Map<String, String> uriVariables = Collections.singletonMap("hotel", "Z\u00fcrich");
|
||||
UriTemplate template = new UriTemplate("/hotel list/{hotel}");
|
||||
URI result = template.expand(uriVariables);
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotel%20list/Z%C3%BCrich"));
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotel%20list/Z%C3%BCrich"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandMapUnboundVariables() throws Exception {
|
||||
void expandMapUnboundVariables() {
|
||||
Map<String, String> uriVariables = new HashMap<>(2);
|
||||
uriVariables.put("booking", "42");
|
||||
uriVariables.put("bar", "1");
|
||||
|
|
@ -120,14 +134,14 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void expandEncoded() throws Exception {
|
||||
void expandEncoded() {
|
||||
UriTemplate template = new UriTemplate("/hotel list/{hotel}");
|
||||
URI result = template.expand("Z\u00fcrich");
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(new URI("/hotel%20list/Z%C3%BCrich"));
|
||||
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotel%20list/Z%C3%BCrich"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matches() throws Exception {
|
||||
void matches() {
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
|
||||
assertThat(template.matches("/hotels/1/bookings/42")).as("UriTemplate does not match").isTrue();
|
||||
assertThat(template.matches("/hotels/bookings")).as("UriTemplate matches").isFalse();
|
||||
|
|
@ -136,14 +150,23 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void matchesCustomRegex() throws Exception {
|
||||
void matchesAgainstEmpty() {
|
||||
UriTemplate template = new UriTemplate("");
|
||||
assertThat(template.matches("/hotels/1/bookings/42")).as("UriTemplate matches").isFalse();
|
||||
assertThat(template.matches("/hotels/bookings")).as("UriTemplate matches").isFalse();
|
||||
assertThat(template.matches("")).as("UriTemplate does not match").isTrue();
|
||||
assertThat(template.matches(null)).as("UriTemplate matches").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchesCustomRegex() {
|
||||
UriTemplate template = new UriTemplate("/hotels/{hotel:\\d+}");
|
||||
assertThat(template.matches("/hotels/42")).as("UriTemplate does not match").isTrue();
|
||||
assertThat(template.matches("/hotels/foo")).as("UriTemplate matches").isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void match() throws Exception {
|
||||
void match() {
|
||||
Map<String, String> expected = new HashMap<>(2);
|
||||
expected.put("booking", "42");
|
||||
expected.put("hotel", "1");
|
||||
|
|
@ -154,7 +177,14 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void matchCustomRegex() throws Exception {
|
||||
void matchAgainstEmpty() {
|
||||
UriTemplate template = new UriTemplate("");
|
||||
Map<String, String> result = template.match("/hotels/1/bookings/42");
|
||||
assertThat(result).as("Invalid match").isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void matchCustomRegex() {
|
||||
Map<String, String> expected = new HashMap<>(2);
|
||||
expected.put("booking", "42");
|
||||
expected.put("hotel", "1");
|
||||
|
|
@ -165,14 +195,14 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test // SPR-13627
|
||||
public void matchCustomRegexWithNestedCurlyBraces() throws Exception {
|
||||
void matchCustomRegexWithNestedCurlyBraces() {
|
||||
UriTemplate template = new UriTemplate("/site.{domain:co.[a-z]{2}}");
|
||||
Map<String, String> result = template.match("/site.co.eu");
|
||||
assertThat(result).as("Invalid match").isEqualTo(Collections.singletonMap("domain", "co.eu"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void matchDuplicate() throws Exception {
|
||||
void matchDuplicate() {
|
||||
UriTemplate template = new UriTemplate("/order/{c}/{c}/{c}");
|
||||
Map<String, String> result = template.match("/order/cheeseburger/cheeseburger/cheeseburger");
|
||||
Map<String, String> expected = Collections.singletonMap("c", "cheeseburger");
|
||||
|
|
@ -180,7 +210,7 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void matchMultipleInOneSegment() throws Exception {
|
||||
void matchMultipleInOneSegment() {
|
||||
UriTemplate template = new UriTemplate("/{foo}-{bar}");
|
||||
Map<String, String> result = template.match("/12-34");
|
||||
Map<String, String> expected = new HashMap<>(2);
|
||||
|
|
@ -190,19 +220,19 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test // SPR-16169
|
||||
public void matchWithMultipleSegmentsAtTheEnd() throws Exception {
|
||||
void matchWithMultipleSegmentsAtTheEnd() {
|
||||
UriTemplate template = new UriTemplate("/account/{accountId}");
|
||||
assertThat(template.matches("/account/15/alias/5")).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void queryVariables() throws Exception {
|
||||
void queryVariables() {
|
||||
UriTemplate template = new UriTemplate("/search?q={query}");
|
||||
assertThat(template.matches("/search?q=foo")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fragments() throws Exception {
|
||||
void fragments() {
|
||||
UriTemplate template = new UriTemplate("/search#{fragment}");
|
||||
assertThat(template.matches("/search#foo")).isTrue();
|
||||
|
||||
|
|
@ -211,19 +241,19 @@ public class UriTemplateTests {
|
|||
}
|
||||
|
||||
@Test // SPR-13705
|
||||
public void matchesWithSlashAtTheEnd() throws Exception {
|
||||
void matchesWithSlashAtTheEnd() {
|
||||
assertThat(new UriTemplate("/test/").matches("/test/")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandWithDollar() throws Exception {
|
||||
void expandWithDollar() {
|
||||
UriTemplate template = new UriTemplate("/{a}");
|
||||
URI uri = template.expand("$replacement");
|
||||
assertThat(uri.toString()).isEqualTo("/$replacement");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void expandWithAtSign() throws Exception {
|
||||
void expandWithAtSign() {
|
||||
UriTemplate template = new UriTemplate("http://localhost/query={query}");
|
||||
URI uri = template.expand("foo@bar");
|
||||
assertThat(uri.toString()).isEqualTo("http://localhost/query=foo@bar");
|
||||
|
|
|
|||
Loading…
Reference in New Issue