Browse Source

Cleanup URLs before using them for metrics

Update `WebMvcTags` to cleanup URLs by removing any double
slashes and any trailing slash.

Fixes gh-11808
pull/11805/merge
Jon Schneider 8 years ago committed by Phillip Webb
parent
commit
112ffd7890
  1. 3
      spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java
  2. 12
      spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java

3
spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/servlet/WebMvcTags.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -84,6 +84,7 @@ public final class WebMvcTags { @@ -84,6 +84,7 @@ public final class WebMvcTags {
if (!StringUtils.hasText(uri)) {
uri = "/";
}
uri = uri.replaceAll("//+", "/").replaceAll("/$", "");
return Tag.of("uri", uri.isEmpty() ? "root" : uri);
}

12
spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/web/servlet/WebMvcTagsTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -22,6 +22,7 @@ import org.junit.Test; @@ -22,6 +22,7 @@ import org.junit.Test;
import org.springframework.boot.actuate.metrics.web.servlet.WebMvcTags;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.HandlerMapping;
import static org.assertj.core.api.Assertions.assertThat;
@ -36,6 +37,13 @@ public class WebMvcTagsTests { @@ -36,6 +37,13 @@ public class WebMvcTagsTests {
private final MockHttpServletResponse response = new MockHttpServletResponse();
@Test
public void uriTrailingSlashesAreSuppressed() {
this.request.setAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE,
"//foo/");
assertThat(WebMvcTags.uri(this.request, null).getValue()).isEqualTo("/foo");
}
@Test
public void uriTagValueIsRedirectionWhenResponseStatusIs3xx() {
this.response.setStatus(301);
@ -54,7 +62,7 @@ public class WebMvcTagsTests { @@ -54,7 +62,7 @@ public class WebMvcTagsTests {
public void uriTagToleratesCustomResponseStatus() {
this.response.setStatus(601);
Tag tag = WebMvcTags.uri(this.request, this.response);
assertThat(tag.getValue()).isEqualTo("/");
assertThat(tag.getValue()).isEqualTo("root");
}
}

Loading…
Cancel
Save