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);
}
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();
}
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);
}
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();
}
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");
}
Aggregations