Search in sources :

Example 1 with DispatcherServlet

use of cn.taketoday.web.servlet.DispatcherServlet in project today-infrastructure by TAKETODAY.

the class ResourceHttpRequestHandlerIntegrationTests method classpathLocationWithEncodedPath.

@ParameterizedTest
@MethodSource("argumentSource")
void classpathLocationWithEncodedPath(boolean usePathPatterns, String pathPrefix) throws Exception {
    MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo with spaces.css");
    MockHttpServletResponse response = new MockHttpServletResponse();
    DispatcherServlet servlet = initDispatcherServlet(WebConfig.class);
    servlet.service(request, response);
    String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix;
    assertThat(response.getStatus()).as(description).isEqualTo(200);
    assertThat(response.getContentType()).as(description).isEqualTo("text/css");
    assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }");
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 2 with DispatcherServlet

use of cn.taketoday.web.servlet.DispatcherServlet in project today-framework by TAKETODAY.

the class DispatcherServletInitializer method getServlet.

@Override
public DispatcherServlet getServlet() {
    DispatcherServlet dispatcherServlet = super.getServlet();
    if (dispatcherServlet == null && isAutoCreateDispatcher()) {
        WebServletApplicationContext context = getApplicationContext();
        BeanDefinitionRegistry registry = context.unwrapFactory(BeanDefinitionRegistry.class);
        if (!registry.containsBeanDefinition(DispatcherServlet.class)) {
            AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(context, registry);
            reader.setEnableConditionEvaluation(false);
            reader.registerBean(DISPATCHER_SERVLET, DispatcherServlet.class);
        }
        dispatcherServlet = context.getBean(DispatcherServlet.class);
        setServlet(dispatcherServlet);
    }
    return dispatcherServlet;
}
Also used : DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) BeanDefinitionRegistry(cn.taketoday.beans.factory.support.BeanDefinitionRegistry) AnnotatedBeanDefinitionReader(cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext)

Example 3 with DispatcherServlet

use of cn.taketoday.web.servlet.DispatcherServlet in project today-framework by TAKETODAY.

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.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
    ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.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");
}
Also used : DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) MockServletConfig(cn.taketoday.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 4 with DispatcherServlet

use of cn.taketoday.web.servlet.DispatcherServlet in project today-framework by TAKETODAY.

the class ResponseEntityExceptionHandlerTests method controllerAdviceWithNestedExceptionWithinDispatcherServlet.

@Test
public void controllerAdviceWithNestedExceptionWithinDispatcherServlet() throws Exception {
    StaticWebApplicationContext ctx = new StaticWebApplicationContext();
    ctx.registerSingleton("controller", NestedExceptionThrowingController.class);
    ctx.registerSingleton("exceptionHandler", ApplicationExceptionHandler.class);
    ctx.registerSingleton("parameterResolvingRegistry", ParameterResolvingRegistry.class);
    ctx.registerSingleton("returnValueHandlerManager", ReturnValueHandlerManager.class);
    ctx.refresh();
    DispatcherServlet servlet = new DispatcherServlet(ctx);
    servlet.init(new MockServletConfig());
    try {
        servlet.service(this.servletRequest, this.servletResponse);
    } catch (ServletException ex) {
        boolean condition1 = ex.getCause() instanceof IllegalStateException;
        assertThat(condition1).isTrue();
        boolean condition = ex.getCause().getCause() instanceof RequestBindingException;
        assertThat(condition).isTrue();
    }
}
Also used : ServletException(jakarta.servlet.ServletException) RequestBindingException(cn.taketoday.web.bind.RequestBindingException) DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) MockServletConfig(cn.taketoday.web.testfixture.servlet.MockServletConfig) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 5 with DispatcherServlet

use of cn.taketoday.web.servlet.DispatcherServlet in project today-framework by TAKETODAY.

the class ResourceHttpRequestHandlerIntegrationTests method cssFile.

@ParameterizedTest
@MethodSource("argumentSource")
void cssFile(boolean usePathPatterns, String pathPrefix) throws Exception {
    MockHttpServletRequest request = initRequest(pathPrefix + "/test/foo.css");
    MockHttpServletResponse response = new MockHttpServletResponse();
    DispatcherServlet servlet = initDispatcherServlet(WebConfig.class);
    servlet.service(request, response);
    String description = "usePathPattern=" + usePathPatterns + ", prefix=" + pathPrefix;
    assertThat(response.getStatus()).as(description).isEqualTo(200);
    assertThat(response.getContentType()).as(description).isEqualTo("text/css");
    assertThat(response.getContentAsString()).as(description).isEqualTo("h1 { color:red; }");
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) DispatcherServlet(cn.taketoday.web.servlet.DispatcherServlet) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

DispatcherServlet (cn.taketoday.web.servlet.DispatcherServlet)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 MethodSource (org.junit.jupiter.params.provider.MethodSource)4 AnnotationConfigServletWebApplicationContext (cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebApplicationContext)2 StaticWebApplicationContext (cn.taketoday.web.context.support.StaticWebApplicationContext)2 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)2 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)2 MockServletConfig (cn.taketoday.web.testfixture.servlet.MockServletConfig)2 Test (org.junit.jupiter.api.Test)2 BeanDefinitionRegistry (cn.taketoday.beans.factory.support.BeanDefinitionRegistry)1 AnnotatedBeanDefinitionReader (cn.taketoday.context.annotation.AnnotatedBeanDefinitionReader)1 RequestBindingException (cn.taketoday.web.bind.RequestBindingException)1 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)1 ServletException (jakarta.servlet.ServletException)1