use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DispatcherServletTests method existingMultipartRequestButWrapped.
@Test
public void existingMultipartRequestButWrapped() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do;abc=def");
request.addPreferredLocale(Locale.CANADA);
request.addUserRole("role1");
MockHttpServletResponse response = new MockHttpServletResponse();
ComplexWebApplicationContext.MockMultipartResolver multipartResolver = (ComplexWebApplicationContext.MockMultipartResolver) complexDispatcherServlet.getWebApplicationContext().getBean("multipartResolver");
MultipartHttpServletRequest multipartRequest = multipartResolver.resolveMultipart(request);
complexDispatcherServlet.service(new HttpServletRequestWrapper(multipartRequest), response);
multipartResolver.cleanupMultipart(multipartRequest);
assertThat(request.getAttribute(SimpleMappingExceptionResolver.DEFAULT_EXCEPTION_ATTRIBUTE)).isNull();
assertThat(request.getAttribute("cleanedUp")).isNotNull();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DispatcherServletTests method simpleMappingExceptionResolverWithAllHandlers1.
@Test
public void simpleMappingExceptionResolverWithAllHandlers1() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do");
request.addPreferredLocale(Locale.CANADA);
request.addUserRole("role1");
request.addParameter("access", "yes");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getStatus()).isEqualTo(500);
assertThat(response.getForwardedUrl()).as("forwarded to failed").isEqualTo("failed1.jsp");
assertThat(request.getAttribute("exception") instanceof IllegalAccessException).as("Exception exposed").isTrue();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DispatcherServletTests method withNoViewNested.
@Test
public void withNoViewNested() throws Exception {
MockServletContext servletContext = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/noview/simple.do");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getForwardedUrl()).isEqualTo("noview/simple.jsp");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class DispatcherServletTests method notDetectAllHandlerAdapters.
@Test
public void notDetectAllHandlerAdapters() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.setDetectAllHandlerAdapters(false);
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
// only ServletHandlerAdapter with bean name "handlerAdapter" detected
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/servlet.do");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getContentAsString()).isEqualTo("body");
// SimpleControllerHandlerAdapter not detected
request = new MockHttpServletRequest(getServletContext(), "GET", "/form.do");
response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getForwardedUrl()).as("forwarded to failed").isEqualTo("failed0.jsp");
assertThat(request.getAttribute("exception").getClass().equals(ServletException.class)).as("Exception exposed").isTrue();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.
the class ObjectToStringHttpMessageConverterTests method read.
@Test
public void read() throws IOException {
Short shortValue = Short.valueOf((short) 781);
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
request.setContent(shortValue.toString().getBytes(StringHttpMessageConverter.DEFAULT_CHARSET));
assertThat(this.converter.read(Short.class, new ServletServerHttpRequest(request))).isEqualTo(shortValue);
Float floatValue = Float.valueOf(123);
request = new MockHttpServletRequest();
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
request.setCharacterEncoding("UTF-16");
request.setContent(floatValue.toString().getBytes("UTF-16"));
assertThat(this.converter.read(Float.class, new ServletServerHttpRequest(request))).isEqualTo(floatValue);
Long longValue = Long.valueOf(55819182821331L);
request = new MockHttpServletRequest();
request.setContentType(MediaType.TEXT_PLAIN_VALUE);
request.setCharacterEncoding("UTF-8");
request.setContent(longValue.toString().getBytes("UTF-8"));
assertThat(this.converter.read(Long.class, new ServletServerHttpRequest(request))).isEqualTo(longValue);
}
Aggregations