Search in sources :

Example 6 with MockHttpServletResponse

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

the class MarshallingViewTests method renderNullModelValue.

@Test
public void renderNullModelValue() throws Exception {
    String modelKey = "key";
    Map<String, Object> model = new HashMap<>();
    model.put(modelKey, null);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    assertThatIllegalStateException().isThrownBy(() -> view.render(model, request, response));
    assertThat(response.getContentLength()).as("Invalid content length").isEqualTo(0);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 7 with MockHttpServletResponse

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

the class MarshallingViewTests method testRenderUnsupportedModel.

@Test
public void testRenderUnsupportedModel() throws Exception {
    Object toBeMarshalled = new Object();
    String modelKey = "key";
    Map<String, Object> model = new HashMap<>();
    model.put(modelKey, toBeMarshalled);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    given(marshallerMock.supports(Object.class)).willReturn(false);
    assertThatIllegalStateException().isThrownBy(() -> view.render(model, request, response));
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 8 with MockHttpServletResponse

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

the class WebUtilsTests method adaptFromForwardedHeaders.

// SPR-16668
private HttpServletRequest adaptFromForwardedHeaders(HttpServletRequest request) throws Exception {
    MockFilterChain chain = new MockFilterChain();
    new ForwardedHeaderFilter().doFilter(request, new MockHttpServletResponse(), chain);
    return (HttpServletRequest) chain.getRequest();
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ForwardedHeaderFilter(org.springframework.web.filter.ForwardedHeaderFilter) MockFilterChain(org.springframework.web.testfixture.servlet.MockFilterChain) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 9 with MockHttpServletResponse

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

the class HandlerMethodAnnotationDetectionTests method testRequestMappingMethod.

@ParameterizedTest(name = "[{index}] controller [{0}], auto-proxy [{1}]")
@MethodSource("handlerTypes")
void testRequestMappingMethod(Class<?> controllerType, boolean useAutoProxy) throws Exception {
    setUp(controllerType, useAutoProxy);
    String datePattern = "MM:dd:yyyy";
    SimpleDateFormat dateFormat = new SimpleDateFormat(datePattern);
    String dateA = "11:01:2011";
    String dateB = "11:02:2011";
    MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path1/path2");
    request.setParameter("datePattern", datePattern);
    request.addHeader("header1", dateA);
    request.addHeader("header2", dateB);
    HandlerExecutionChain chain = handlerMapping.getHandler(request);
    assertThat(chain).isNotNull();
    ModelAndView mav = handlerAdapter.handle(request, new MockHttpServletResponse(), chain.getHandler());
    assertThat(mav.getModel().get("attr1")).as("model attr1:").isEqualTo(dateFormat.parse(dateA));
    assertThat(mav.getModel().get("attr2")).as("model attr2:").isEqualTo(dateFormat.parse(dateB));
    MockHttpServletResponse response = new MockHttpServletResponse();
    exceptionResolver.resolveException(request, response, chain.getHandler(), new Exception("failure"));
    assertThat(response.getHeader("Content-Type")).isEqualTo("text/plain;charset=ISO-8859-1");
    assertThat(response.getContentAsString()).isEqualTo("failure");
}
Also used : HandlerExecutionChain(org.springframework.web.servlet.HandlerExecutionChain) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) SimpleDateFormat(java.text.SimpleDateFormat) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 10 with MockHttpServletResponse

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

the class HttpEntityMethodProcessorMockTests method setup.

@BeforeEach
@SuppressWarnings("unchecked")
public void setup() throws Exception {
    stringHttpMessageConverter = mock(HttpMessageConverter.class);
    given(stringHttpMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(TEXT_PLAIN));
    resourceMessageConverter = mock(HttpMessageConverter.class);
    given(resourceMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    given(resourceMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
    resourceRegionMessageConverter = mock(HttpMessageConverter.class);
    given(resourceRegionMessageConverter.getSupportedMediaTypes()).willReturn(Collections.singletonList(MediaType.ALL));
    given(resourceRegionMessageConverter.getSupportedMediaTypes(any())).willReturn(Collections.singletonList(MediaType.ALL));
    processor = new HttpEntityMethodProcessor(Arrays.asList(stringHttpMessageConverter, resourceMessageConverter, resourceRegionMessageConverter));
    Method handle1 = getClass().getMethod("handle1", HttpEntity.class, ResponseEntity.class, Integer.TYPE, RequestEntity.class);
    paramHttpEntity = new MethodParameter(handle1, 0);
    paramRequestEntity = new MethodParameter(handle1, 3);
    paramResponseEntity = new MethodParameter(handle1, 1);
    paramInt = new MethodParameter(handle1, 2);
    returnTypeResponseEntity = new MethodParameter(handle1, -1);
    returnTypeResponseEntityProduces = new MethodParameter(getClass().getMethod("handle4"), -1);
    returnTypeHttpEntity = new MethodParameter(getClass().getMethod("handle2", HttpEntity.class), -1);
    returnTypeHttpEntitySubclass = new MethodParameter(getClass().getMethod("handle2x", HttpEntity.class), -1);
    returnTypeInt = new MethodParameter(getClass().getMethod("handle3"), -1);
    returnTypeResponseEntityResource = new MethodParameter(getClass().getMethod("handle5"), -1);
    mavContainer = new ModelAndViewContainer();
    servletRequest = new MockHttpServletRequest("GET", "/foo");
    servletResponse = new MockHttpServletResponse();
    webRequest = new ServletWebRequest(servletRequest, servletResponse);
}
Also used : ModelAndViewContainer(org.springframework.web.method.support.ModelAndViewContainer) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpMessageConverter(org.springframework.http.converter.HttpMessageConverter) Method(java.lang.reflect.Method) HttpMethod(org.springframework.http.HttpMethod) MethodParameter(org.springframework.core.MethodParameter) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) 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