use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.
the class WebRequestTraceFilterTests method filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude.
@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddAuthorizationHeaderWithoutAuthorizationHeaderInclude() throws ServletException, IOException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
request.addHeader("Authorization", "my-auth-header");
MockHttpServletResponse response = new MockHttpServletResponse();
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");
assertThat(((Map<Object, Object>) headers.get("request"))).hasSize(0);
}
use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.
the class WebRequestTraceFilterTests method filterDoesNotAddResponseHeadersWithoutResponseHeadersInclude.
@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");
assertThat(headers.get("response") == null).isTrue();
}
use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.
the class WebRequestTraceFilterTests method filterHasResponseStatus.
@Test
public void filterHasResponseStatus() {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
response.setStatus(404);
response.addHeader("Content-Type", "application/json");
Map<String, Object> trace = this.filter.getTrace(request);
this.filter.enhanceTrace(trace, response);
@SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) trace.get("headers")).get("response");
assertThat(map.get("status").toString()).isEqualTo("404");
}
use of org.springframework.mock.web.MockHttpServletResponse in project spring-framework by spring-projects.
the class MockHttpServletRequestBuilder method buildRequest.
/**
* Build a {@link MockHttpServletRequest}.
*/
@Override
public final MockHttpServletRequest buildRequest(ServletContext servletContext) {
MockHttpServletRequest request = createServletRequest(servletContext);
request.setAsyncSupported(true);
request.setMethod(this.method);
String requestUri = this.url.getRawPath();
request.setRequestURI(requestUri);
if (this.url.getScheme() != null) {
request.setScheme(this.url.getScheme());
}
if (this.url.getHost() != null) {
request.setServerName(this.url.getHost());
}
if (this.url.getPort() != -1) {
request.setServerPort(this.url.getPort());
}
updatePathRequestProperties(request, requestUri);
if (this.secure != null) {
request.setSecure(this.secure);
}
if (this.principal != null) {
request.setUserPrincipal(this.principal);
}
if (this.session != null) {
request.setSession(this.session);
}
request.setCharacterEncoding(this.characterEncoding);
request.setContent(this.content);
request.setContentType(this.contentType);
for (String name : this.headers.keySet()) {
for (Object value : this.headers.get(name)) {
request.addHeader(name, value);
}
}
if (this.url.getRawQuery() != null) {
request.setQueryString(this.url.getRawQuery());
}
addRequestParams(request, UriComponentsBuilder.fromUri(this.url).build().getQueryParams());
for (String name : this.parameters.keySet()) {
for (String value : this.parameters.get(name)) {
request.addParameter(name, value);
}
}
if (this.content != null && this.content.length > 0) {
String requestContentType = request.getContentType();
if (requestContentType != null) {
MediaType mediaType = MediaType.parseMediaType(requestContentType);
if (MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType)) {
addRequestParams(request, parseFormData(mediaType));
}
}
}
if (!ObjectUtils.isEmpty(this.cookies)) {
request.setCookies(this.cookies.toArray(new Cookie[this.cookies.size()]));
}
if (!ObjectUtils.isEmpty(this.locales)) {
request.setPreferredLocales(this.locales);
}
for (String name : this.requestAttributes.keySet()) {
request.setAttribute(name, this.requestAttributes.get(name));
}
for (String name : this.sessionAttributes.keySet()) {
request.getSession().setAttribute(name, this.sessionAttributes.get(name));
}
FlashMap flashMap = new FlashMap();
flashMap.putAll(this.flashAttributes);
FlashMapManager flashMapManager = getFlashMapManager(request);
flashMapManager.saveOutputFlashMap(flashMap, request, new MockHttpServletResponse());
return request;
}
use of org.springframework.mock.web.MockHttpServletResponse in project spring-framework by spring-projects.
the class MockMvcClientHttpRequestFactory method createRequest.
@Override
public ClientHttpRequest createRequest(final URI uri, final HttpMethod httpMethod) throws IOException {
return new MockClientHttpRequest(httpMethod, uri) {
@Override
public ClientHttpResponse executeInternal() throws IOException {
try {
MockHttpServletRequestBuilder requestBuilder = request(httpMethod, uri);
requestBuilder.content(getBodyAsBytes());
requestBuilder.headers(getHeaders());
MvcResult mvcResult = MockMvcClientHttpRequestFactory.this.mockMvc.perform(requestBuilder).andReturn();
MockHttpServletResponse servletResponse = mvcResult.getResponse();
HttpStatus status = HttpStatus.valueOf(servletResponse.getStatus());
byte[] body = servletResponse.getContentAsByteArray();
HttpHeaders headers = getResponseHeaders(servletResponse);
MockClientHttpResponse clientResponse = new MockClientHttpResponse(body, status);
clientResponse.getHeaders().putAll(headers);
return clientResponse;
} catch (Exception ex) {
byte[] body = ex.toString().getBytes(StandardCharsets.UTF_8);
return new MockClientHttpResponse(body, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
};
}
Aggregations