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