Search in sources :

Example 51 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method crud.

@Test
public void crud() throws Exception {
    initServletWithControllers(CrudController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("list", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/hotels/");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("list", response.getContentAsString());
    request = new MockHttpServletRequest("POST", "/hotels");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("create", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/hotels/42");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("show-42", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/hotels/42/");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("show-42", response.getContentAsString());
    request = new MockHttpServletRequest("PUT", "/hotels/42");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("createOrUpdate-42", response.getContentAsString());
    request = new MockHttpServletRequest("DELETE", "/hotels/42");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("remove-42", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 52 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method typeConversionError.

@Test
public void typeConversionError() throws Exception {
    initServletWithControllers(SimpleUriTemplateController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo.xml");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("Invalid response status code", HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 53 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method doIt.

/*
	 * See SPR-6978
	 */
@Test
public void doIt() throws Exception {
    initServletWithControllers(Spr6978Controller.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo/100");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("loadEntity:foo:100", response.getContentAsString());
    request = new MockHttpServletRequest("POST", "/foo/100");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("publish:foo:100", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/module/100");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("loadModule:100", response.getContentAsString());
    request = new MockHttpServletRequest("POST", "/module/100");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("publish:module:100", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 54 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method multiPaths.

@Test
public void multiPaths() throws Exception {
    initServletWithControllers(MultiPathController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/category/page/5");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("handle4-page-5", response.getContentAsString());
    request = new MockHttpServletRequest("GET", "/category/page/5.html");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("handle4-page-5", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 55 with MockHttpServletResponse

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

the class UriTemplateServletAnnotationControllerHandlerMethodTests method pathVarsInModel.

@Test
public void pathVarsInModel() throws Exception {
    final Map<String, Object> pathVars = new HashMap<>();
    pathVars.put("hotel", "42");
    pathVars.put("booking", 21);
    pathVars.put("other", "other");
    WebApplicationContext wac = initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext context) {
            RootBeanDefinition beanDef = new RootBeanDefinition(ModelValidatingViewResolver.class);
            beanDef.getConstructorArgumentValues().addGenericArgumentValue(pathVars);
            context.registerBeanDefinition("viewResolver", beanDef);
        }
    }, ViewRenderingController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hotels/42;q=1,2/bookings/21-other;q=3;r=R");
    getServlet().service(request, new MockHttpServletResponse());
    ModelValidatingViewResolver resolver = wac.getBean(ModelValidatingViewResolver.class);
    assertEquals(3, resolver.validatedAttrCount);
}
Also used : HashMap(java.util.HashMap) 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) WebApplicationContext(org.springframework.web.context.WebApplicationContext) GenericWebApplicationContext(org.springframework.web.context.support.GenericWebApplicationContext) 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