Search in sources :

Example 21 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class RouterFunctionMappingTests method changeParser.

@Test
void changeParser() throws Exception {
    HandlerFunction<ServerResponse> handlerFunction = request -> ServerResponse.ok().build();
    RouterFunction<ServerResponse> routerFunction = RouterFunctions.route().GET("/foo", handlerFunction).POST("/bar", handlerFunction).build();
    RouterFunctionMapping mapping = new RouterFunctionMapping(routerFunction);
    mapping.setMessageConverters(this.messageConverters);
    PathPatternParser patternParser = new PathPatternParser();
    patternParser.setCaseSensitive(false);
    mapping.setPatternParser(patternParser);
    mapping.afterPropertiesSet();
    MockHttpServletRequest request = createTestRequest("/FOO");
    HandlerExecutionChain result = mapping.getHandler(request);
    assertThat(result).isNotNull();
    assertThat(result.getHandler()).isSameAs(handlerFunction);
}
Also used : ServerResponse(org.springframework.web.servlet.function.ServerResponse) HandlerFunction(org.springframework.web.servlet.function.HandlerFunction) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RouterFunctions(org.springframework.web.servlet.function.RouterFunctions) ServletRequestPathUtils(org.springframework.web.util.ServletRequestPathUtils) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ServerResponse(org.springframework.web.servlet.function.ServerResponse) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) RouterFunction(org.springframework.web.servlet.function.RouterFunction) List(java.util.List) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Optional(java.util.Optional) Collections(java.util.Collections) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) Test(org.junit.jupiter.api.Test)

Example 22 with PathPatternParser

use of org.springframework.web.util.pattern.PathPatternParser in project spring-framework by spring-projects.

the class DelegatingWebMvcConfigurationTests method configurePathPatternParser.

@Test
public void configurePathPatternParser() {
    PathPatternParser patternParser = new PathPatternParser();
    PathMatcher pathMatcher = mock(PathMatcher.class);
    UrlPathHelper pathHelper = mock(UrlPathHelper.class);
    WebMvcConfigurer configurer = new WebMvcConfigurer() {

        @Override
        public void configurePathMatch(PathMatchConfigurer configurer) {
            configurer.setPatternParser(patternParser).setUrlPathHelper(pathHelper).setPathMatcher(pathMatcher);
        }

        @Override
        public void addViewControllers(ViewControllerRegistry registry) {
            registry.addViewController("/").setViewName("home");
        }

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/");
        }
    };
    MockServletContext servletContext = new MockServletContext();
    webMvcConfig.setConfigurers(Collections.singletonList(configurer));
    webMvcConfig.setServletContext(servletContext);
    webMvcConfig.setApplicationContext(new GenericWebApplicationContext(servletContext));
    BiConsumer<UrlPathHelper, PathMatcher> configAssertion = (helper, matcher) -> {
        assertThat(helper).isNotSameAs(pathHelper);
        assertThat(matcher).isNotSameAs(pathMatcher);
    };
    RequestMappingHandlerMapping annotationsMapping = webMvcConfig.requestMappingHandlerMapping(webMvcConfig.mvcContentNegotiationManager(), webMvcConfig.mvcConversionService(), webMvcConfig.mvcResourceUrlProvider());
    assertThat(annotationsMapping).isNotNull();
    assertThat(annotationsMapping.getPatternParser()).isSameAs(patternParser).isSameAs(webMvcConfig.mvcPatternParser());
    configAssertion.accept(annotationsMapping.getUrlPathHelper(), annotationsMapping.getPathMatcher());
    SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) webMvcConfig.viewControllerHandlerMapping(webMvcConfig.mvcConversionService(), webMvcConfig.mvcResourceUrlProvider());
    assertThat(mapping).isNotNull();
    assertThat(mapping.getPatternParser()).isSameAs(patternParser);
    configAssertion.accept(mapping.getUrlPathHelper(), mapping.getPathMatcher());
    mapping = (SimpleUrlHandlerMapping) webMvcConfig.resourceHandlerMapping(webMvcConfig.mvcContentNegotiationManager(), webMvcConfig.mvcConversionService(), webMvcConfig.mvcResourceUrlProvider());
    assertThat(mapping).isNotNull();
    assertThat(mapping.getPatternParser()).isSameAs(patternParser);
    configAssertion.accept(mapping.getUrlPathHelper(), mapping.getPathMatcher());
    BeanNameUrlHandlerMapping beanNameMapping = webMvcConfig.beanNameHandlerMapping(webMvcConfig.mvcConversionService(), webMvcConfig.mvcResourceUrlProvider());
    assertThat(beanNameMapping).isNotNull();
    assertThat(beanNameMapping.getPatternParser()).isSameAs(patternParser);
    configAssertion.accept(beanNameMapping.getUrlPathHelper(), beanNameMapping.getPathMatcher());
    assertThat(webMvcConfig.mvcResourceUrlProvider().getUrlPathHelper()).isSameAs(pathHelper);
    assertThat(webMvcConfig.mvcResourceUrlProvider().getPathMatcher()).isSameAs(pathMatcher);
}
Also used : Mock(org.mockito.Mock) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Captor(org.mockito.Captor) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) HandlerExceptionResolverComposite(org.springframework.web.servlet.handler.HandlerExceptionResolverComposite) BDDMockito.given(org.mockito.BDDMockito.given) BiConsumer(java.util.function.BiConsumer) LocalValidatorFactoryBean(org.springframework.validation.beanvalidation.LocalValidatorFactoryBean) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) PathMatcher(org.springframework.util.PathMatcher) ConfigurableWebBindingInitializer(org.springframework.web.bind.support.ConfigurableWebBindingInitializer) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) FormattingConversionService(org.springframework.format.support.FormattingConversionService) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) Mockito.verify(org.mockito.Mockito.verify) Test(org.junit.jupiter.api.Test) List(java.util.List) DefaultMessageCodesResolver(org.springframework.validation.DefaultMessageCodesResolver) HandlerExceptionResolver(org.springframework.web.servlet.HandlerExceptionResolver) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) UrlPathHelper(org.springframework.web.util.UrlPathHelper) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) ExceptionHandlerExceptionResolver(org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver) Collections(java.util.Collections) ResponseStatusExceptionResolver(org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) HandlerMethodReturnValueHandler(org.springframework.web.method.support.HandlerMethodReturnValueHandler) Mockito.mock(org.mockito.Mockito.mock) BeanNameUrlHandlerMapping(org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping) PathPatternParser(org.springframework.web.util.pattern.PathPatternParser) UrlPathHelper(org.springframework.web.util.UrlPathHelper) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) PathMatcher(org.springframework.util.PathMatcher) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) Test(org.junit.jupiter.api.Test)

Aggregations

PathPatternParser (org.springframework.web.util.pattern.PathPatternParser)22 Test (org.junit.jupiter.api.Test)13 RequestMappingInfo (org.springframework.web.reactive.result.method.RequestMappingInfo)4 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 Field (java.lang.reflect.Field)2 Collections (java.util.Collections)2 List (java.util.List)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ApplicationContext (org.springframework.context.ApplicationContext)2 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)2 HttpMessageConverter (org.springframework.http.converter.HttpMessageConverter)2 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)2 PathPattern (org.springframework.web.util.pattern.PathPattern)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Tag (io.micrometer.core.instrument.Tag)1 Method (java.lang.reflect.Method)1 Optional (java.util.Optional)1 Properties (java.util.Properties)1 BiConsumer (java.util.function.BiConsumer)1