Merge pull request #21392 from jkschneider

* pr/21392:
  Fix uri tag for empty path

Closes gh-21392
pull/22022/head
Stephane Nicoll 5 years ago
commit 3f372824ea

@ -110,6 +110,9 @@ public final class WebFluxTags {
if (ignoreTrailingSlash && patternString.length() > 1) { if (ignoreTrailingSlash && patternString.length() > 1) {
patternString = TRAILING_SLASH_PATTERN.matcher(patternString).replaceAll(""); patternString = TRAILING_SLASH_PATTERN.matcher(patternString).replaceAll("");
} }
if (patternString.isEmpty()) {
return URI_ROOT;
}
return Tag.of("uri", patternString); return Tag.of("uri", patternString);
} }
HttpStatus status = exchange.getResponse().getStatusCode(); HttpStatus status = exchange.getResponse().getStatusCode();

@ -115,6 +115,9 @@ public final class WebMvcTags {
if (ignoreTrailingSlash && pattern.length() > 1) { if (ignoreTrailingSlash && pattern.length() > 1) {
pattern = TRAILING_SLASH_PATTERN.matcher(pattern).replaceAll(""); pattern = TRAILING_SLASH_PATTERN.matcher(pattern).replaceAll("");
} }
if (pattern.isEmpty()) {
return URI_ROOT;
}
return Tag.of("uri", pattern); return Tag.of("uri", pattern);
} }
if (response != null) { if (response != null) {

@ -58,6 +58,14 @@ class WebMvcTagsTests {
assertThat(tag.getValue()).isEqualTo("/spring/"); assertThat(tag.getValue()).isEqualTo("/spring/");
} }
@Test
void uriTagValueIsRootWhenBestMatchingPatternIsEmpty() {
this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "");
this.response.setStatus(301);
Tag tag = WebMvcTags.uri(this.request, this.response);
assertThat(tag.getValue()).isEqualTo("root");
}
@Test @Test
void uriTagValueWithBestMatchingPatternAndIgnoreTrailingSlashRemoveTrailingSlash() { void uriTagValueWithBestMatchingPatternAndIgnoreTrailingSlashRemoveTrailingSlash() {
this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/spring/"); this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, "/spring/");

@ -61,6 +61,15 @@ class WebFluxTagsTests {
assertThat(tag.getValue()).isEqualTo("/spring/"); assertThat(tag.getValue()).isEqualTo("/spring/");
} }
@Test
void uriTagValueIsRootWhenBestMatchingPatternIsEmpty() {
this.exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,
this.parser.parse(""));
this.exchange.getResponse().setStatusCode(HttpStatus.MOVED_PERMANENTLY);
Tag tag = WebFluxTags.uri(this.exchange);
assertThat(tag.getValue()).isEqualTo("root");
}
@Test @Test
void uriTagValueWithBestMatchingPatternAndIgnoreTrailingSlashRemoveTrailingSlash() { void uriTagValueWithBestMatchingPatternAndIgnoreTrailingSlashRemoveTrailingSlash() {
this.exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE, this.exchange.getAttributes().put(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,

Loading…
Cancel
Save