Search in sources :

Example 26 with WebResponse

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));
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) UrlResource(com.enonic.xp.resource.UrlResource) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) URL(java.net.URL) ResourceKey(com.enonic.xp.resource.ResourceKey) Test(org.junit.jupiter.api.Test)

Example 27 with WebResponse

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));
}
Also used : Cookie(javax.servlet.http.Cookie) WebResponse(com.enonic.xp.web.WebResponse) WebRequest(com.enonic.xp.web.WebRequest) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 28 with WebResponse

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());
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 29 with WebResponse

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"));
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 30 with WebResponse

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"));
}
Also used : WebResponse(com.enonic.xp.web.WebResponse) ByteSource(com.google.common.io.ByteSource) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Aggregations

WebResponse (com.enonic.xp.web.WebResponse)63 Test (org.junit.jupiter.api.Test)53 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)32 PortalResponse (com.enonic.xp.portal.PortalResponse)11 WebRequest (com.enonic.xp.web.WebRequest)9 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 ByteSource (com.google.common.io.ByteSource)6 Cookie (javax.servlet.http.Cookie)6 ResourceKey (com.enonic.xp.resource.ResourceKey)5 WebException (com.enonic.xp.web.WebException)5 ServletOutputStream (javax.servlet.ServletOutputStream)5 PortalRequest (com.enonic.xp.portal.PortalRequest)4 Content (com.enonic.xp.content.Content)2 PropertyTree (com.enonic.xp.data.PropertyTree)2 PageUrlParams (com.enonic.xp.portal.url.PageUrlParams)2 MockResource (com.enonic.xp.resource.MockResource)2 Resource (com.enonic.xp.resource.Resource)2 ControllerMappingDescriptor (com.enonic.xp.site.mapping.ControllerMappingDescriptor)2 Trace (com.enonic.xp.trace.Trace)2 VirtualHost (com.enonic.xp.web.vhost.VirtualHost)2