Search in sources :

Example 11 with RequestContext

use of cn.taketoday.web.RequestContext 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)

Example 12 with RequestContext

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

the class BaseViewTests method dynamicModelOverridesStaticAttributesIfCollision.

@Test
public void dynamicModelOverridesStaticAttributesIfCollision() 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("one", "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);
    // Check it contains all
    checkContainsAll(model, tv.model);
    assertThat(tv.model.size()).isEqualTo(3);
    assertThat(tv.model.get("something")).isEqualTo("else");
    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)

Example 13 with RequestContext

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

the class CorsFilter method doFilterInternal.

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException {
    RequestContext context = RequestContextHolder.get();
    if (context == null) {
        WebApplicationContext webApplicationContext = ServletUtils.findWebApplicationContext(request);
        context = new ServletRequestContext(webApplicationContext, request, response);
        RequestContextHolder.set(context);
    }
    try {
        CorsConfiguration corsConfiguration = this.configSource.getCorsConfiguration(context);
        if (!processor.process(corsConfiguration, context) || CorsUtils.isPreFlightRequest(context)) {
            return;
        }
        // handle next
        filterChain.doFilter(request, response);
    } finally {
        RequestContextHolder.remove();
    }
}
Also used : CorsConfiguration(cn.taketoday.web.cors.CorsConfiguration) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) WebApplicationContext(cn.taketoday.web.WebApplicationContext)

Example 14 with RequestContext

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

the class UrlFilenameViewControllerTests method withPrefix.

@PathPatternsParameterizedTest
void withPrefix(Function<String, RequestContext> requestFactory) throws Exception {
    UrlFilenameViewController controller = new UrlFilenameViewController();
    controller.setPrefix("mypre_");
    RequestContext request = requestFactory.apply("/index.html");
    ModelAndView mv = controller.handleRequest(request);
    assertThat(mv.getViewName()).isEqualTo("mypre_index");
    assertThat(mv.getModel().isEmpty()).isTrue();
}
Also used : ModelAndView(cn.taketoday.web.view.ModelAndView) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) PathPatternsParameterizedTest(cn.taketoday.web.view.PathPatternsParameterizedTest)

Example 15 with RequestContext

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

the class UrlFilenameViewControllerTests method withPrefixAndSuffix.

@PathPatternsParameterizedTest
void withPrefixAndSuffix(Function<String, RequestContext> requestFactory) throws Exception {
    UrlFilenameViewController controller = new UrlFilenameViewController();
    controller.setPrefix("mypre_");
    controller.setSuffix("_mysuf");
    RequestContext request = requestFactory.apply("/index.html");
    ModelAndView mv = controller.handleRequest(request);
    assertThat(mv.getViewName()).isEqualTo("mypre_index_mysuf");
    assertThat(mv.getModel().isEmpty()).isTrue();
}
Also used : ModelAndView(cn.taketoday.web.view.ModelAndView) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) PathPatternsParameterizedTest(cn.taketoday.web.view.PathPatternsParameterizedTest)

Aggregations

RequestContext (cn.taketoday.web.RequestContext)109 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)65 MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)46 Test (org.junit.jupiter.api.Test)42 ModelAndView (cn.taketoday.web.view.ModelAndView)22 PathPatternsParameterizedTest (cn.taketoday.web.view.PathPatternsParameterizedTest)22 HashMap (java.util.HashMap)22 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)12 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)12 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)12 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)12 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)10 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)9 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)9 Map (java.util.Map)8 Properties (java.util.Properties)8 MockServletContext (cn.taketoday.web.mock.MockServletContext)6 WebSession (cn.taketoday.web.session.WebSession)6 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)6 View (cn.taketoday.web.view.View)6