Search in sources :

Example 71 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method defaultExpressionParameters.

@Test
public void defaultExpressionParameters() throws Exception {
    initServlet(new ApplicationContextInitializer<GenericWebApplicationContext>() {

        @Override
        public void initialize(GenericWebApplicationContext context) {
            RootBeanDefinition ppc = new RootBeanDefinition(PropertyPlaceholderConfigurer.class);
            ppc.getPropertyValues().add("properties", "myKey=foo");
            context.registerBeanDefinition("ppc", ppc);
        }
    }, DefaultExpressionValueParamController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/myApp/myPath.do");
    request.setContextPath("/myApp");
    MockHttpServletResponse response = new MockHttpServletResponse();
    System.setProperty("myHeader", "bar");
    try {
        getServlet().service(request, response);
    } finally {
        System.clearProperty("myHeader");
    }
    assertEquals("foo-bar-/myApp", response.getContentAsString());
}
Also used : PropertyPlaceholderConfigurer(org.springframework.beans.factory.config.PropertyPlaceholderConfigurer) 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)

Example 72 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method trailingSlash.

@Test
public void trailingSlash() throws Exception {
    initServletWithControllers(TrailingSlashController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("templatePath", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 73 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method multipartFileAsStringArray.

@Test
public void multipartFileAsStringArray() throws Exception {
    initServletWithControllers(MultipartController.class);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setRequestURI("/stringArray");
    request.addFile(new MockMultipartFile("content", "Juergen".getBytes()));
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("Juergen", response.getContentAsString());
}
Also used : MockMultipartFile(org.springframework.mock.web.test.MockMultipartFile) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 74 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method requestMappingBaseClass.

@Test
public void requestMappingBaseClass() throws Exception {
    initServletWithControllers(MyAbstractControllerImpl.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/handle");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("handle", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 75 with MockHttpServletResponse

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

the class ResourceHttpRequestHandlerTests method setUp.

@Before
public void setUp() throws Exception {
    dateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
    List<Resource> paths = new ArrayList<>(2);
    paths.add(new ClassPathResource("test/", getClass()));
    paths.add(new ClassPathResource("testalternatepath/", getClass()));
    paths.add(new ClassPathResource("META-INF/resources/webjars/"));
    this.handler = new ResourceHttpRequestHandler();
    this.handler.setLocations(paths);
    this.handler.setCacheSeconds(3600);
    this.handler.setServletContext(new TestServletContext());
    this.handler.afterPropertiesSet();
    this.request = new MockHttpServletRequest("GET", "");
    this.response = new MockHttpServletResponse();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) ArrayList(java.util.ArrayList) UrlResource(org.springframework.core.io.UrlResource) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) SimpleDateFormat(java.text.SimpleDateFormat) ClassPathResource(org.springframework.core.io.ClassPathResource) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Before(org.junit.Before)

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