Search in sources :

Example 1 with StaticWebApplicationContext

use of cn.taketoday.web.context.support.StaticWebApplicationContext in project today-infrastructure by TAKETODAY.

the class ResourceHttpRequestHandlerTests method servletContextRootValidation.

@Test
public void servletContextRootValidation() {
    StaticWebApplicationContext context = new StaticWebApplicationContext() {

        @Override
        public Resource getResource(String location) {
            return new FileSystemResource("/");
        }
    };
    ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
    handler.setLocationValues(Collections.singletonList("/"));
    handler.setApplicationContext(context);
    assertThatIllegalStateException().isThrownBy(handler::afterPropertiesSet).withMessage("The String-based location \"/\" should be relative to the web application root but " + "resolved to a Resource of type: class cn.taketoday.core.io.FileSystemResource. " + "If this is intentional, please pass it as a pre-configured Resource via setLocations.");
}
Also used : StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) FileSystemResource(cn.taketoday.core.io.FileSystemResource) Test(org.junit.jupiter.api.Test)

Example 2 with StaticWebApplicationContext

use of cn.taketoday.web.context.support.StaticWebApplicationContext in project today-infrastructure by TAKETODAY.

the class ControllerTests method parameterizableViewController.

@Test
public void parameterizableViewController() throws Exception {
    String viewName = "viewName";
    ParameterizableViewController pvc = new ParameterizableViewController();
    pvc.setViewName(viewName);
    // We don't care about the params.
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.refresh();
    ServletRequestContext context = new ServletRequestContext(wac, new MockHttpServletRequest("GET", "foo.html"), new MockHttpServletResponse());
    ModelAndView mv = pvc.handleRequest(context);
    assertThat(mv.getModel().size() == 0).as("model has no data").isTrue();
    assertThat(mv.getViewName().equals(viewName)).as("model has correct viewname").isTrue();
    assertThat(pvc.getViewName().equals(viewName)).as("getViewName matches").isTrue();
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ModelAndView(cn.taketoday.web.view.ModelAndView) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with StaticWebApplicationContext

use of cn.taketoday.web.context.support.StaticWebApplicationContext in project today-infrastructure by TAKETODAY.

the class UrlFilenameViewControllerTests method withContextMapping.

@Test
void withContextMapping() throws Exception {
    UrlFilenameViewController controller = new UrlFilenameViewController();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/docs/cvs/commit.html");
    request.setContextPath("/myapp");
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.refresh();
    ServletRequestContext context = new ServletRequestContext(wac, request, new MockHttpServletResponse());
    ModelAndView mv = controller.handleRequest(context);
    assertThat(mv.getViewName()).isEqualTo("docs/cvs/commit");
    assertThat(mv.getModel().isEmpty()).isTrue();
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ModelAndView(cn.taketoday.web.view.ModelAndView) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) PathPatternsParameterizedTest(cn.taketoday.web.view.PathPatternsParameterizedTest)

Example 4 with StaticWebApplicationContext

use of cn.taketoday.web.context.support.StaticWebApplicationContext in project today-framework by TAKETODAY.

the class UrlFilenameViewControllerTests method withContextMapping.

@Test
void withContextMapping() throws Exception {
    UrlFilenameViewController controller = new UrlFilenameViewController();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myapp/docs/cvs/commit.html");
    request.setContextPath("/myapp");
    StaticWebApplicationContext wac = new StaticWebApplicationContext();
    wac.refresh();
    ServletRequestContext context = new ServletRequestContext(wac, request, new MockHttpServletResponse());
    ModelAndView mv = controller.handleRequest(context);
    assertThat(mv.getViewName()).isEqualTo("docs/cvs/commit");
    assertThat(mv.getModel().isEmpty()).isTrue();
}
Also used : MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(cn.taketoday.web.view.ModelAndView) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test) PathPatternsParameterizedTest(cn.taketoday.web.view.PathPatternsParameterizedTest)

Example 5 with StaticWebApplicationContext

use of cn.taketoday.web.context.support.StaticWebApplicationContext in project today-framework by TAKETODAY.

the class XsltViewTests method getXsltView.

private XsltView getXsltView(String templatePath) {
    XsltView view = new XsltView();
    view.setUrl(templatePath);
    view.setApplicationContext(new StaticWebApplicationContext());
    view.initApplicationContext();
    return view;
}
Also used : StaticWebApplicationContext(cn.taketoday.web.context.support.StaticWebApplicationContext)

Aggregations

StaticWebApplicationContext (cn.taketoday.web.context.support.StaticWebApplicationContext)21 Test (org.junit.jupiter.api.Test)11 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)7 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)4 MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)4 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)4 ModelAndView (cn.taketoday.web.view.ModelAndView)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 RequestBindingException (cn.taketoday.web.bind.RequestBindingException)3 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)3 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)3 PathPatternsParameterizedTest (cn.taketoday.web.view.PathPatternsParameterizedTest)3 FileSystemResource (cn.taketoday.core.io.FileSystemResource)2 ContentNegotiationManager (cn.taketoday.web.accept.ContentNegotiationManager)2 ExceptionHandlerAnnotationExceptionHandler (cn.taketoday.web.handler.method.ExceptionHandlerAnnotationExceptionHandler)2 DispatcherServlet (cn.taketoday.web.servlet.DispatcherServlet)2 MockServletConfig (cn.taketoday.web.testfixture.servlet.MockServletConfig)2 ServletException (jakarta.servlet.ServletException)1