Search in sources :

Example 1 with MockServletContext

use of cn.taketoday.web.mock.MockServletContext in project today-infrastructure by TAKETODAY.

the class ContentNegotiatingViewResolverTests method createViewResolver.

@BeforeEach
public void createViewResolver() {
    wac.setServletContext(new MockServletContext());
    wac.refresh();
    viewResolver = new ContentNegotiatingViewResolver();
    viewResolver.setApplicationContext(wac);
    request = new MockHttpServletRequest("GET", "/test");
    MockHttpServletResponse response = new MockHttpServletResponse();
    this.requestContext = new MockServletRequestContext(wac, request, response);
    RequestContextHolder.set(requestContext);
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockServletContext(cn.taketoday.web.mock.MockServletContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with MockServletContext

use of cn.taketoday.web.mock.MockServletContext in project today-infrastructure by TAKETODAY.

the class InternalResourceViewTests method forward.

@Test
public void forward() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myservlet/handler.do");
    request.setContextPath("/mycontext");
    request.setServletPath("/myservlet");
    request.setPathInfo(";mypathinfo");
    request.setQueryString("?param1=value1");
    RequestContext context = new ServletRequestContext(null, request, response);
    view.setUrl(url);
    view.setServletContext(new MockServletContext() {

        @Override
        public int getMinorVersion() {
            return 4;
        }
    });
    view.render(model, context);
    assertThat(response.getForwardedUrl()).isEqualTo(url);
    model.forEach((key, value) -> assertThat(request.getAttribute(key)).as("Values for model key '" + key + "' must match").isEqualTo(value));
}
Also used : MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletContext(cn.taketoday.web.mock.MockServletContext) Test(org.junit.jupiter.api.Test)

Example 3 with MockServletContext

use of cn.taketoday.web.mock.MockServletContext in project today-infrastructure by TAKETODAY.

the class ScriptTemplateViewTests method resourceLoaderPath.

// SPR-14210
@Test
public void resourceLoaderPath() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    this.wac.setServletContext(servletContext);
    this.wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> model = new HashMap<>();
    InvocableScriptEngine engine = mock(InvocableScriptEngine.class);
    given(engine.invokeFunction(any(), any(), any(), any())).willReturn("foo");
    this.view.setEngine(engine);
    this.view.setRenderFunction("render");
    this.view.setApplicationContext(this.wac);
    this.view.setUrl("cn/taketoday/web/servlet/view/script/empty.txt");
    this.view.render(model, new ServletRequestContext(wac, request, response));
    assertThat(response.getContentAsString()).isEqualTo("foo");
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:cn/taketoday/web/servlet/view/script/");
    this.view.setUrl("empty.txt");
    this.view.render(model, new ServletRequestContext(wac, request, response));
    assertThat(response.getContentAsString()).isEqualTo("foo");
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:cn/taketoday/web/servlet/view/script");
    this.view.setUrl("empty.txt");
    this.view.render(model, new ServletRequestContext(wac, request, response));
    assertThat(response.getContentAsString()).isEqualTo("foo");
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletContext(cn.taketoday.web.mock.MockServletContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockServletContext

use of cn.taketoday.web.mock.MockServletContext in project today-infrastructure by TAKETODAY.

the class ContentNegotiatingViewResolverTests method nestedViewResolverIsNotSpringBean.

@Test
public void nestedViewResolverIsNotSpringBean() throws Exception {
    StaticWebServletApplicationContext webAppContext = new StaticWebServletApplicationContext();
    webAppContext.setServletContext(new MockServletContext());
    webAppContext.refresh();
    InternalResourceViewResolver nestedResolver = new InternalResourceViewResolver();
    nestedResolver.setApplicationContext(webAppContext);
    nestedResolver.setViewClass(InternalResourceView.class);
    viewResolver.setViewResolvers(new ArrayList<>(Arrays.asList(nestedResolver)));
    FixedContentNegotiationStrategy fixedStrategy = new FixedContentNegotiationStrategy(MediaType.TEXT_HTML);
    viewResolver.setContentNegotiationManager(new ContentNegotiationManager(fixedStrategy));
    viewResolver.afterPropertiesSet();
    String viewName = "view";
    Locale locale = Locale.ENGLISH;
    View result = viewResolver.resolveViewName(viewName, locale);
    assertThat(result).as("Invalid view").isNotNull();
}
Also used : Locale(java.util.Locale) ContentNegotiationManager(cn.taketoday.web.accept.ContentNegotiationManager) FixedContentNegotiationStrategy(cn.taketoday.web.accept.FixedContentNegotiationStrategy) StaticWebServletApplicationContext(cn.taketoday.web.context.support.StaticWebServletApplicationContext) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) MockServletContext(cn.taketoday.web.mock.MockServletContext) InternalResourceViewResolver(cn.taketoday.web.servlet.view.InternalResourceViewResolver) Test(org.junit.jupiter.api.Test)

Example 5 with MockServletContext

use of cn.taketoday.web.mock.MockServletContext in project today-infrastructure by TAKETODAY.

the class BaseViewTests method renderWithStaticAttributesNoCollision.

/**
 * Test attribute passing, NOT CSV parsing.
 */
@Test
public void renderWithStaticAttributesNoCollision() throws Exception {
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    Properties p = new Properties();
    p.setProperty("foo", "bar");
    p.setProperty("something", "else");
    tv.setAttributes(p);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    tv.render(model, requestContext);
    checkContainsAll(model, tv.model);
    checkContainsAll(p, tv.model);
    assertThat(tv.initialized).isTrue();
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) Properties(java.util.Properties) MockServletContext(cn.taketoday.web.mock.MockServletContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.mock.MockHttpServletRequest) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

MockServletContext (cn.taketoday.web.mock.MockServletContext)41 Test (org.junit.jupiter.api.Test)35 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)14 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)12 AnnotationConfigServletWebApplicationContext (cn.taketoday.framework.web.servlet.context.AnnotationConfigServletWebApplicationContext)10 StaticWebServletApplicationContext (cn.taketoday.web.context.support.StaticWebServletApplicationContext)10 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)10 HashMap (java.util.HashMap)10 Resource (cn.taketoday.core.io.Resource)6 RequestContext (cn.taketoday.web.RequestContext)6 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)6 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)6 ServletContextResource (cn.taketoday.web.context.support.ServletContextResource)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 PropertyValues (cn.taketoday.beans.PropertyValues)4 ServletContextResourcePatternLoader (cn.taketoday.web.context.support.ServletContextResourcePatternLoader)4 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 HashSet (java.util.HashSet)4 InternalResourceView (cn.taketoday.web.servlet.view.InternalResourceView)3 Properties (java.util.Properties)3