use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class ResponseSerializerTest method serializeBodyResource.
@Test
public void serializeBodyResource() throws Exception {
final ResourceKey resourceKey = ResourceKey.from("myapp:/site/test/view/body_file.txt");
final URL resourceUrl = ResponseSerializerTest.class.getResource("body_file.txt");
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(new UrlResource(resourceKey, resourceUrl)).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(any(byte[].class));
}
use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class ResponseSerializerTest method serializeBodyString.
@Test
public void serializeBodyString() throws Exception {
final WebRequest req = new WebRequest();
req.setMethod(HttpMethod.GET);
final String string_body = "String body";
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(string_body).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(string_body.getBytes(StandardCharsets.UTF_8));
}
use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class IdentityHandlerTest method testHandle.
@Test
public void testHandle() throws Exception {
final WebResponse portalResponse = this.handler.handle(this.request, PortalResponse.create().build(), null);
assertEquals(HttpStatus.OK, portalResponse.getStatus());
assertEquals(HttpStatus.OK, portalResponse.getStatus());
assertEquals("/site/draft/_/idprovider/myidprovider", this.request.getContextPath());
}
use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class ImageHandlerTest method get.
@Test
void get() throws Exception {
mockCachableContent();
this.request.setEndpointPath("/_/image/123456/scale-100-100/image-name.jpg.png");
final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
assertAll(() -> assertEquals(HttpStatus.OK, res.getStatus()), () -> assertEquals(MediaType.PNG, res.getContentType()), () -> assertThat(res.getHeaders()).doesNotContainKey("Cache-Control"));
}
use of com.enonic.xp.web.WebResponse in project xp by enonic.
the class ImageHandlerTest method svgImage.
@Test
void svgImage() throws Exception {
setupContentSvg();
when(this.mediaInfoService.getImageOrientation(any(ByteSource.class))).thenReturn(ImageOrientation.LeftBottom);
this.request.setEndpointPath("/_/image/123456/full/image-name.svg");
final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
assertNotNull(res);
assertEquals(HttpStatus.OK, res.getStatus());
assertEquals(MediaType.SVG_UTF_8.withoutParameters(), res.getContentType());
assertTrue(res.getBody() instanceof ByteSource);
assertNull(res.getHeaders().get("Content-Encoding"));
assertEquals("default-src 'none'; base-uri 'none'; form-action 'none'; style-src 'self' 'unsafe-inline'", res.getHeaders().get("Content-Security-Policy"));
}
Aggregations