Search in sources :

Example 36 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method httpEntity.

@Test
public void httpEntity() throws ServletException, IOException {
    initServletWithControllers(ResponseEntityController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/foo");
    String requestBody = "Hello World";
    request.setContent(requestBody.getBytes("UTF-8"));
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("Accept", "text/*, */*");
    request.addHeader("MyRequestHeader", "MyValue");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(201, response.getStatus());
    assertEquals(requestBody, response.getContentAsString());
    assertEquals("MyValue", response.getHeader("MyResponseHeader"));
    request = new MockHttpServletRequest("GET", "/bar");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("MyValue", response.getHeader("MyResponseHeader"));
    assertEquals(404, response.getStatus());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 37 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method multipartFileAsSingleString.

@Test
public void multipartFileAsSingleString() throws Exception {
    initServletWithControllers(MultipartController.class);
    MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
    request.setRequestURI("/singleString");
    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 38 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method responseBodyAsHtmlWithSuffixPresent.

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

        @Override
        public void initialize(GenericWebApplicationContext wac) {
            ContentNegotiationManagerFactoryBean factoryBean = new ContentNegotiationManagerFactoryBean();
            factoryBean.afterPropertiesSet();
            RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
            adapterDef.getPropertyValues().add("contentNegotiationManager", factoryBean.getObject());
            wac.registerBeanDefinition("handlerAdapter", adapterDef);
        }
    }, TextRestController.class);
    byte[] content = "alert('boo')".getBytes(StandardCharsets.ISO_8859_1);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/a2.html");
    request.setContent(content);
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals(200, response.getStatus());
    assertEquals("text/html;charset=ISO-8859-1", response.getContentType());
    assertNull(response.getHeader("Content-Disposition"));
    assertArrayEquals(content, response.getContentAsByteArray());
}
Also used : ContentNegotiationManagerFactoryBean(org.springframework.web.accept.ContentNegotiationManagerFactoryBean) 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 39 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method emptyParameterListHandleMethod.

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

        @Override
        public void initialize(GenericWebApplicationContext context) {
            RootBeanDefinition vrDef = new RootBeanDefinition(InternalResourceViewResolver.class);
            vrDef.getPropertyValues().add("suffix", ".jsp");
            context.registerBeanDefinition("viewResolver", vrDef);
        }
    }, EmptyParameterListHandlerMethodController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/emptyParameterListHandler");
    MockHttpServletResponse response = new MockHttpServletResponse();
    EmptyParameterListHandlerMethodController.called = false;
    getServlet().service(request, response);
    assertTrue(EmptyParameterListHandlerMethodController.called);
    assertEquals("", 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) InternalResourceViewResolver(org.springframework.web.servlet.view.InternalResourceViewResolver) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) Test(org.junit.Test)

Example 40 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method requestHeaderMap.

@Test
public void requestHeaderMap() throws Exception {
    initServletWithControllers(RequestHeaderMapController.class);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/map");
    request.addHeader("Content-Type", "text/html");
    request.addHeader("Custom-Header", new String[] { "value21", "value22" });
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("Content-Type=text/html,Custom-Header=value21", response.getContentAsString());
    request.setRequestURI("/multiValueMap");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("Content-Type=[text/html],Custom-Header=[value21,value22]", response.getContentAsString());
    request.setRequestURI("/httpHeaders");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertEquals("Content-Type=[text/html],Custom-Header=[value21,value22]", response.getContentAsString());
}
Also used : MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) 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