Search in sources :

Example 51 with MockHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.

the class DispatcherServletTests method parsedRequestPathIsRestoredOnForward.

// gh-26318
@Test
public void parsedRequestPathIsRestoredOnForward() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.register(PathPatternParserConfig.class);
    DispatcherServlet servlet = new DispatcherServlet(context);
    servlet.init(servletConfig);
    RequestPath previousRequestPath = RequestPath.parse("/", null);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/test");
    request.setDispatcherType(DispatcherType.FORWARD);
    request.setAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE, previousRequestPath);
    MockHttpServletResponse response = new MockHttpServletResponse();
    servlet.service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getContentAsString()).isEqualTo("test-body");
    assertThat(request.getAttribute(ServletRequestPathUtils.PATH_ATTRIBUTE)).isSameAs(previousRequestPath);
}
Also used : RequestPath(org.springframework.http.server.RequestPath) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 52 with MockHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.

the class DispatcherServletTests method cleanupAfterIncludeWithRemove.

@Test
public void cleanupAfterIncludeWithRemove() throws ServletException, IOException {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/main.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    request.setAttribute("test1", "value1");
    request.setAttribute("test2", "value2");
    WebApplicationContext wac = new StaticWebApplicationContext();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
    request.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/form.do");
    simpleDispatcherServlet.service(request, response);
    assertThat(request.getAttribute("test1")).isEqualTo("value1");
    assertThat(request.getAttribute("test2")).isEqualTo("value2");
    assertThat(request.getAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE)).isEqualTo(wac);
    assertThat(request.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isNull();
    assertThat(request.getAttribute("command")).isNull();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ConfigurableWebApplicationContext(org.springframework.web.context.ConfigurableWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 53 with MockHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.

the class DispatcherServletTests method publishEventsOff.

@Test
public void publishEventsOff() throws Exception {
    complexDispatcherServlet.setPublishEvents(false);
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
    MockHttpServletResponse response = new MockHttpServletResponse();
    complexDispatcherServlet.service(request, response);
    ComplexWebApplicationContext.TestApplicationListener listener = (ComplexWebApplicationContext.TestApplicationListener) complexDispatcherServlet.getWebApplicationContext().getBean("testListener");
    assertThat(listener.counter).isEqualTo(0);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 54 with MockHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.

the class DispatcherServletTests method simpleMappingExceptionResolverWithAllHandlers2.

@Test
public void simpleMappingExceptionResolverWithAllHandlers2() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/loc.do");
    request.addPreferredLocale(Locale.CANADA);
    request.addUserRole("role1");
    request.addParameter("servlet", "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 ServletException).as("Exception exposed").isTrue();
}
Also used : ServletException(jakarta.servlet.ServletException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 55 with MockHttpServletRequest

use of org.springframework.web.testfixture.servlet.MockHttpServletRequest in project spring-framework by spring-projects.

the class DispatcherServletTests method allowedOptionsIncludesPatchMethod.

@Test
public void allowedOptionsIncludesPatchMethod() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "OPTIONS", "/foo");
    MockHttpServletResponse response = spy(new MockHttpServletResponse());
    DispatcherServlet servlet = new DispatcherServlet();
    servlet.setDispatchOptionsRequest(false);
    servlet.service(request, response);
    // SPR-10341
    verify(response, never()).getHeader(anyString());
    assertThat(response.getHeader("Allow")).isEqualTo("GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) 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