Skip to content

Commit 54a6d89

Browse files
committed
Additional unit tests for operations on empty UriTemplate
See gh-32432
1 parent 2922a82 commit 54a6d89

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

spring-web/src/test/java/org/springframework/web/util/UriTemplateTests.java

+30
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,27 @@ void getVariableNames() {
5353
assertThat(variableNames).as("Invalid variable names").isEqualTo(Arrays.asList("hotel", "booking"));
5454
}
5555

56+
@Test
57+
void getVariableNamesFromEmpty() {
58+
UriTemplate template = new UriTemplate("");
59+
List<String> variableNames = template.getVariableNames();
60+
assertThat(variableNames).isEmpty();
61+
}
62+
5663
@Test
5764
void expandVarArgs() {
5865
UriTemplate template = new UriTemplate("/hotels/{hotel}/bookings/{booking}");
5966
URI result = template.expand("1", "42");
6067
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create("/hotels/1/bookings/42"));
6168
}
6269

70+
@Test
71+
void expandVarArgsFromEmpty() {
72+
UriTemplate template = new UriTemplate("");
73+
URI result = template.expand();
74+
assertThat(result).as("Invalid expanded template").isEqualTo(URI.create(""));
75+
}
76+
6377
@Test // SPR-9712
6478
void expandVarArgsWithArrayValue() {
6579
UriTemplate template = new UriTemplate("/sum?numbers={numbers}");
@@ -135,6 +149,15 @@ void matches() {
135149
assertThat(template.matches(null)).as("UriTemplate matches").isFalse();
136150
}
137151

152+
@Test
153+
void matchesAgainstEmpty() {
154+
UriTemplate template = new UriTemplate("");
155+
assertThat(template.matches("/hotels/1/bookings/42")).as("UriTemplate matches").isFalse();
156+
assertThat(template.matches("/hotels/bookings")).as("UriTemplate matches").isFalse();
157+
assertThat(template.matches("")).as("UriTemplate does not match").isTrue();
158+
assertThat(template.matches(null)).as("UriTemplate matches").isFalse();
159+
}
160+
138161
@Test
139162
void matchesCustomRegex() {
140163
UriTemplate template = new UriTemplate("/hotels/{hotel:\\d+}");
@@ -153,6 +176,13 @@ void match() {
153176
assertThat(result).as("Invalid match").isEqualTo(expected);
154177
}
155178

179+
@Test
180+
void matchAgainstEmpty() {
181+
UriTemplate template = new UriTemplate("");
182+
Map<String, String> result = template.match("/hotels/1/bookings/42");
183+
assertThat(result).as("Invalid match").isEmpty();
184+
}
185+
156186
@Test
157187
void matchCustomRegex() {
158188
Map<String, String> expected = new HashMap<>(2);

0 commit comments

Comments
 (0)