Search in sources :

Example 6 with HandlerMapping

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

the class HandlerMappingIntrospectorTests method detectHandlerMappingsOrdered.

@Test
public void detectHandlerMappingsOrdered() throws Exception {
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    MutablePropertyValues pvs = new MutablePropertyValues(Collections.singletonMap("order", "3"));
    cxt.registerSingleton("hmA", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues(Collections.singletonMap("order", "2"));
    cxt.registerSingleton("hmB", SimpleUrlHandlerMapping.class, pvs);
    pvs = new MutablePropertyValues(Collections.singletonMap("order", "1"));
    cxt.registerSingleton("hmC", SimpleUrlHandlerMapping.class, pvs);
    cxt.refresh();
    List<?> expected = Arrays.asList(cxt.getBean("hmC"), cxt.getBean("hmB"), cxt.getBean("hmA"));
    List<HandlerMapping> actual = new HandlerMappingIntrospector(cxt).getHandlerMappings();
    assertEquals(expected, actual);
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 7 with HandlerMapping

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

the class HandlerMappingIntrospectorTests method detectHandlerMappings.

@Test
public void detectHandlerMappings() throws Exception {
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    cxt.registerSingleton("hmA", SimpleUrlHandlerMapping.class);
    cxt.registerSingleton("hmB", SimpleUrlHandlerMapping.class);
    cxt.registerSingleton("hmC", SimpleUrlHandlerMapping.class);
    cxt.refresh();
    List<?> expected = Arrays.asList(cxt.getBean("hmA"), cxt.getBean("hmB"), cxt.getBean("hmC"));
    List<HandlerMapping> actual = new HandlerMappingIntrospector(cxt).getHandlerMappings();
    assertEquals(expected, actual);
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 8 with HandlerMapping

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

the class BeanNameUrlHandlerMappingTests method requestsWithSubPaths.

@Test
public void requestsWithSubPaths() throws Exception {
    HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
    doTestRequestsWithSubPaths(hm);
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) Test(org.junit.Test)

Example 9 with HandlerMapping

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

the class BeanNameUrlHandlerMappingTests method requestsWithoutHandlers.

@Test
public void requestsWithoutHandlers() throws Exception {
    HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html");
    req.setContextPath("/myapp");
    Object h = hm.getHandler(req);
    assertTrue("Handler is null", h == null);
    req = new MockHttpServletRequest("GET", "/foo/bar/baz.html");
    h = hm.getHandler(req);
    assertTrue("Handler is null", h == null);
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 10 with HandlerMapping

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

the class SimpleUrlHandlerMappingTests method checkMappings.

private void checkMappings(String beanName) throws Exception {
    MockServletContext sc = new MockServletContext("");
    XmlWebApplicationContext wac = new XmlWebApplicationContext();
    wac.setServletContext(sc);
    wac.setConfigLocations(new String[] { "/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);
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html");
    HandlerExecutionChain hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome.x");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome.x", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/welcome/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == otherBean);
    assertEquals("welcome", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/");
    req.setServletPath("/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/welcome.html");
    req.setContextPath("/app");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-welcome.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/welcome.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-show.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/show.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/original-bookseats.html");
    req.setAttribute(WebUtils.INCLUDE_REQUEST_URI_ATTRIBUTE, "/bookseats.html");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("/", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    req = new MockHttpServletRequest("GET", "/somePath");
    hec = getHandler(hm, req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("/somePath", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
Also used : XmlWebApplicationContext(org.springframework.web.context.support.XmlWebApplicationContext) HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockServletContext(org.springframework.mock.web.test.MockServletContext)

Aggregations

HandlerMapping (org.springframework.web.servlet.HandlerMapping)13 Test (org.junit.Test)11 SimpleUrlHandlerMapping (org.springframework.web.servlet.handler.SimpleUrlHandlerMapping)5 WebSocketHttpRequestHandler (org.springframework.web.socket.server.support.WebSocketHttpRequestHandler)4 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)3 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)3 RequestMappingHandlerMapping (org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping)3 HandshakeHandler (org.springframework.web.socket.server.HandshakeHandler)3 OriginHandshakeInterceptor (org.springframework.web.socket.server.support.OriginHandshakeInterceptor)3 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)2 MessageHandler (org.springframework.messaging.MessageHandler)2 SimpAnnotationMethodMessageHandler (org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler)2 SimpleBrokerMessageHandler (org.springframework.messaging.simp.broker.SimpleBrokerMessageHandler)2 StompBrokerRelayMessageHandler (org.springframework.messaging.simp.stomp.StompBrokerRelayMessageHandler)2 DefaultUserDestinationResolver (org.springframework.messaging.simp.user.DefaultUserDestinationResolver)2 SimpUserRegistry (org.springframework.messaging.simp.user.SimpUserRegistry)2 UserDestinationMessageHandler (org.springframework.messaging.simp.user.UserDestinationMessageHandler)2 UserDestinationResolver (org.springframework.messaging.simp.user.UserDestinationResolver)2 UserRegistryMessageHandler (org.springframework.messaging.simp.user.UserRegistryMessageHandler)2 HttpRequestHandler (org.springframework.web.HttpRequestHandler)2