diff --git a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java index 723aa83a99f0..54c0640bfc07 100644 --- a/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java +++ b/spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java @@ -53,6 +53,13 @@ void getVariableNames() { assertThat(variableNames).as("Invalid variable names").isEqualTo(Arrays.asList("hotel", "booking")); } + @Test + void getVariableNamesFromEmpty() { + UriTemplate template = new UriTemplate(""); + List variableNames = template.getVariableNames(); + assertThat(variableNames).isEmpty(); + } + @Test void expandVarArgs() { UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}"); @@ -60,6 +67,13 @@ void expandVarArgs() { 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}"); @@ -135,6 +149,15 @@ void matches() { assertThat(template.matches(null)).as("UriTemplate matches").isFalse(); } + @Test + 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+}"); @@ -153,6 +176,13 @@ void match() { assertThat(result).as("Invalid match").isEqualTo(expected); } + @Test + void matchAgainstEmpty() { + UriTemplate template = new UriTemplate(""); + Map result = template.match("/hotels/1/bookings/42"); + assertThat(result).as("Invalid match").isEmpty(); + } + @Test void matchCustomRegex() { Map expected = new HashMap<>(2);