Commit 112ffd78 authored by Jon Schneider's avatar Jon Schneider Committed by Phillip Webb

Cleanup URLs before using them for metrics

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

Fixes gh-11808
parent 1da0f2c6
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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)) { if (!StringUtils.hasText(uri)) {
uri = "/"; uri = "/";
} }
uri = uri.replaceAll("//+", "/").replaceAll("/$", "");
return Tag.of("uri", uri.isEmpty() ? "root" : uri); return Tag.of("uri", uri.isEmpty() ? "root" : uri);
} }
......
/* /*
* 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.boot.actuate.metrics.web.servlet.WebMvcTags;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.web.servlet.HandlerMapping;
import static org.assertj.core.api.Assertions.assertThat; 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(); 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 @Test
public void uriTagValueIsRedirectionWhenResponseStatusIs3xx() { public void uriTagValueIsRedirectionWhenResponseStatusIs3xx() {
this.response.setStatus(301); this.response.setStatus(301);
...@@ -54,7 +62,7 @@ public class WebMvcTagsTests { ...@@ -54,7 +62,7 @@ public class WebMvcTagsTests {
public void uriTagToleratesCustomResponseStatus() { public void uriTagToleratesCustomResponseStatus() {
this.response.setStatus(601); this.response.setStatus(601);
Tag tag = WebMvcTags.uri(this.request, this.response); Tag tag = WebMvcTags.uri(this.request, this.response);
assertThat(tag.getValue()).isEqualTo("/"); assertThat(tag.getValue()).isEqualTo("root");
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment