Search in sources :

Example 6 with HandlerMatchingMetadata

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

the class RequestResponseBodyMethodProcessorMockTests method handleReturnValueProduces.

@Test
public void handleReturnValueProduces() throws Throwable {
    String body = "Foo";
    servletRequest.addHeader("Accept", "text/*");
    HandlerMatchingMetadata metadata = mock(HandlerMatchingMetadata.class);
    given(metadata.getProducibleMediaTypes()).willReturn(new MediaType[] { MediaType.TEXT_HTML });
    webRequest.setMatchingMetadata(metadata);
    given(stringMessageConverter.canWrite(String.class, MediaType.TEXT_HTML)).willReturn(true);
    processor.handleReturnValue(webRequest, handlerMethod6, body);
    verify(stringMessageConverter).write(eq(body), eq(MediaType.TEXT_HTML), isA(HttpOutputMessage.class));
}
Also used : HttpOutputMessage(cn.taketoday.http.HttpOutputMessage) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 7 with HandlerMatchingMetadata

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

the class PathVariableMapMethodArgumentResolverTests method resolveArgument.

@Test
public void resolveArgument() throws Throwable {
    applyTemplateVars();
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("name1", "value1");
    uriTemplateVars.put("name2", "value2");
    RequestPath requestPath = RequestPath.parse("/mock/value1/value2", null);
    PathPattern pathPattern = PathPatternParser.defaultInstance.parse("/mock/{name1}/{name2}");
    HandlerMatchingMetadata matchingMetadata = new HandlerMatchingMetadata(new Object(), "/mock", requestPath, pathPattern, PathPatternParser.defaultInstance);
    webRequest.setMatchingMetadata(matchingMetadata);
    Object result = resolver.resolveArgument(webRequest, paramMap);
    assertThat(result).isEqualTo(uriTemplateVars);
}
Also used : RequestPath(cn.taketoday.http.server.RequestPath) PathPattern(cn.taketoday.web.util.pattern.PathPattern) HashMap(java.util.HashMap) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 8 with HandlerMatchingMetadata

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

the class RedirectViewUriTemplateTests method uriTemplateReuseCurrentRequestVars.

@Test
void uriTemplateReuseCurrentRequestVars() throws Exception {
    Map<String, Object> model = new HashMap<>();
    model.put("key1", "value1");
    model.put("name", "value2");
    model.put("key3", "value3");
    Map<String, String> currentRequestUriTemplateVars = new HashMap<>();
    currentRequestUriTemplateVars.put("var1", "v1");
    currentRequestUriTemplateVars.put("name", "v2");
    currentRequestUriTemplateVars.put("var3", "v3");
    HandlerMatchingMetadata metadata = new HandlerMatchingMetadata(context) {

        @Override
        public Map<String, String> getUriVariables() {
            return currentRequestUriTemplateVars;
        }
    };
    context.setMatchingMetadata(metadata);
    String url = "https://url.somewhere.com";
    RedirectView redirectView = new RedirectView(url + "/{key1}/{var1}/{name}");
    redirectView.renderMergedOutputModel(model, context);
    assertThat(this.response.getRedirectedUrl()).isEqualTo((url + "/value1/v1/value2?key3=value3"));
}
Also used : HashMap(java.util.HashMap) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 9 with HandlerMatchingMetadata

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

the class ContentNegotiatingViewResolverTests method getMediaTypeAcceptHeaderWithProduces.

@Test
public void getMediaTypeAcceptHeaderWithProduces() throws Exception {
    HandlerMatchingMetadata matchingMetadata = new HandlerMatchingMetadata(requestContext);
    matchingMetadata.setProducibleMediaTypes(new MediaType[] { MediaType.APPLICATION_XHTML_XML });
    requestContext.setMatchingMetadata(matchingMetadata);
    request.addHeader("Accept", "text/html,application/xml;q=0.9,application/xhtml+xml,*/*;q=0.8");
    viewResolver.afterPropertiesSet();
    List<MediaType> result = viewResolver.getMediaTypes(requestContext);
    assertThat(result.get(0)).as("Invalid content type").isEqualTo(new MediaType("application", "xhtml+xml"));
}
Also used : MediaType(cn.taketoday.http.MediaType) HandlerMatchingMetadata(cn.taketoday.web.HandlerMatchingMetadata) Test(org.junit.jupiter.api.Test)

Example 10 with HandlerMatchingMetadata

use of cn.taketoday.web.HandlerMatchingMetadata in project today-framework 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.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) WebServletApplicationContext(cn.taketoday.web.servlet.WebServletApplicationContext) Properties(java.util.Properties) MockServletContext(cn.taketoday.web.testfixture.servlet.MockServletContext) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(cn.taketoday.web.testfixture.servlet.MockHttpServletRequest) RequestContext(cn.taketoday.web.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) MockHttpServletResponse(cn.taketoday.web.testfixture.servlet.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