Search in sources :

Example 96 with MockHttpServletResponse

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

the class UrlFilenameViewControllerTests method multiLevelWithMapping.

@Test
public void multiLevelWithMapping() throws Exception {
    UrlFilenameViewController ctrl = new UrlFilenameViewController();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/docs/cvs/commit.html");
    exposePathInMapping(request, "/docs/**");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = ctrl.handleRequest(request, response);
    assertEquals("cvs/commit", mv.getViewName());
    assertTrue(mv.getModel().isEmpty());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 97 with MockHttpServletResponse

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

the class UrlFilenameViewControllerTests method nestedPathisUsedAsViewName_InBreakingChangeFromSpring12Line.

/**
	 * This is the expected behavior, and it now has a test to prove it.
	 * http://opensource.atlassian.com/projects/spring/browse/SPR-2789
	 */
@Test
public void nestedPathisUsedAsViewName_InBreakingChangeFromSpring12Line() throws Exception {
    UrlFilenameViewController ctrl = new UrlFilenameViewController();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/products/view.html");
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mv = ctrl.handleRequest(request, response);
    assertEquals("products/view", mv.getViewName());
    assertTrue(mv.getModel().isEmpty());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 98 with MockHttpServletResponse

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

the class ResponseEntityExceptionHandlerTests method setup.

@Before
public void setup() {
    this.servletRequest = new MockHttpServletRequest("GET", "/");
    this.servletResponse = new MockHttpServletResponse();
    this.request = new ServletWebRequest(this.servletRequest, this.servletResponse);
    this.exceptionHandlerSupport = new ApplicationExceptionHandler();
    this.defaultExceptionResolver = new DefaultHandlerExceptionResolver();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) DefaultHandlerExceptionResolver(org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

Example 99 with MockHttpServletResponse

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

the class RequestMappingHandlerAdapterTests method testJsonp.

private void testJsonp(String value, boolean validValue) throws Exception {
    this.request = new MockHttpServletRequest("GET", "/");
    this.request.addHeader("Accept", MediaType.APPLICATION_JSON_VALUE);
    this.request.setParameter("c", value);
    this.response = new MockHttpServletResponse();
    HandlerMethod handlerMethod = handlerMethod(new SimpleController(), "handleWithResponseEntity");
    this.handlerAdapter.afterPropertiesSet();
    this.handlerAdapter.handle(this.request, this.response, handlerMethod);
    assertEquals(200, this.response.getStatus());
    if (validValue) {
        assertEquals("/**/" + value + "({\"foo\":\"bar\"});", this.response.getContentAsString());
    } else {
        assertEquals("{\"foo\":\"bar\"}", this.response.getContentAsString());
    }
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod)

Example 100 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method mavResolver.

@Test
public void mavResolver() throws ServletException, IOException {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            ModelAndViewResolver[] mavResolvers = new ModelAndViewResolver[] { new MyModelAndViewResolver() };
            adapterDef.getPropertyValues().add("modelAndViewResolvers", mavResolvers);
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, ModelAndViewResolverController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("myValue", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockHttpServletResponse (org.springframework.mock.web.test.MockHttpServletResponse)171 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)162 Test (org.junit.Test)140 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)33 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)28 Before (org.junit.Before)19 MockServletContext (org.springframework.mock.web.test.MockServletContext)14 HttpServletResponse (javax.servlet.http.HttpServletResponse)13 ModelAndView (org.springframework.web.servlet.ModelAndView)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)10 TestBean (org.springframework.tests.sample.beans.TestBean)9 HashMap (java.util.HashMap)8 FilterChain (javax.servlet.FilterChain)8 ServletException (javax.servlet.ServletException)7 HttpSession (javax.servlet.http.HttpSession)7 IOException (java.io.IOException)6 Map (java.util.Map)6 ServletRequest (javax.servlet.ServletRequest)6 ServletResponse (javax.servlet.ServletResponse)6