Merge pull request #4242 from izeye/trace-response

* pr/4242:
  Make response headers optional in /trace endpoint
pull/4189/merge
Phillip Webb 9 years ago
commit fb63e887bb

@ -170,8 +170,10 @@ public class WebRequestTraceFilter extends OncePerRequestFilter implements Order
@SuppressWarnings("unchecked")
protected void enhanceTrace(Map<String, Object> trace, HttpServletResponse response) {
Map<String, Object> headers = (Map<String, Object>) trace.get("headers");
headers.put("response", getResponseHeaders(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) {

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