Search in sources :

Example 31 with MockHttpServletResponse

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");
}
Also used : Cookie(jakarta.servlet.http.Cookie) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) HttpHeaders(org.springframework.http.HttpHeaders) ZonedDateTime(java.time.ZonedDateTime) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MultiValueMap(org.springframework.util.MultiValueMap) Test(org.junit.jupiter.api.Test) HttpStatus(org.springframework.http.HttpStatus) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Cookie(jakarta.servlet.http.Cookie) ChronoUnit(java.time.temporal.ChronoUnit) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) Collections(java.util.Collections) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 32 with MockHttpServletResponse

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());
}
Also used : ZonedDateTime(java.time.ZonedDateTime) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 33 with MockHttpServletResponse

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();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) RequestMappingHandlerMapping(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping) RequestMappingHandlerAdapter(org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse)

Example 34 with MockHttpServletResponse

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();
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletConfig(org.springframework.web.testfixture.servlet.MockServletConfig) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 35 with MockHttpServletResponse

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);
}
Also used : MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

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