Search in sources :

Example 81 with MockHttpServletRequest

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();
}
Also used : HttpServletRequestWrapper(jakarta.servlet.http.HttpServletRequestWrapper) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) Test(org.junit.jupiter.api.Test)

Example 82 with MockHttpServletRequest

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();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 83 with MockHttpServletRequest

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");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 84 with MockHttpServletRequest

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();
}
Also used : ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 85 with MockHttpServletRequest

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);
}
Also used : ServletServerHttpRequest(org.springframework.http.server.ServletServerHttpRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) Test(org.junit.jupiter.api.Test)

Aggregations

MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)752 Test (org.junit.jupiter.api.Test)458 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)359 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)180 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)83 BeforeEach (org.junit.jupiter.api.BeforeEach)73 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)57 ModelAndView (org.springframework.web.servlet.ModelAndView)45 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)45 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)39 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)38 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)38 TestBean (org.springframework.beans.testfixture.beans.TestBean)36 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)35 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)31 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)29 Cookie (jakarta.servlet.http.Cookie)27 HttpRequest (org.springframework.http.HttpRequest)27 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)26 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)25