Search in sources :

Example 16 with HandlerMatchingMetadata

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

the class ReactiveTypeHandlerTests method mediaTypes.

@Test
public void mediaTypes() throws Exception {
    // Media type from request
    this.servletRequest.addHeader("Accept", "text/event-stream");
    testSseResponse(true);
    // Media type from "produces" attribute
    Set<MediaType> types = Collections.singleton(MediaType.TEXT_EVENT_STREAM);
    HandlerMatchingMetadata matchingMetadata = new HandlerMatchingMetadata(webRequest);
    matchingMetadata.setProducibleMediaTypes(new MediaType[] { MediaType.TEXT_EVENT_STREAM });
    webRequest.setMatchingMetadata(matchingMetadata);
    testSseResponse(true);
    // No media type preferences
    testSseResponse(false);
}
Also used : MediaType(cn.taketoday.http.MediaType) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 17 with HandlerMatchingMetadata

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

the class BaseViewTests method dynamicModelOverridesPathVariables.

@Test
public void dynamicModelOverridesPathVariables() throws Exception {
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, String> pathVars = new HashMap<>();
    pathVars.put("one", "bar");
    pathVars.put("something", "else");
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    HandlerMatchingMetadata metadata = new HandlerMatchingMetadata(requestContext) {

        @Override
        public Map<String, String> getUriVariables() {
            return pathVars;
        }
    };
    requestContext.setMatchingMetadata(metadata);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    tv.render(model, requestContext);
    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.testfixture.servlet.MockHttpServletRequest) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 18 with HandlerMatchingMetadata

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

the class BaseViewTests method pathVarsOverrideStaticAttributes.

@Test
public void pathVarsOverrideStaticAttributes() 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 pathVars = new HashMap<>();
    pathVars.put("one", new HashMap<>());
    pathVars.put("two", new Object());
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    HandlerMatchingMetadata metadata = new HandlerMatchingMetadata(requestContext) {

        @Override
        public Map<String, String> getUriVariables() {
            return pathVars;
        }
    };
    requestContext.setMatchingMetadata(metadata);
    tv.render(new HashMap<>(), requestContext);
    checkContainsAll(pathVars, 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) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 19 with HandlerMatchingMetadata

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

the class BaseViewTests method dynamicModelOverridesPathVariables.

@Test
public void dynamicModelOverridesPathVariables() throws Exception {
    WebServletApplicationContext wac = mock(WebServletApplicationContext.class);
    given(wac.getServletContext()).willReturn(new MockServletContext());
    TestView tv = new TestView(wac);
    tv.setApplicationContext(wac);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, String> pathVars = new HashMap<>();
    pathVars.put("one", "bar");
    pathVars.put("something", "else");
    RequestContext requestContext = ServletUtils.getRequestContext(request, response);
    HandlerMatchingMetadata metadata = new HandlerMatchingMetadata(requestContext) {

        @Override
        public Map<String, String> getUriVariables() {
            return pathVars;
        }
    };
    requestContext.setMatchingMetadata(metadata);
    Map<String, Object> model = new HashMap<>();
    model.put("one", new HashMap<>());
    model.put("two", new Object());
    tv.render(model, requestContext);
    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) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) MockServletContext(cn.taketoday.web.mock.MockServletContext) RequestContext(cn.taketoday.web.RequestContext) MockHttpServletResponse(cn.taketoday.web.mock.MockHttpServletResponse) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Aggregations

HandlerMatchingMetadata (cn.taketoday.web.HandlerMatchingMetadata)19 Test (org.junit.jupiter.api.Test)12 HashMap (java.util.HashMap)7 MediaType (cn.taketoday.http.MediaType)6 RequestPath (cn.taketoday.http.server.RequestPath)4 RequestContext (cn.taketoday.web.RequestContext)4 WebServletApplicationContext (cn.taketoday.web.servlet.WebServletApplicationContext)4 PathPattern (cn.taketoday.web.util.pattern.PathPattern)4 PathPatternsRequestCondition (cn.taketoday.web.handler.condition.PathPatternsRequestCondition)2 MockHttpServletRequest (cn.taketoday.web.mock.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.mock.MockHttpServletResponse)2 MockServletContext (cn.taketoday.web.mock.MockServletContext)2 MockHttpServletRequest (cn.taketoday.web.testfixture.servlet.MockHttpServletRequest)2 MockHttpServletResponse (cn.taketoday.web.testfixture.servlet.MockHttpServletResponse)2 MockServletContext (cn.taketoday.web.testfixture.servlet.MockServletContext)2 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)2 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)2 Map (java.util.Map)2 Properties (java.util.Properties)2 HttpOutputMessage (cn.taketoday.http.HttpOutputMessage)1