Move configuration of TraceEndpoint

See gh-10263
pull/10278/merge
Stephane Nicoll 7 years ago
parent 9234801c0a
commit 0d62b0cb3c

@ -0,0 +1,52 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.autoconfigure.trace;
import java.util.HashSet;
import java.util.Set;
import org.springframework.boot.actuate.trace.Include;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for tracing.
*
* @author Wallace Wadge
* @author Phillip Webb
* @author Venil Noronha
* @author Madhura Bhave
* @author Stephane Nicoll
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "management.trace")
public class TraceEndpointProperties {
/**
* Items to be included in the trace. Defaults to request/response headers (including
* cookies) and errors.
*/
private Set<Include> include = new HashSet<>(Include.defaultIncludes());
public Set<Include> getInclude() {
return this.include;
}
public void setInclude(Set<Include> include) {
this.include = include;
}
}

@ -19,9 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.trace;
import javax.servlet.Servlet; import javax.servlet.Servlet;
import javax.servlet.ServletRegistration; import javax.servlet.ServletRegistration;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.actuate.trace.TraceProperties;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.boot.actuate.trace.WebRequestTraceFilter;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -46,28 +44,28 @@ import org.springframework.web.servlet.DispatcherServlet;
@ConditionalOnClass({ Servlet.class, DispatcherServlet.class, ServletRegistration.class }) @ConditionalOnClass({ Servlet.class, DispatcherServlet.class, ServletRegistration.class })
@AutoConfigureAfter(TraceRepositoryAutoConfiguration.class) @AutoConfigureAfter(TraceRepositoryAutoConfiguration.class)
@ConditionalOnProperty(prefix = "management.trace.filter", name = "enabled", matchIfMissing = true) @ConditionalOnProperty(prefix = "management.trace.filter", name = "enabled", matchIfMissing = true)
@EnableConfigurationProperties(TraceProperties.class) @EnableConfigurationProperties(TraceEndpointProperties.class)
public class TraceWebFilterAutoConfiguration { public class TraceWebFilterAutoConfiguration {
private final TraceRepository traceRepository; private final TraceRepository traceRepository;
private final TraceProperties traceProperties; private final TraceEndpointProperties endpointProperties;
private final ErrorAttributes errorAttributes; private final ErrorAttributes errorAttributes;
public TraceWebFilterAutoConfiguration(TraceRepository traceRepository, public TraceWebFilterAutoConfiguration(TraceRepository traceRepository,
TraceProperties traceProperties, TraceEndpointProperties endpointProperties,
ObjectProvider<ErrorAttributes> errorAttributes) { ObjectProvider<ErrorAttributes> errorAttributes) {
this.traceRepository = traceRepository; this.traceRepository = traceRepository;
this.traceProperties = traceProperties; this.endpointProperties = endpointProperties;
this.errorAttributes = errorAttributes.getIfAvailable(); this.errorAttributes = errorAttributes.getIfAvailable();
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public WebRequestTraceFilter webRequestLoggingFilter(BeanFactory beanFactory) { public WebRequestTraceFilter webRequestLoggingFilter() {
WebRequestTraceFilter filter = new WebRequestTraceFilter(this.traceRepository, WebRequestTraceFilter filter = new WebRequestTraceFilter(this.traceRepository,
this.traceProperties); this.endpointProperties.getInclude());
if (this.errorAttributes != null) { if (this.errorAttributes != null) {
filter.setErrorAttributes(this.errorAttributes); filter.setErrorAttributes(this.errorAttributes);
} }

@ -21,7 +21,6 @@ import java.util.Map;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.trace.TraceProperties;
import org.springframework.boot.actuate.trace.TraceRepository; import org.springframework.boot.actuate.trace.TraceRepository;
import org.springframework.boot.actuate.trace.WebRequestTraceFilter; import org.springframework.boot.actuate.trace.WebRequestTraceFilter;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
@ -91,7 +90,7 @@ public class TraceWebFilterAutoConfigurationTests {
@Bean @Bean
public TestWebRequestTraceFilter testWebRequestTraceFilter( public TestWebRequestTraceFilter testWebRequestTraceFilter(
TraceRepository repository, TraceProperties properties) { TraceRepository repository, TraceEndpointProperties properties) {
return new TestWebRequestTraceFilter(repository, properties); return new TestWebRequestTraceFilter(repository, properties);
} }
@ -100,8 +99,8 @@ public class TraceWebFilterAutoConfigurationTests {
static class TestWebRequestTraceFilter extends WebRequestTraceFilter { static class TestWebRequestTraceFilter extends WebRequestTraceFilter {
TestWebRequestTraceFilter(TraceRepository repository, TestWebRequestTraceFilter(TraceRepository repository,
TraceProperties properties) { TraceEndpointProperties properties) {
super(repository, properties); super(repository, properties.getInclude());
} }
@Override @Override

@ -0,0 +1,131 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.trace;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
/**
* Include options for tracing.
*
* @author Wallace Wadge
* @since 2.0.0
*/
public enum Include {
/**
* Include request headers.
*/
REQUEST_HEADERS,
/**
* Include response headers.
*/
RESPONSE_HEADERS,
/**
* Include "Cookie" in request and "Set-Cookie" in response headers.
*/
COOKIES,
/**
* Include authorization header (if any).
*/
AUTHORIZATION_HEADER,
/**
* Include errors (if any).
*/
ERRORS,
/**
* Include path info.
*/
PATH_INFO,
/**
* Include the translated path.
*/
PATH_TRANSLATED,
/**
* Include the context path.
*/
CONTEXT_PATH,
/**
* Include the user principal.
*/
USER_PRINCIPAL,
/**
* Include the parameters.
*/
PARAMETERS,
/**
* Include the query string.
*/
QUERY_STRING,
/**
* Include the authentication type.
*/
AUTH_TYPE,
/**
* Include the remote address.
*/
REMOTE_ADDRESS,
/**
* Include the session ID.
*/
SESSION_ID,
/**
* Include the remote user.
*/
REMOTE_USER,
/**
* Include the time taken to service the request in milliseconds.
*/
TIME_TAKEN;
private static final Set<Include> DEFAULT_INCLUDES;
static {
Set<Include> defaultIncludes = new LinkedHashSet<>();
defaultIncludes.add(Include.REQUEST_HEADERS);
defaultIncludes.add(Include.RESPONSE_HEADERS);
defaultIncludes.add(Include.COOKIES);
defaultIncludes.add(Include.ERRORS);
defaultIncludes.add(Include.TIME_TAKEN);
DEFAULT_INCLUDES = Collections.unmodifiableSet(defaultIncludes);
}
/**
* Return the default {@link Include}.
* @return the default include.
*/
public static Set<Include> defaultIncludes() {
return DEFAULT_INCLUDES;
}
}

@ -1,151 +0,0 @@
/*
* Copyright 2012-2017 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.trace;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Configuration properties for tracing.
*
* @author Wallace Wadge
* @author Phillip Webb
* @author Venil Noronha
* @author Madhura Bhave
* @since 1.3.0
*/
@ConfigurationProperties(prefix = "management.trace")
public class TraceProperties {
private static final Set<Include> DEFAULT_INCLUDES;
static {
Set<Include> defaultIncludes = new LinkedHashSet<>();
defaultIncludes.add(Include.REQUEST_HEADERS);
defaultIncludes.add(Include.RESPONSE_HEADERS);
defaultIncludes.add(Include.COOKIES);
defaultIncludes.add(Include.ERRORS);
defaultIncludes.add(Include.TIME_TAKEN);
DEFAULT_INCLUDES = Collections.unmodifiableSet(defaultIncludes);
}
/**
* Items to be included in the trace. Defaults to request/response headers (including
* cookies) and errors.
*/
private Set<Include> include = new HashSet<>(DEFAULT_INCLUDES);
public Set<Include> getInclude() {
return this.include;
}
public void setInclude(Set<Include> include) {
this.include = include;
}
/**
* Include options for tracing.
*/
public enum Include {
/**
* Include request headers.
*/
REQUEST_HEADERS,
/**
* Include response headers.
*/
RESPONSE_HEADERS,
/**
* Include "Cookie" in request and "Set-Cookie" in response headers.
*/
COOKIES,
/**
* Include authorization header (if any).
*/
AUTHORIZATION_HEADER,
/**
* Include errors (if any).
*/
ERRORS,
/**
* Include path info.
*/
PATH_INFO,
/**
* Include the translated path.
*/
PATH_TRANSLATED,
/**
* Include the context path.
*/
CONTEXT_PATH,
/**
* Include the user principal.
*/
USER_PRINCIPAL,
/**
* Include the parameters.
*/
PARAMETERS,
/**
* Include the query string.
*/
QUERY_STRING,
/**
* Include the authentication type.
*/
AUTH_TYPE,
/**
* Include the remote address.
*/
REMOTE_ADDRESS,
/**
* Include the session ID.
*/
SESSION_ID,
/**
* Include the remote user.
*/
REMOTE_USER,
/**
* Include the time taken to service the request in milliseconds.
*/
TIME_TAKEN
}
}

@ -38,7 +38,6 @@ import javax.servlet.http.HttpSession;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.boot.actuate.trace.TraceProperties.Include;
import org.springframework.boot.web.servlet.error.ErrorAttributes; import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.core.Ordered; import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -68,16 +67,26 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
private ErrorAttributes errorAttributes; private ErrorAttributes errorAttributes;
private final TraceProperties properties; private final Set<Include> includes;
/** /**
* Create a new {@link WebRequestTraceFilter} instance. * Create a new {@link WebRequestTraceFilter} instance.
* @param repository the trace repository * @param repository the trace repository
* @param properties the trace properties * @param includes the {@link Include} to apply
*/ */
public WebRequestTraceFilter(TraceRepository repository, TraceProperties properties) { public WebRequestTraceFilter(TraceRepository repository, Set<Include> includes) {
this.repository = repository; this.repository = repository;
this.properties = properties; this.includes = includes;
}
/**
* Create a new {@link WebRequestTraceFilter} instance with the default
* {@link Include} to apply.
* @param repository the trace repository
* @see Include#defaultIncludes()
*/
public WebRequestTraceFilter(TraceRepository repository) {
this(repository, Include.defaultIncludes());
} }
/** /**
@ -247,7 +256,7 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
} }
private boolean isIncluded(Include include) { private boolean isIncluded(Include include) {
return this.properties.getInclude().contains(include); return this.includes.contains(include);
} }
public void setErrorAttributes(ErrorAttributes errorAttributes) { public void setErrorAttributes(ErrorAttributes errorAttributes) {

@ -28,7 +28,6 @@ import javax.servlet.ServletException;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.actuate.trace.TraceProperties.Include;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes; import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.mock.web.MockFilterChain; import org.springframework.mock.web.MockFilterChain;
import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletRequest;
@ -55,17 +54,13 @@ public class WebRequestTraceFilterTests {
private final InMemoryTraceRepository repository = new InMemoryTraceRepository(); private final InMemoryTraceRepository repository = new InMemoryTraceRepository();
private TraceProperties properties = new TraceProperties();
private WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository,
this.properties);
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void filterAddsTraceWithDefaultIncludes() { public void filterAddsTraceWithDefaultIncludes() {
WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository);
MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo"));
request.addHeader("Accept", "application/json"); request.addHeader("Accept", "application/json");
Map<String, Object> trace = this.filter.getTrace(request); Map<String, Object> trace = filter.getTrace(request);
assertThat(trace.get("method")).isEqualTo("GET"); assertThat(trace.get("method")).isEqualTo("GET");
assertThat(trace.get("path")).isEqualTo("/foo"); assertThat(trace.get("path")).isEqualTo("/foo");
Map<String, Object> map = (Map<String, Object>) trace.get("headers"); Map<String, Object> map = (Map<String, Object>) trace.get("headers");
@ -76,7 +71,8 @@ public class WebRequestTraceFilterTests {
@Test @Test
@SuppressWarnings({ "rawtypes", "unchecked" }) @SuppressWarnings({ "rawtypes", "unchecked" })
public void filterAddsTraceWithCustomIncludes() throws IOException, ServletException { public void filterAddsTraceWithCustomIncludes() throws IOException, ServletException {
this.properties.setInclude(EnumSet.allOf(Include.class)); WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository,
EnumSet.allOf(Include.class));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.addHeader("Accept", "application/json"); request.addHeader("Accept", "application/json");
request.addHeader("Cookie", "testCookie=testValue;"); request.addHeader("Cookie", "testCookie=testValue;");
@ -95,7 +91,7 @@ public class WebRequestTraceFilterTests {
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/json"); response.addHeader("Content-Type", "application/json");
response.addHeader("Set-Cookie", "a=b"); response.addHeader("Set-Cookie", "a=b");
this.filter.doFilterInternal(request, response, (req, resp) -> { filter.doFilterInternal(request, response, (req, resp) -> {
BufferedReader bufferedReader = req.getReader(); BufferedReader bufferedReader = req.getReader();
while (bufferedReader.readLine() != null) { while (bufferedReader.readLine() != null) {
// read the contents as normal (forces cache to fill up) // read the contents as normal (forces cache to fill up)
@ -126,11 +122,12 @@ public class WebRequestTraceFilterTests {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude() public void filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude()
throws ServletException, IOException { throws ServletException, IOException {
this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS)); WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository,
Collections.singleton(Include.REQUEST_HEADERS));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/json"); response.addHeader("Content-Type", "application/json");
this.filter.doFilterInternal(request, response, (req, resp) -> { filter.doFilterInternal(request, response, (req, resp) -> {
}); });
Map<String, Object> info = this.repository.findAll().iterator().next().getInfo(); Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
Map<String, Object> headers = (Map<String, Object>) info.get("headers"); Map<String, Object> headers = (Map<String, Object>) info.get("headers");
@ -141,11 +138,12 @@ public class WebRequestTraceFilterTests {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public void filterDoesNotAddRequestCookiesWithCookiesExclude() public void filterDoesNotAddRequestCookiesWithCookiesExclude()
throws ServletException, IOException { throws ServletException, IOException {
this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS)); WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository,
Collections.singleton(Include.REQUEST_HEADERS));
MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo"));
request.addHeader("Accept", "application/json"); request.addHeader("Accept", "application/json");
request.addHeader("Cookie", "testCookie=testValue;"); request.addHeader("Cookie", "testCookie=testValue;");
Map<String, Object> map = (Map<String, Object>) this.filter.getTrace(request) Map<String, Object> map = (Map<String, Object>) filter.getTrace(request)
.get("headers"); .get("headers");
assertThat(map.get("request").toString()).isEqualTo("{Accept=application/json}"); assertThat(map.get("request").toString()).isEqualTo("{Accept=application/json}");
} }
@ -154,10 +152,11 @@ public class WebRequestTraceFilterTests {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public void filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude() public void filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude()
throws ServletException, IOException { throws ServletException, IOException {
WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.addHeader("Authorization", "my-auth-header"); request.addHeader("Authorization", "my-auth-header");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
this.filter.doFilterInternal(request, response, (req, resp) -> { filter.doFilterInternal(request, response, (req, resp) -> {
}); });
Map<String, Object> info = this.repository.findAll().iterator().next().getInfo(); Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
Map<String, Object> headers = (Map<String, Object>) info.get("headers"); Map<String, Object> headers = (Map<String, Object>) info.get("headers");
@ -168,12 +167,12 @@ public class WebRequestTraceFilterTests {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public void filterAddsAuthorizationHeaderWhenAuthorizationHeaderIncluded() public void filterAddsAuthorizationHeaderWhenAuthorizationHeaderIncluded()
throws ServletException, IOException { throws ServletException, IOException {
this.properties.setInclude( WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository,
EnumSet.of(Include.REQUEST_HEADERS, Include.AUTHORIZATION_HEADER)); EnumSet.of(Include.REQUEST_HEADERS, Include.AUTHORIZATION_HEADER));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.addHeader("Authorization", "my-auth-header"); request.addHeader("Authorization", "my-auth-header");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
this.filter.doFilterInternal(request, response, (req, resp) -> { filter.doFilterInternal(request, response, (req, resp) -> {
}); });
Map<String, Object> info = this.repository.findAll().iterator().next().getInfo(); Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
Map<String, Object> headers = (Map<String, Object>) info.get("headers"); Map<String, Object> headers = (Map<String, Object>) info.get("headers");
@ -185,13 +184,14 @@ public class WebRequestTraceFilterTests {
@SuppressWarnings({ "unchecked" }) @SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseCookiesWithCookiesExclude() public void filterDoesNotAddResponseCookiesWithCookiesExclude()
throws ServletException, IOException { throws ServletException, IOException {
this.properties.setInclude(Collections.singleton(Include.RESPONSE_HEADERS)); WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository,
Collections.singleton(Include.RESPONSE_HEADERS));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/json"); response.addHeader("Content-Type", "application/json");
response.addHeader("Set-Cookie", "testCookie=testValue;"); response.addHeader("Set-Cookie", "testCookie=testValue;");
Map<String, Object> trace = this.filter.getTrace(request); Map<String, Object> trace = filter.getTrace(request);
this.filter.enhanceTrace(trace, response); filter.enhanceTrace(trace, response);
Map<String, Object> map = (Map<String, Object>) trace.get("headers"); Map<String, Object> map = (Map<String, Object>) trace.get("headers");
assertThat(map.get("response").toString()) assertThat(map.get("response").toString())
.isEqualTo("{Content-Type=application/json, status=200}"); .isEqualTo("{Content-Type=application/json, status=200}");
@ -199,12 +199,13 @@ public class WebRequestTraceFilterTests {
@Test @Test
public void filterHasResponseStatus() { public void filterHasResponseStatus() {
WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
response.setStatus(404); response.setStatus(404);
response.addHeader("Content-Type", "application/json"); response.addHeader("Content-Type", "application/json");
Map<String, Object> trace = this.filter.getTrace(request); Map<String, Object> trace = filter.getTrace(request);
this.filter.enhanceTrace(trace, response); filter.enhanceTrace(trace, response);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) trace Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) trace
.get("headers")).get("response"); .get("headers")).get("response");
@ -213,10 +214,11 @@ public class WebRequestTraceFilterTests {
@Test @Test
public void filterAddsTimeTaken() throws Exception { public void filterAddsTimeTaken() throws Exception {
WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository);
MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo"));
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
MockFilterChain chain = new MockFilterChain(); MockFilterChain chain = new MockFilterChain();
this.filter.doFilter(request, response, chain); filter.doFilter(request, response, chain);
String timeTaken = (String) this.repository.findAll().iterator().next().getInfo() String timeTaken = (String) this.repository.findAll().iterator().next().getInfo()
.get("timeTaken"); .get("timeTaken");
assertThat(timeTaken).isNotNull(); assertThat(timeTaken).isNotNull();
@ -224,15 +226,16 @@ public class WebRequestTraceFilterTests {
@Test @Test
public void filterHasError() { public void filterHasError() {
this.filter.setErrorAttributes(new DefaultErrorAttributes()); WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository);
filter.setErrorAttributes(new DefaultErrorAttributes());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
response.setStatus(500); response.setStatus(500);
request.setAttribute("javax.servlet.error.exception", request.setAttribute("javax.servlet.error.exception",
new IllegalStateException("Foo")); new IllegalStateException("Foo"));
response.addHeader("Content-Type", "application/json"); response.addHeader("Content-Type", "application/json");
Map<String, Object> trace = this.filter.getTrace(request); Map<String, Object> trace = filter.getTrace(request);
this.filter.enhanceTrace(trace, response); filter.enhanceTrace(trace, response);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Map<String, Object> map = (Map<String, Object>) trace.get("error"); Map<String, Object> map = (Map<String, Object>) trace.get("error");
System.err.println(map); System.err.println(map);
@ -243,11 +246,12 @@ public class WebRequestTraceFilterTests {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void filterHas500ResponseStatusWhenExceptionIsThrown() public void filterHas500ResponseStatusWhenExceptionIsThrown()
throws ServletException, IOException { throws ServletException, IOException {
WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository);
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse(); MockHttpServletResponse response = new MockHttpServletResponse();
try { try {
this.filter.doFilterInternal(request, response, (req, resp) -> { filter.doFilterInternal(request, response, (req, resp) -> {
throw new RuntimeException(); throw new RuntimeException();
}); });
fail("Exception was swallowed"); fail("Exception was swallowed");
@ -264,7 +268,7 @@ public class WebRequestTraceFilterTests {
@Test @Test
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void postProcessRequestHeaders() throws Exception { public void postProcessRequestHeaders() throws Exception {
this.filter = new WebRequestTraceFilter(this.repository, this.properties) { WebRequestTraceFilter filter = new WebRequestTraceFilter(this.repository) {
@Override @Override
protected void postProcessRequestHeaders(Map<String, Object> headers) { protected void postProcessRequestHeaders(Map<String, Object> headers) {
@ -275,7 +279,7 @@ public class WebRequestTraceFilterTests {
MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo")); MockHttpServletRequest request = spy(new MockHttpServletRequest("GET", "/foo"));
request.addHeader("Accept", "application/json"); request.addHeader("Accept", "application/json");
request.addHeader("Test", "spring"); request.addHeader("Test", "spring");
Map<String, Object> map = (Map<String, Object>) this.filter.getTrace(request) Map<String, Object> map = (Map<String, Object>) filter.getTrace(request)
.get("headers"); .get("headers");
assertThat(map.get("request").toString()).isEqualTo("{Accept=application/json}"); assertThat(map.get("request").toString()).isEqualTo("{Accept=application/json}");
} }

Loading…
Cancel
Save