Make response headers optional in /trace endpoint

Update `WebRequestTraceFilter` so that the response respects the
TraceProperties.Include.RESPONSE enum.

See gh-3948
Closes gh-4242
pull/4189/merge
Johnny Lim 9 years ago committed by Phillip Webb
parent eeb407881a
commit 71c8a114f1

@ -170,9 +170,11 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
@SuppressWarnings("unchecked")
protected void enhanceTrace(Map<String, Object> trace, HttpServletResponse response) {
if (isIncluded(Include.RESPONSE_HEADERS)) {
Map<String, Object> headers = (Map<String, Object>) trace.get("headers");
headers.put("response", getResponseHeaders(response));
}
}
private Map<String, String> getResponseHeaders(HttpServletResponse response) {
Map<String, String> headers = new LinkedHashMap<String, String>();

@ -20,6 +20,7 @@ import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.security.Principal;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Map;
@ -37,6 +38,7 @@ import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* Tests for {@link WebRequestTraceFilter}.
@ -126,6 +128,25 @@ public class WebRequestTraceFilterTests {
assertEquals("{Accept=application/json}", map.get("request").toString());
}
@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude()
throws ServletException, IOException {
this.properties.setInclude(Collections.singleton(Include.REQUEST_HEADERS));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/json");
this.filter.doFilterInternal(request, response, new FilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response)
throws IOException, ServletException {
}
});
Map<String, Object> info = this.repository.findAll().iterator().next().getInfo();
Map<String, Object> headers = (Map<String, Object>) info.get("headers");
assertTrue(headers.get("response") == null);
}
@Test
public void filterHasResponseStatus() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
@ -156,4 +177,5 @@ public class WebRequestTraceFilterTests {
System.err.println(map);
assertEquals("Foo", map.get("message").toString());
}
}

Loading…
Cancel
Save