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 7 years ago committed by Phillip Webb
parent 1da0f2c6b0
commit 112ffd7890

@ -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 {
if (!StringUtils.hasText(uri)) {
uri = "/";
}
uri = uri.replaceAll("//+", "/").replaceAll("/$", "");
return Tag.of("uri", uri.isEmpty() ? "root" : uri);
}

@ -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;
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 {
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 {
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