Search in sources :

Example 1 with BEST_MATCHING_HANDLER_ATTRIBUTE

use of org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE in project spring-framework by spring-projects.

the class SimpleUrlHandlerMappingTests method checkMappings.

@ParameterizedTest
@ValueSource(strings = { "urlMapping", "urlMappingWithProps", "urlMappingWithPathPatterns" })
void checkMappings(String beanName) throws Exception {
    MockServletContext sc = new MockServletContext("");
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setServletContext(sc);
    wac.setConfigLocations("/org/springframework/web/servlet/handler/map2.xml");
    wac.refresh();
    Object bean = wac.getBean("mainController");
    Object otherBean = wac.getBean("otherController");
    Object defaultBean = wac.getBean("starController");
    HandlerMapping hm = (HandlerMapping) wac.getBean(beanName);
    wac.close();
    boolean usePathPatterns = (((AbstractHandlerMapping) hm).getPatternParser() != null);
    MockHttpServletRequest request = PathPatternsTestUtils.initRequest("GET", "/welcome.html", usePathPatterns);
    HandlerExecutionChain chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("/welcome.html");
    assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(bean);
    request = PathPatternsTestUtils.initRequest("GET", "/welcome.x;jsessionid=123", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(otherBean);
    assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("welcome.x");
    assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(otherBean);
    request = PathPatternsTestUtils.initRequest("GET", "/app", "/welcome.x", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(otherBean);
    assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("welcome.x");
    assertThat(request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).isEqualTo(otherBean);
    request = PathPatternsTestUtils.initRequest("GET", "/welcome/", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(otherBean);
    assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("welcome");
    request = PathPatternsTestUtils.initRequest("GET", "/", usePathPatterns);
    request.setServletPath("/welcome.html");
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    request = PathPatternsTestUtils.initRequest("GET", "/app", "/welcome.html", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    request = PathPatternsTestUtils.initRequest("GET", "/show.html", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    request = PathPatternsTestUtils.initRequest("GET", "/bookseats.html", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    request = PathPatternsTestUtils.initRequest("GET", null, "/original-welcome.html", usePathPatterns, req -> req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/welcome.html"));
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    request = PathPatternsTestUtils.initRequest("GET", null, "/original-show.html", usePathPatterns, req -> req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/show.html"));
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    request = PathPatternsTestUtils.initRequest("GET", null, "/original-bookseats.html", usePathPatterns, req -> req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/bookseats.html"));
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    request = PathPatternsTestUtils.initRequest("GET", "/", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler()).isSameAs(bean);
    assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("/");
    request = PathPatternsTestUtils.initRequest("GET", "/somePath", usePathPatterns);
    chain = getHandler(hm, request);
    assertThat(chain.getHandler() == defaultBean).as("Handler is correct bean").isTrue();
    assertThat(request.getAttribute(PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)).isEqualTo("/somePath");
}
Also used : ValueSource(org.junit.jupiter.params.provider.ValueSource) WebUtils(org.springframework.web.util.WebUtils) FatalBeanException(org.springframework.beans.FatalBeanException) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) BEST_MATCHING_HANDLER_ATTRIBUTE(org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) HandlerMapping(org.springframework.web.servlet.HandlerMapping) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) HandlerInterceptor(org.springframework.web.servlet.HandlerInterceptor) PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE(org.springframework.web.servlet.HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE) Collections(java.util.Collections) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) ValueSource(org.junit.jupiter.params.provider.ValueSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

Collections (java.util.Collections)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Assertions.assertThatExceptionOfType (org.assertj.core.api.Assertions.assertThatExceptionOfType)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 ValueSource (org.junit.jupiter.params.provider.ValueSource)1 FatalBeanException (org.springframework.beans.FatalBeanException)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1 XmlWebApplicationContext (org.springframework.web.context.support.XmlWebApplicationContext)1 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)1 HandlerInterceptor (org.springframework.web.servlet.HandlerInterceptor)1 HandlerMapping (org.springframework.web.servlet.HandlerMapping)1 BEST_MATCHING_HANDLER_ATTRIBUTE (org.springframework.web.servlet.HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE)1 PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE (org.springframework.web.servlet.HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)1 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)1 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)1 WebUtils (org.springframework.web.util.WebUtils)1