Search in sources :

Example 1 with MockHttpServletResponse

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

the class ServletServerHttpResponseTests method create.

@BeforeEach
void create() {
    mockResponse = new MockHttpServletResponse();
    response = new ServletServerHttpResponse(mockResponse);
}
Also used : MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 2 with MockHttpServletResponse

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

the class ScriptTemplateViewTests method resourceLoaderPath.

// SPR-14210
@Test
public void resourceLoaderPath() throws Exception {
    MockServletContext servletContext = new MockServletContext();
    this.wac.setServletContext(servletContext);
    this.wac.refresh();
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.wac);
    MockHttpServletResponse response = new MockHttpServletResponse();
    Map<String, Object> model = new HashMap<>();
    InvocableScriptEngine engine = mock(InvocableScriptEngine.class);
    given(engine.invokeFunction(any(), any(), any(), any())).willReturn("foo");
    this.view.setEngine(engine);
    this.view.setRenderFunction("render");
    this.view.setApplicationContext(this.wac);
    this.view.setUrl("org/springframework/web/servlet/view/script/empty.txt");
    this.view.render(model, request, response);
    assertThat(response.getContentAsString()).isEqualTo("foo");
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script/");
    this.view.setUrl("empty.txt");
    this.view.render(model, request, response);
    assertThat(response.getContentAsString()).isEqualTo("foo");
    response = new MockHttpServletResponse();
    this.view.setResourceLoaderPath("classpath:org/springframework/web/servlet/view/script");
    this.view.setUrl("empty.txt");
    this.view.render(model, request, response);
    assertThat(response.getContentAsString()).isEqualTo("foo");
}
Also used : HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockServletContext(org.springframework.web.testfixture.servlet.MockServletContext) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 3 with MockHttpServletResponse

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

the class MarshallingViewTests method renderModelKeyWithJaxbElement.

@Test
public void renderModelKeyWithJaxbElement() throws Exception {
    String toBeMarshalled = "value";
    String modelKey = "key";
    view.setModelKey(modelKey);
    Map<String, Object> model = new HashMap<>();
    model.put(modelKey, new JAXBElement<>(new QName("model"), String.class, toBeMarshalled));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    given(marshallerMock.supports(String.class)).willReturn(true);
    marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));
    view.render(model, request, response);
    assertThat(response.getContentType()).as("Invalid content type").isEqualTo("application/xml");
    assertThat(response.getContentLength()).as("Invalid content length").isEqualTo(0);
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) QName(javax.xml.namespace.QName) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 4 with MockHttpServletResponse

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

the class MarshallingViewTests method renderModelKeyUnsupported.

@Test
public void renderModelKeyUnsupported() throws Exception {
    Object toBeMarshalled = new Object();
    String modelKey = "key";
    view.setModelKey(modelKey);
    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 5 with MockHttpServletResponse

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

the class MarshallingViewTests method renderModelKey.

@Test
public void renderModelKey() throws Exception {
    Object toBeMarshalled = new Object();
    String modelKey = "key";
    view.setModelKey(modelKey);
    Map<String, Object> model = new HashMap<>();
    model.put(modelKey, toBeMarshalled);
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    given(marshallerMock.supports(Object.class)).willReturn(true);
    marshallerMock.marshal(eq(toBeMarshalled), isA(StreamResult.class));
    view.render(model, request, response);
    assertThat(response.getContentType()).as("Invalid content type").isEqualTo("application/xml");
    assertThat(response.getContentLength()).as("Invalid content length").isEqualTo(0);
}
Also used : StreamResult(javax.xml.transform.stream.StreamResult) 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)

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