use of org.springframework.mock.web.MockHttpServletResponse in project opennms by OpenNMS.
the class AbstractSpringJerseyRestJsonTestCase method sendPost.
@Override
protected MockHttpServletResponse sendPost(String url, String xml, int statusCode, final String expectedUrlSuffix) throws Exception {
LOG.debug("POST {}, expected status code = {}, expected URL suffix = {}", url, statusCode, expectedUrlSuffix);
final MockHttpServletResponse response = sendData(POST, MediaType.APPLICATION_JSON, url, xml, statusCode);
if (expectedUrlSuffix != null) {
final Object header = response.getHeader("Location");
assertNotNull("Location header is null", header);
final String location = URLDecoder.decode(header.toString(), StandardCharsets.UTF_8.name());
final String decodedExpectedUrlSuffix = URLDecoder.decode(expectedUrlSuffix, StandardCharsets.UTF_8.name());
assertTrue("location '" + location + "' should end with '" + decodedExpectedUrlSuffix + "'", location.endsWith(decodedExpectedUrlSuffix));
}
return response;
}
use of org.springframework.mock.web.MockHttpServletResponse in project opennms by OpenNMS.
the class AbstractSpringJerseyRestTestCase method putXmlObject.
protected void putXmlObject(final JAXBContext context, final String url, final int expectedStatus, final Object object) throws Exception {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final Marshaller marshaller = context.createMarshaller();
marshaller.marshal(object, out);
final byte[] content = out.toByteArray();
final MockHttpServletRequest request = createRequest(servletContext, PUT, url, getUser(), getUserRoles());
request.setContentType(MediaType.APPLICATION_XML);
request.setContent(content);
final MockHttpServletResponse response = createResponse();
dispatch(request, response);
assertEquals(expectedStatus, response.getStatus());
}
use of org.springframework.mock.web.MockHttpServletResponse in project ORCID-Source by ORCID.
the class OrcidUrlManagerTest method setUpSavedRequest.
private Pair<HttpServletRequest, HttpServletResponse> setUpSavedRequest(String savedUrl) throws URISyntaxException {
URI uri = new URI(savedUrl);
MockHttpServletRequest savedRequest = new MockHttpServletRequest("GET", uri.getPath());
savedRequest.setScheme(uri.getScheme());
savedRequest.setServerName(uri.getHost());
savedRequest.setQueryString(uri.getQuery());
MockHttpServletResponse savedResponse = new MockHttpServletResponse();
HttpSessionRequestCache sessionCache = new HttpSessionRequestCache();
sessionCache.saveRequest(savedRequest, savedResponse);
MockHttpServletRequest currentRequest = new MockHttpServletRequest();
currentRequest.setSession(savedRequest.getSession());
MockHttpServletResponse currentResponse = new MockHttpServletResponse();
return new ImmutablePair<>(currentRequest, currentResponse);
}
use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.
the class WebRequestTraceFilterTests method filterHas500ResponseStatusWhenExceptionIsThrown.
@Test
@SuppressWarnings("unchecked")
public void filterHas500ResponseStatusWhenExceptionIsThrown() throws ServletException, IOException {
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
try {
this.filter.doFilterInternal(request, response, new FilterChain() {
@Override
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
throw new RuntimeException();
}
});
fail("Exception was swallowed");
} catch (RuntimeException ex) {
Map<String, Object> headers = (Map<String, Object>) this.repository.findAll().iterator().next().getInfo().get("headers");
Map<String, Object> responseHeaders = (Map<String, Object>) headers.get("response");
assertThat((String) responseHeaders.get("status")).isEqualTo("500");
}
}
use of org.springframework.mock.web.MockHttpServletResponse in project spring-boot by spring-projects.
the class WebRequestTraceFilterTests method filterDoesNotAddResponseCookiesWithCookiesExclude.
@Test
@SuppressWarnings({ "unchecked" })
public void filterDoesNotAddResponseCookiesWithCookiesExclude() throws ServletException, IOException {
this.properties.setInclude(Collections.singleton(Include.RESPONSE_HEADERS));
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
response.addHeader("Content-Type", "application/json");
response.addHeader("Set-Cookie", "testCookie=testValue;");
Map<String, Object> trace = this.filter.getTrace(request);
this.filter.enhanceTrace(trace, response);
Map<String, Object> map = (Map<String, Object>) trace.get("headers");
assertThat(map.get("response").toString()).isEqualTo("{Content-Type=application/json, status=200}");
}
Aggregations