use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ServletContextAwareProcessorTests method servletConfigAwareWithNullServletContextAndNonNullServletConfig.
@Test
public void servletConfigAwareWithNullServletContextAndNonNullServletConfig() {
ServletContext servletContext = new MockServletContext();
ServletConfig servletConfig = new MockServletConfig(servletContext);
ServletContextAwareProcessor processor = new ServletContextAwareProcessor(null, servletConfig);
ServletConfigAwareBean bean = new ServletConfigAwareBean();
assertThat(bean.getServletConfig()).isNull();
processor.postProcessBeforeInitialization(bean, "testBean");
assertThat(bean.getServletConfig()).as("ServletConfig should have been set").isNotNull();
assertThat(bean.getServletConfig()).isEqualTo(servletConfig);
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class HttpRequestHandlerTests method testHttpRequestHandlerServletPassThrough.
@Test
public void testHttpRequestHandlerServletPassThrough() throws Exception {
MockServletContext servletContext = new MockServletContext();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
StaticWebApplicationContext wac = new StaticWebApplicationContext();
wac.getBeanFactory().registerSingleton("myHandler", (HttpRequestHandler) (req, res) -> {
assertThat(req).isSameAs(request);
assertThat(res).isSameAs(response);
String exception = request.getParameter("exception");
if ("ServletException".equals(exception)) {
throw new ServletException("test");
}
if ("IOException".equals(exception)) {
throw new IOException("test");
}
res.getWriter().write("myResponse");
});
wac.setServletContext(servletContext);
wac.refresh();
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, wac);
Servlet servlet = new HttpRequestHandlerServlet();
servlet.init(new MockServletConfig(servletContext, "myHandler"));
servlet.service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myResponse");
request.setParameter("exception", "ServletException");
assertThatExceptionOfType(ServletException.class).isThrownBy(() -> servlet.service(request, response)).withMessage("test");
request.setParameter("exception", "IOException");
assertThatIOException().isThrownBy(() -> servlet.service(request, response)).withMessage("test");
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class AbstractServletHandlerMethodTests method initDispatcherServlet.
@SuppressWarnings("serial")
WebApplicationContext initDispatcherServlet(@Nullable Class<?> controllerClass, boolean usePathPatterns, @Nullable ApplicationContextInitializer<GenericWebApplicationContext> initializer) throws ServletException {
final GenericWebApplicationContext wac = new GenericWebApplicationContext();
servlet = new DispatcherServlet() {
@Override
protected WebApplicationContext createWebApplicationContext(@Nullable WebApplicationContext parent) {
if (controllerClass != null) {
wac.registerBeanDefinition(controllerClass.getSimpleName(), new RootBeanDefinition(controllerClass));
}
if (initializer != null) {
initializer.initialize(wac);
}
if (!wac.containsBeanDefinition("handlerMapping")) {
BeanDefinition def = register("handlerMapping", RequestMappingHandlerMapping.class, wac);
def.getPropertyValues().add("removeSemicolonContent", "false");
}
BeanDefinition mappingDef = wac.getBeanDefinition("handlerMapping");
if (usePathPatterns && !mappingDef.hasAttribute("patternParser")) {
BeanDefinition parserDef = register("parser", PathPatternParser.class, wac);
mappingDef.getPropertyValues().add("patternParser", parserDef);
}
register("handlerAdapter", RequestMappingHandlerAdapter.class, wac);
register("requestMappingResolver", ExceptionHandlerExceptionResolver.class, wac);
register("responseStatusResolver", ResponseStatusExceptionResolver.class, wac);
register("defaultResolver", DefaultHandlerExceptionResolver.class, wac);
wac.refresh();
return wac;
}
};
servlet.init(new MockServletConfig());
return wac;
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ResponseEntityExceptionHandlerTests method controllerAdviceWithinDispatcherServlet.
@Test
public void controllerAdviceWithinDispatcherServlet() throws Exception {
StaticWebApplicationContext ctx = new StaticWebApplicationContext();
ctx.registerSingleton("controller", ExceptionThrowingController.class);
ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
ctx.refresh();
DispatcherServlet servlet = new DispatcherServlet(ctx);
servlet.init(new MockServletConfig());
servlet.service(this.servletRequest, this.servletResponse);
assertThat(this.servletResponse.getStatus()).isEqualTo(400);
assertThat(this.servletResponse.getContentAsString()).isEqualTo("error content");
assertThat(this.servletResponse.getHeader("someHeader")).isEqualTo("someHeaderValue");
}
use of org.springframework.web.testfixture.servlet.MockServletConfig in project spring-framework by spring-projects.
the class ServletAnnotationControllerHandlerMethodTests method nullCommandController.
@PathPatternsParameterizedTest
void nullCommandController(boolean usePathPatterns) throws Exception {
initDispatcherServlet(MyNullCommandController.class, usePathPatterns);
getServlet().init(new MockServletConfig());
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myPath");
request.setUserPrincipal(new OtherPrincipal());
MockHttpServletResponse response = new MockHttpServletResponse();
getServlet().service(request, response);
assertThat(response.getContentAsString()).isEqualTo("myView");
}
Aggregations