use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class ResponseSerializerTest method serializeBodyJsonMap.
@Test
public void serializeBodyJsonMap() throws Exception {
final Map<String, Object> jsonObject = new HashMap<>();
jsonObject.put("key1", "value1");
jsonObject.put("key2", true);
jsonObject.put("key3", 42);
final WebRequest req = new WebRequest();
req.setMethod(HttpMethod.GET);
final WebResponse resp = WebResponse.create().status(HttpStatus.ACCEPTED).contentType(MediaType.PLAIN_TEXT_UTF_8).header("header-test", "header-value").cookie(new Cookie("cookie-name", "cookie-value")).body(jsonObject).build();
final ResponseSerializer serializer = new ResponseSerializer(req, resp);
final HttpServletResponse httpResponse = mock(HttpServletResponse.class);
final ServletOutputStream servletOutputStream = mock(ServletOutputStream.class);
when(httpResponse.getOutputStream()).thenReturn(servletOutputStream);
serializer.serialize(httpResponse);
verify(httpResponse).setHeader("header-test", "header-value");
verify(httpResponse).setStatus(202);
verify(httpResponse).setContentType("text/plain; charset=utf-8");
verify(httpResponse).setContentLength(39);
verify(servletOutputStream).write(any(byte[].class));
}
use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class ResponseSerializerTest method serializeBodyBytes.
@Test
public void serializeBodyBytes() throws Exception {
final WebRequest req = new WebRequest();
req.setMethod(HttpMethod.GET);
final byte[] body_bytes = "String body".getBytes(StandardCharsets.UTF_8);
final WebResponse resp = WebResponse.create().status(HttpStatus.ACCEPTED).contentType(MediaType.PLAIN_TEXT_UTF_8).header("header-test", "header-value").cookie(new Cookie("cookie-name", "cookie-value")).body(body_bytes).build();
final ResponseSerializer serializer = new ResponseSerializer(req, resp);
final HttpServletResponse httpResponse = mock(HttpServletResponse.class);
final ServletOutputStream servletOutputStream = mock(ServletOutputStream.class);
when(httpResponse.getOutputStream()).thenReturn(servletOutputStream);
serializer.serialize(httpResponse);
verify(httpResponse).setHeader("header-test", "header-value");
verify(httpResponse).setStatus(202);
verify(httpResponse).setContentType("text/plain; charset=utf-8");
verify(httpResponse).setContentLength(11);
verify(servletOutputStream).write(body_bytes);
}
use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class ResponseSerializerTest method serializeRequestCommitted.
@Test
public void serializeRequestCommitted() throws Exception {
final WebRequest req = new WebRequest();
req.setMethod(HttpMethod.GET);
final WebResponse resp = WebResponse.create().status(HttpStatus.OK).contentType(MediaType.PLAIN_TEXT_UTF_8).body("String body").build();
final ResponseSerializer serializer = new ResponseSerializer(req, resp);
final HttpServletResponse httpResponse = mock(HttpServletResponse.class);
when(httpResponse.isCommitted()).thenReturn(true);
serializer.serialize(httpResponse);
verify(httpResponse).isCommitted();
verifyNoMoreInteractions(httpResponse);
}
Aggregations