use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method parameterDispatchingController.
@PathPatternsParameterizedTest
void parameterDispatchingController(boolean usePathPatterns) throws Exception {
final MockServletContext servletContext = new MockServletContext();
final MockServletConfig servletConfig = new MockServletConfig(servletContext);
WebApplicationContext webAppContext = initDispatcherServlet(MyParameterDispatchingController.class, usePathPatterns, wac -> {
wac.setServletContext(servletContext);
AnnotationConfigUtils.registerAnnotationConfigProcessors(wac);
wac.getBeanFactory().registerResolvableDependency(ServletConfig.class, servletConfig);
});
MockHttpServletRequest request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
MockHttpServletResponse response = new MockHttpServletResponse();
HttpSession session = request.getSession();
assertThat(session).isNotNull();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView");
assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
assertThat(request.getAttribute("locale")).isSameAs(request.getLocale());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
response = new MockHttpServletResponse();
session = request.getSession();
assertThat(session).isNotNull();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView");
assertThat(request.getAttribute("servletContext")).isSameAs(servletContext);
assertThat(request.getAttribute("servletConfig")).isSameAs(servletConfig);
assertThat(request.getAttribute("sessionId")).isSameAs(session.getId());
assertThat(request.getAttribute("requestUri")).isSameAs(request.getRequestURI());
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "other");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myOtherView");
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("view", "my");
request.addParameter("lang", "de");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myLangView");
request = new MockHttpServletRequest(servletContext, "GET", "/myPath.do");
request.addParameter("surprise", "!");
response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("mySurpriseView");
MyParameterDispatchingController deserialized = (MyParameterDispatchingController) SerializationTestUtils.serializeAndDeserialize(webAppContext.getBean(MyParameterDispatchingController.class.getSimpleName()));
assertThat(deserialized.request).isNotNull();
assertThat(deserialized.session).isNotNull();
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method routerFunction.
@Test
void routerFunction() throws ServletException, IOException {
GenericWebApplicationContext wac = new GenericWebApplicationContext();
wac.registerBean(RouterFunction.class, () -> RouterFunctions.route().GET("/foo", request -> ServerResponse.ok().body("foo-body")).build());
wac.refresh();
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(wac);
servlet.init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo");
MockHttpServletResponse response = new MockHttpServletResponse();
servlet.service(request, response);
assertThat(response.getStatus()).isEqualTo(200);
assertThat(response.getContentAsString()).isEqualTo("foo-body");
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method detectHandlerMappingFromParent.
@Test
public void detectHandlerMappingFromParent() throws ServletException, IOException {
// create a parent context that includes a mapping
StaticWebApplicationContext parent = new StaticWebApplicationContext();
parent.setServletContext(getServletContext());
parent.registerSingleton("parentHandler", ControllerFromParent.class, new MutablePropertyValues());
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.addPropertyValue(new PropertyValue("mappings", URL_KNOWN_ONLY_PARENT + "=parentHandler"));
parent.registerSingleton("parentMapping", SimpleUrlHandlerMapping.class, pvs);
parent.refresh();
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
// will have parent
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
ServletConfig config = new MockServletConfig(getServletContext(), "complex");
config.getServletContext().setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, parent);
complexDispatcherServlet.init(config);
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getStatus() == HttpServletResponse.SC_NOT_FOUND).as("Matched through parent controller/handler pair: not response=" + response.getStatus()).isFalse();
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method handlerNotMappedWithAutodetect.
@Test
public void handlerNotMappedWithAutodetect() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
// no parent
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", URL_KNOWN_ONLY_PARENT);
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class DispatcherServletTests method notDetectAllViewResolvers.
@Test
public void notDetectAllViewResolvers() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(ComplexWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.setDetectAllViewResolvers(false);
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown.do");
MockHttpServletResponse response = new MockHttpServletResponse();
assertThatExceptionOfType(ServletException.class).isThrownBy(() -> complexDispatcherServlet.service(request, response)).withMessageContaining("failed0");
}
Aggregations