Search in sources :

Example 86 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method badRequestRequestBody.

@PathPatternsParameterizedTest
void badRequestRequestBody(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(RequestResponseBodyController.class, usePathPatterns, wac -> {
        RootBeanDefinition adapterDef = new RootBeanDefinition(RequestMappingHandlerAdapter.class);
        adapterDef.getPropertyValues().add("messageConverters", new NotReadableMessageConverter());
        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", "application/pdf");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getStatus()).as("Invalid response status code").isEqualTo(HttpServletResponse.SC_BAD_REQUEST);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 87 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithFieldMarker.

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

Example 88 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithServletPart.

@PathPatternsParameterizedTest
void dataClassBindingWithServletPart(boolean usePathPatterns) throws Exception {
    initDispatcherServlet(ServletPartDataClassController.class, usePathPatterns);
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bind");
    request.setContentType("multipart/form-data");
    request.addPart(new MockPart("param1", "value1".getBytes(StandardCharsets.UTF_8)));
    request.addParameter("param2", "true");
    MockHttpServletResponse response = new MockHttpServletResponse();
    getServlet().service(request, response);
    assertThat(response.getContentAsString()).isEqualTo("value1-true-0");
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockPart(org.springframework.web.testfixture.servlet.MockPart) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) PathPatternsParameterizedTest(org.springframework.web.servlet.handler.PathPatternsParameterizedTest)

Example 89 with MockHttpServletResponse

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

the class ServletAnnotationControllerHandlerMethodTests method dataClassBindingWithMissingParameter.

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

Example 90 with MockHttpServletResponse

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

the class ServletServerHttpResponseTests method create.

@BeforeEach
void create() {
    mockResponse = new MockHttpServletResponse();
    response = new ServletServerHttpResponse(mockResponse);
}
Also used : MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

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