use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class DefaultRenderingResponseTests method cookies.
@Test
public void cookies() throws Exception {
MultiValueMap<String, Cookie> newCookies = new LinkedMultiValueMap<>();
newCookies.add("name", new Cookie("name", "value"));
RenderingResponse result = RenderingResponse.create("foo").cookies(cookies -> cookies.addAll(newCookies)).build();
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
assertThat(mav).isNotNull();
assertThat(response.getCookies().length).isEqualTo(1);
assertThat(response.getCookies()[0].getName()).isEqualTo("name");
assertThat(response.getCookies()[0].getValue()).isEqualTo("value");
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class DefaultRenderingResponseTests method notModifiedLastModified.
@Test
public void notModifiedLastModified() throws Exception {
ZonedDateTime now = ZonedDateTime.now();
ZonedDateTime oneMinuteBeforeNow = now.minus(1, ChronoUnit.MINUTES);
RenderingResponse result = RenderingResponse.create("bar").header(HttpHeaders.LAST_MODIFIED, DateTimeFormatter.RFC_1123_DATE_TIME.format(oneMinuteBeforeNow)).build();
MockHttpServletRequest request = new MockHttpServletRequest("GET", "https://example.com");
request.addHeader(HttpHeaders.IF_MODIFIED_SINCE, DateTimeFormatter.RFC_1123_DATE_TIME.format(now));
MockHttpServletResponse response = new MockHttpServletResponse();
ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
assertThat(mav).isNull();
assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_MODIFIED.value());
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class MvcNamespaceTests method doTestCustomValidator.
private void doTestCustomValidator(String xml) throws Exception {
loadBeanDefinitions(xml);
RequestMappingHandlerMapping mapping = appContext.getBean(RequestMappingHandlerMapping.class);
assertThat(mapping).isNotNull();
assertThat(mapping.getUrlPathHelper().shouldRemoveSemicolonContent()).isFalse();
RequestMappingHandlerAdapter adapter = appContext.getBean(RequestMappingHandlerAdapter.class);
assertThat(adapter).isNotNull();
assertThat(new DirectFieldAccessor(adapter).getPropertyValue("ignoreDefaultModelOnRedirect")).asInstanceOf(BOOLEAN).isTrue();
// default web binding initializer behavior test
MockHttpServletRequest request = new MockHttpServletRequest();
request.addParameter("date", "2009-10-31");
MockHttpServletResponse response = new MockHttpServletResponse();
adapter.handle(request, response, handlerMethod);
assertThat(appContext.getBean(TestValidator.class).validatorInvoked).isTrue();
assertThat(handler.recordedValidationError).isFalse();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class DispatcherServletTests method throwExceptionIfNoHandlerFound.
@Test
public void throwExceptionIfNoHandlerFound() throws ServletException, IOException {
DispatcherServlet complexDispatcherServlet = new DispatcherServlet();
complexDispatcherServlet.setContextClass(SimpleWebApplicationContext.class);
complexDispatcherServlet.setNamespace("test");
complexDispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
complexDispatcherServlet.init(new MockServletConfig(getServletContext(), "complex"));
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/unknown");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
assertThat(response.getStatus() == HttpServletResponse.SC_NOT_FOUND).as("correct error code").isTrue();
}
use of org.springframework.web.testfixture.servlet.MockHttpServletResponse in project spring-framework by spring-projects.
the class DispatcherServletTests method requestHandledEvent.
@Test
public void requestHandledEvent() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest(getServletContext(), "GET", "/locale.do");
MockHttpServletResponse response = new MockHttpServletResponse();
complexDispatcherServlet.service(request, response);
ComplexWebApplicationContext.TestApplicationListener listener = (ComplexWebApplicationContext.TestApplicationListener) complexDispatcherServlet.getWebApplicationContext().getBean("testListener");
assertThat(listener.counter).isEqualTo(1);
}
Aggregations