Search in sources :

Example 36 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class HandlerMethodMappingTests method patternMatch.

@Test
public void patternMatch() throws Exception {
    this.mapping.registerMapping("/fo*", this.handler, this.method1);
    this.mapping.registerMapping("/f*", this.handler, this.method2);
    HandlerMethod result = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
    assertEquals(method1, result.getMethod());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 37 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class HandlerMethodMappingTests method getCorsConfigWithBeanNameHandler.

@Test
public void getCorsConfigWithBeanNameHandler() throws Exception {
    String key = "foo";
    String beanName = "handler1";
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    context.registerSingleton(beanName, MyHandler.class);
    this.mapping.setApplicationContext(context);
    this.mapping.registerMapping(key, beanName, this.method1);
    HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key));
    CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod);
    assertNotNull(config);
    assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0));
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) HandlerMethod(org.springframework.web.method.HandlerMethod) Test(org.junit.Test)

Example 38 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class HandlerMethodMappingTests method ambiguousMatch.

@Test(expected = IllegalStateException.class)
public void ambiguousMatch() throws Exception {
    this.mapping.registerMapping("/f?o", this.handler, this.method1);
    this.mapping.registerMapping("/fo?", this.handler, this.method2);
    this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo"));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 39 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class PathMatchingUrlHandlerMappingTests method actualPathMatching.

@Test
public void actualPathMatching() throws Exception {
    // there a couple of mappings defined with which we can test the
    // path matching, let's do that...
    Object bean = wac.getBean("mainController");
    Object defaultBean = wac.getBean("starController");
    // testing some normal behavior
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/pathmatchingTest.html");
    HandlerExecutionChain hec = getHandler(req);
    assertTrue("Handler is null", hec != null);
    assertTrue("Handler is correct bean", hec.getHandler() == bean);
    assertEquals("/pathmatchingTest.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // no match, no forward slash included
    req = new MockHttpServletRequest("GET", "welcome.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("welcome.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // testing some ????? behavior
    req = new MockHttpServletRequest("GET", "/pathmatchingAA.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("pathmatchingAA.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // testing some ????? behavior
    req = new MockHttpServletRequest("GET", "/pathmatchingA.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    assertEquals("/pathmatchingA.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
    // testing some ????? behavior
    req = new MockHttpServletRequest("GET", "/administrator/pathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // testing simple /**/behavior
    req = new MockHttpServletRequest("GET", "/administrator/test/pathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // this should not match because of the administratorT
    req = new MockHttpServletRequest("GET", "/administratort/pathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    // this should match because of *.jsp
    req = new MockHttpServletRequest("GET", "/bla.jsp");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // should match because exact pattern is there
    req = new MockHttpServletRequest("GET", "/administrator/another/bla.xml");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // should not match, because there's not .gif extension in there
    req = new MockHttpServletRequest("GET", "/administrator/another/bla.gif");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    // should match because there testlast* in there
    req = new MockHttpServletRequest("GET", "/administrator/test/testlastbit");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    // but this not, because it's testlast and not testla
    req = new MockHttpServletRequest("GET", "/administrator/test/testla");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/administrator/testing/longer/bla");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/administrator/testing/longer/test.jsp");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/administrator/testing/longer2/notmatching/notmatching");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/shortpattern/testing/toolong");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/XXpathXXmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/pathXXmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/XpathXXmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/XXpathmatching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/show12.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show123.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/show1.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/reallyGood-test-is-this.jpeg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/reallyGood-tst-is-this.jpeg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/testing/test.jpeg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/testing/test.jpg");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/anotherTest");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/stillAnotherTest");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    // there outofpattern*yeah in the pattern, so this should fail
    req = new MockHttpServletRequest("GET", "/outofpattern*ye");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/test't est/path'm atching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
    req = new MockHttpServletRequest("GET", "/test%26t%20est/path%26m%20atching.html");
    hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == defaultBean);
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 40 with MockHttpServletRequest

use of org.springframework.mock.web.test.MockHttpServletRequest in project spring-framework by spring-projects.

the class PathMatchingUrlHandlerMappingTests method mappingExposedInRequest.

@Test
public void mappingExposedInRequest() throws Exception {
    Object bean = wac.getBean("mainController");
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/show.html");
    HandlerExecutionChain hec = getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    assertEquals("Mapping not exposed", "show.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE));
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Aggregations

MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)646 Test (org.junit.Test)540 MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)330 Before (org.junit.Before)78 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)77 MockServletContext (org.springframework.mock.web.test.MockServletContext)56 TestBean (org.springframework.tests.sample.beans.TestBean)43 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)41 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)37 HttpServletRequest (javax.servlet.http.HttpServletRequest)35 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)30 ServletServerHttpRequest (org.springframework.http.server.ServletServerHttpRequest)29 ITestBean (org.springframework.tests.sample.beans.ITestBean)28 HandlerExecutionChain (org.springframework.web.servlet.HandlerExecutionChain)28 ServletException (javax.servlet.ServletException)27 HashMap (java.util.HashMap)26 FilterChain (javax.servlet.FilterChain)26 ModelAndViewContainer (org.springframework.web.method.support.ModelAndViewContainer)23 Cookie (javax.servlet.http.Cookie)22