Search in sources :

Example 26 with MockHttpServletResponse

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

the class DefaultRenderingResponseTests method modelAttributeConventions.

@Test
public void modelAttributeConventions() throws Exception {
    RenderingResponse result = RenderingResponse.create("foo").modelAttribute("bar").build();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
    assertThat(mav).isNotNull();
    assertThat(mav.getModel().get("string")).isEqualTo("bar");
}
Also used : 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 27 with MockHttpServletResponse

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

the class DefaultRenderingResponseTests method notModifiedEtag.

@Test
public void notModifiedEtag() throws Exception {
    String etag = "\"foo\"";
    RenderingResponse result = RenderingResponse.create("bar").header(HttpHeaders.ETAG, etag).build();
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "https://example.com");
    request.addHeader(HttpHeaders.IF_NONE_MATCH, etag);
    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 : 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 28 with MockHttpServletResponse

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

the class DefaultRenderingResponseTests method create.

@Test
public void create() throws Exception {
    String name = "foo";
    RenderingResponse result = RenderingResponse.create(name).build();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
    assertThat(mav.getViewName()).isEqualTo(name);
}
Also used : 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 29 with MockHttpServletResponse

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

the class DefaultRenderingResponseTests method modelAttributes.

@Test
public void modelAttributes() throws Exception {
    Map<String, String> model = Collections.singletonMap("foo", "bar");
    RenderingResponse result = RenderingResponse.create("foo").modelAttributes(model).build();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
    assertThat(mav).isNotNull();
    assertThat(mav.getModel().get("foo")).isEqualTo("bar");
}
Also used : 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 30 with MockHttpServletResponse

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

the class DefaultRenderingResponseTests method modelAttribute.

@Test
public void modelAttribute() throws Exception {
    RenderingResponse result = RenderingResponse.create("foo").modelAttribute("foo", "bar").build();
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mav = result.writeTo(request, response, EMPTY_CONTEXT);
    assertThat(mav).isNotNull();
    assertThat(mav.getModel().get("foo")).isEqualTo("bar");
}
Also used : 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)

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