Skip to content

Commit

Permalink
Additional unit tests for operations on empty UriTemplate
Browse files Browse the repository at this point in the history
See gh-32432

(cherry picked from commit 54a6d89)
  • Loading branch information
jhoeller committed Mar 13, 2024
1 parent 5dfec09 commit 274fba4
Showing 1 changed file with 61 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* @author Juergen Hoeller
* @author Rossen Stoyanchev
*/
public class UriTemplateTests {
class UriTemplateTests {

@Test
void emptyPathDoesNotThrowException() {
Expand All @@ -47,70 +47,84 @@ void nullPathThrowsException() {
}

@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 {
void getVariableNamesFromEmpty() {
UriTemplate template = new UriTemplate("");
List<String> variableNames = template.getVariableNames();
assertThat(variableNames).isEmpty();
}

@Test
void expandVarArgs() {
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"));
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
public void expandVarArgsWithArrayValue() throws Exception {
void expandVarArgsWithArrayValue() {
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"));
assertThat(result).isEqualTo(URI.create("/sum?numbers=1,2,3"));
}

@Test
public void expandVarArgsNotEnoughVariables() throws Exception {
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");
Expand All @@ -120,14 +134,14 @@ public void expandMapUnboundVariables() throws Exception {
}

@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();
Expand All @@ -136,14 +150,23 @@ public void matches() throws Exception {
}

@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");
Expand All @@ -154,7 +177,14 @@ public void match() throws Exception {
}

@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");
Expand All @@ -165,22 +195,22 @@ public void matchCustomRegex() throws Exception {
}

@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");
assertThat(result).as("Invalid match").isEqualTo(expected);
}

@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);
Expand All @@ -190,19 +220,19 @@ public void matchMultipleInOneSegment() throws Exception {
}

@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();

Expand All @@ -211,19 +241,19 @@ public void fragments() throws Exception {
}

@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");
Expand Down

0 comments on commit 274fba4

Please sign in to comment.