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);
}
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));
}
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();
}
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");
}
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);
}
Aggregations