Search in sources :

Example 71 with MockHttpServletResponse

use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithOptionalParameter.

@PathPatternsParameterizedTest
void dataClassBindingWithOptionalParameter(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ValidatedDataClassController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/bind");
    request.addParameter("param1", "value1");
    request.addParameter("param2", "true");
    request.addParameter("optionalParam", "8");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("value1-true-8");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 72 with MockHttpServletResponse

use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method responseBodyNoAcceptableMediaType.

@PathPatternsParameterizedTest
void responseBodyNoAcceptableMediaType(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestResponseBodyProducesController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        StringHttpMessageConverter converter = new StringHttpMessageConverter();
        adapterDef.getPropertyValues().add("messageConverters", converter);
        wac.registerBeanDefinition("handlerAdapter", adapterDef);
    });
    MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
    String requestBody = "Hello World";
    request.setContent(requestBody.getBytes(StandardCharsets.UTF_8));
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("Accept", "application/pdf, application/msword");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(406);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) StringHttpMessageConverter(org.springframework.http.converter.StringHttpMessageConverter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 73 with MockHttpServletResponse

use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method httpHead.

@PathPatternsParameterizedTest
void httpHead(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ResponseEntityController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("HEAD", "/baz");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getHeader("MyResponseHeader")).isEqualTo("MyValue");
    assertThat(response.getContentLength()).isEqualTo(4);
    assertThat(response.getContentAsByteArray().length == 0).isTrue();
    // Now repeat with GET
    request = new MockHttpServletRequest("GET", "/baz");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getHeader("MyResponseHeader")).isEqualTo("MyValue");
    assertThat(response.getContentLength()).isEqualTo(4);
    assertThat(response.getContentAsString()).isEqualTo("body");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 74 with MockHttpServletResponse

use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method contentTypeHeaders.

@PathPatternsParameterizedTest
void contentTypeHeaders(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ContentTypeHeadersController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/something");
    request.setContentType("application/pdf");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("pdf");
    request = new MockHttpServletRequest("POST", "/something");
    request.setContentType("text/html");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("text");
    request = new MockHttpServletRequest("POST", "/something");
    request.setContentType("application/xml");
    response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).isEqualTo(415);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 75 with MockHttpServletResponse

use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.

the class ServletAnnotationControllerHandlerMethodTests method restController.

@PathPatternsParameterizedTest
void restController(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ThisWillActuallyRun.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("Hello World!");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Aggregations

MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)415 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)364 Test (org.junit.jupiter.api.Test)203 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)143 BeforeEach (org.junit.jupiter.api.BeforeEach)54 ModelAndView (org.springframework.web.servlet.ModelAndView)47 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)38 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)36 ServletWebRequest (org.springframework.web.context.request.ServletWebRequest)29 HashMap (java.util.HashMap)27 FilterChain (jakarta.servlet.FilterChain)24 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)24 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)23 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)23 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)20 ServletException (jakarta.servlet.ServletException)18 Cookie (jakarta.servlet.http.Cookie)16 Locale (java.util.Locale)15 MockFilterConfig (org.springframework.web.testfixture.servlet.MockFilterConfig)15 MockServletConfig (org.springframework.web.testfixture.servlet.MockServletConfig)13