Search in sources :

Example 26 with UrlPathHelper

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

the class WebMvcConfigurationSupportTests method defaultPathMatchConfiguration.

@Test
public void defaultPathMatchConfiguration() {
    ApplicationContext context = initContext(WebConfig.class);
    UrlPathHelper urlPathHelper = context.getBean(UrlPathHelper.class);
    PathMatcher pathMatcher = context.getBean(PathMatcher.class);
    assertThat(urlPathHelper).isNotNull();
    assertThat(pathMatcher).isNotNull();
    assertThat(pathMatcher.getClass()).isEqualTo(AntPathMatcher.class);
}
Also used : AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) ApplicationContext(org.springframework.context.ApplicationContext) AntPathMatcher(org.springframework.util.AntPathMatcher) PathMatcher(org.springframework.util.PathMatcher) UrlPathHelper(org.springframework.web.util.UrlPathHelper) Test(org.junit.jupiter.api.Test)

Example 27 with UrlPathHelper

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

the class WebMvcStompEndpointRegistryTests method handlerMapping.

@Test
public void handlerMapping() {
    SimpleUrlHandlerMapping hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
    assertThat(hm.getUrlMap().size()).isEqualTo(0);
    UrlPathHelper pathHelper = new UrlPathHelper();
    this.endpointRegistry.setUrlPathHelper(pathHelper);
    this.endpointRegistry.addEndpoint("/stompOverWebSocket");
    this.endpointRegistry.addEndpoint("/stompOverSockJS").withSockJS();
    // SPR-12403
    assertThat(this.webSocketHandler.getProtocolHandlers().size()).isEqualTo(1);
    hm = (SimpleUrlHandlerMapping) this.endpointRegistry.getHandlerMapping();
    assertThat(hm.getUrlMap().size()).isEqualTo(2);
    assertThat(hm.getUrlMap().get("/stompOverWebSocket")).isNotNull();
    assertThat(hm.getUrlMap().get("/stompOverSockJS/**")).isNotNull();
    assertThat(hm.getUrlPathHelper()).isSameAs(pathHelper);
}
Also used : UrlPathHelper(org.springframework.web.util.UrlPathHelper) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 28 with UrlPathHelper

use of org.springframework.web.util.UrlPathHelper 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)

Example 29 with UrlPathHelper

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

the class DelegatingWebMvcConfigurationTests method configurePathMatcher.

@Test
@SuppressWarnings("deprecation")
public void configurePathMatcher() {
    PathMatcher pathMatcher = mock(PathMatcher.class);
    UrlPathHelper pathHelper = mock(UrlPathHelper.class);
    WebMvcConfigurer configurer = new WebMvcConfigurer() {

        @Override
        @SuppressWarnings("deprecation")
        public void configurePathMatch(PathMatchConfigurer configurer) {
            configurer.setUseRegisteredSuffixPatternMatch(true).setUseTrailingSlashMatch(false).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).isSameAs(pathHelper);
        assertThat(matcher).isSameAs(pathMatcher);
    };
    RequestMappingHandlerMapping annotationsMapping = webMvcConfig.requestMappingHandlerMapping(webMvcConfig.mvcContentNegotiationManager(), webMvcConfig.mvcConversionService(), webMvcConfig.mvcResourceUrlProvider());
    assertThat(annotationsMapping).isNotNull();
    assertThat(annotationsMapping.useRegisteredSuffixPatternMatch()).isTrue();
    assertThat(annotationsMapping.useSuffixPatternMatch()).isTrue();
    assertThat(annotationsMapping.useTrailingSlashMatch()).isFalse();
    configAssertion.accept(annotationsMapping.getUrlPathHelper(), annotationsMapping.getPathMatcher());
    SimpleUrlHandlerMapping mapping = (SimpleUrlHandlerMapping) webMvcConfig.viewControllerHandlerMapping(webMvcConfig.mvcConversionService(), webMvcConfig.mvcResourceUrlProvider());
    assertThat(mapping).isNotNull();
    configAssertion.accept(mapping.getUrlPathHelper(), mapping.getPathMatcher());
    mapping = (SimpleUrlHandlerMapping) webMvcConfig.resourceHandlerMapping(webMvcConfig.mvcContentNegotiationManager(), webMvcConfig.mvcConversionService(), webMvcConfig.mvcResourceUrlProvider());
    assertThat(mapping).isNotNull();
    configAssertion.accept(mapping.getUrlPathHelper(), mapping.getPathMatcher());
    configAssertion.accept(webMvcConfig.mvcResourceUrlProvider().getUrlPathHelper(), webMvcConfig.mvcResourceUrlProvider().getPathMatcher());
    configAssertion.accept(webMvcConfig.mvcUrlPathHelper(), webMvcConfig.mvcPathMatcher());
}
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) PathMatcher(org.springframework.util.PathMatcher) UrlPathHelper(org.springframework.web.util.UrlPathHelper) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) SimpleUrlHandlerMapping(org.springframework.web.servlet.handler.SimpleUrlHandlerMapping) Test(org.junit.jupiter.api.Test)

Example 30 with UrlPathHelper

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

the class ResourceHandlerRegistryTests method setup.

@BeforeEach
public void setup() {
    GenericWebApplicationContext appContext = new GenericWebApplicationContext();
    appContext.refresh();
    this.registry = new ResourceHandlerRegistry(appContext, new MockServletContext(), new ContentNegotiationManager(), new UrlPathHelper());
    this.registration = this.registry.addResourceHandler("/resources/**");
    this.registration.addResourceLocations("classpath:org/springframework/web/servlet/config/annotation/");
    this.response = new MockHttpServletResponse();
}
Also used : ContentNegotiationManager(org.springframework.web.accept.ContentNegotiationManager) UrlPathHelper(org.springframework.web.util.UrlPathHelper) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

UrlPathHelper (org.springframework.web.util.UrlPathHelper)32 Test (org.junit.jupiter.api.Test)10 PathMatcher (org.springframework.util.PathMatcher)5 Map (java.util.Map)4 Test (org.junit.Test)4 MultiValueMap (org.springframework.util.MultiValueMap)4 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)4 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)3 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)3 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)3 Collections (java.util.Collections)2 List (java.util.List)2 BiConsumer (java.util.function.BiConsumer)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)2 ArgumentCaptor (org.mockito.ArgumentCaptor)2 BDDMockito.given (org.mockito.BDDMockito.given)2