Search in sources :

Example 31 with MockHttpServletRequest

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

the class BeanNameUrlHandlerMappingTests method asteriskMatches.

@Test
public void asteriskMatches() throws Exception {
    HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping");
    Object bean = wac.getBean("godCtrl");
    MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html");
    HandlerExecutionChain hec = hm.getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/mypath/testarossa");
    hec = hm.getHandler(req);
    assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean);
    req = new MockHttpServletRequest("GET", "/mypath/tes");
    hec = hm.getHandler(req);
    assertTrue("Handler is correct bean", hec == null);
}
Also used : HandlerMapping(org.springframework.web.servlet.HandlerMapping) HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Test(org.junit.Test)

Example 32 with MockHttpServletRequest

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

the class HandlerMappingIntrospectorTests method getMatchableWhereHandlerMappingDoesNotImplementMatchableInterface.

@Test(expected = IllegalStateException.class)
public void getMatchableWhereHandlerMappingDoesNotImplementMatchableInterface() throws Exception {
    StaticWebApplicationContext cxt = new StaticWebApplicationContext();
    cxt.registerSingleton("hm1", TestHandlerMapping.class);
    cxt.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest();
    new HandlerMappingIntrospector(cxt).getMatchableHandlerMapping(request);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Test(org.junit.Test)

Example 33 with MockHttpServletRequest

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

the class HandlerMappingIntrospectorTests method getCorsConfigurationActual.

@Test
public void getCorsConfigurationActual() throws Exception {
    AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext();
    cxt.register(TestConfig.class);
    cxt.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path");
    request.addHeader("Origin", "http://localhost:9000");
    CorsConfiguration corsConfig = new HandlerMappingIntrospector(cxt).getCorsConfiguration(request);
    assertNotNull(corsConfig);
    assertEquals(Collections.singletonList("http://localhost:9000"), corsConfig.getAllowedOrigins());
    assertEquals(Collections.singletonList("POST"), corsConfig.getAllowedMethods());
}
Also used : CorsConfiguration(org.springframework.web.cors.CorsConfiguration) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) AnnotationConfigWebApplicationContext(org.springframework.web.context.support.AnnotationConfigWebApplicationContext) Test(org.junit.Test)

Example 34 with MockHttpServletRequest

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

the class HandlerMappingTests method setup.

@Before
public void setup() {
    this.context = new StaticWebApplicationContext();
    this.handlerMapping = new TestHandlerMapping();
    this.request = new MockHttpServletRequest();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) Before(org.junit.Before)

Example 35 with MockHttpServletRequest

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

the class HandlerMethodMappingTests method unregisterMapping.

@Test
public void unregisterMapping() throws Exception {
    String key = "foo";
    HandlerMethod handlerMethod = new HandlerMethod(this.handler, this.method1);
    this.mapping.registerMapping(key, this.handler, this.method1);
    assertNotNull(this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key)));
    this.mapping.unregisterMapping(key);
    assertNull(mapping.getHandlerInternal(new MockHttpServletRequest("GET", key)));
    assertNull(this.mapping.getMappingRegistry().getMappingsByUrl(key));
    assertNull(this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(this.method1.getName()));
    assertNull(this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod));
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) HandlerMethod(org.springframework.web.method.HandlerMethod) 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