Search in sources :

Example 46 with PortalResponse

use of com.enonic.xp.portal.PortalResponse in project xp by enonic.

the class AttachmentHandlerTest method byteServing.

@Test
void byteServing() throws Exception {
    this.request.setEndpointPath("/_/attachment/inline/123456/logo.png");
    this.request.getHeaders().put("Range", "bytes=2-4");
    final PortalResponse res = (PortalResponse) this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.PARTIAL_CONTENT, res.getStatus());
    assertEquals(MediaType.PNG.withoutParameters(), res.getContentType());
    assertEquals("bytes", res.getHeaders().get("accept-ranges"));
    assertNull(res.getHeaders().get("Content-Disposition"));
    final byte[] responseBody = ((ByteSource) res.getBody()).read();
    final byte[] mediaBytesData = mediaBytes.read();
    assertEquals(3, responseBody.length);
    assertArrayEquals(new byte[] { mediaBytesData[2], mediaBytesData[3], mediaBytesData[4] }, responseBody);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ByteSource(com.google.common.io.ByteSource) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 47 with PortalResponse

use of com.enonic.xp.portal.PortalResponse in project xp by enonic.

the class AttachmentHandlerTest method byteServingInvalidRange.

@Test
void byteServingInvalidRange() throws Exception {
    this.request.setEndpointPath("/_/attachment/inline/123456/logo.png");
    this.request.getHeaders().put("Range", "bytes=many");
    final PortalResponse res = (PortalResponse) this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.REQUESTED_RANGE_NOT_SATISFIABLE, res.getStatus());
    assertEquals(MediaType.PNG.withoutParameters(), res.getContentType());
    assertEquals("bytes", res.getHeaders().get("accept-ranges"));
    assertNull(res.getBody());
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 48 with PortalResponse

use of com.enonic.xp.portal.PortalResponse in project xp by enonic.

the class AttachmentHandlerTest method byteServingSuffixLength.

@Test
void byteServingSuffixLength() throws Exception {
    this.request.setEndpointPath("/_/attachment/inline/123456/logo.png");
    this.request.getHeaders().put("Range", "bytes=-3");
    final PortalResponse res = (PortalResponse) this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.PARTIAL_CONTENT, res.getStatus());
    assertEquals(MediaType.PNG.withoutParameters(), res.getContentType());
    assertEquals("bytes", res.getHeaders().get("accept-ranges"));
    assertNull(res.getHeaders().get("Content-Disposition"));
    final byte[] responseBody = ((ByteSource) res.getBody()).read();
    final byte[] mediaBytesData = mediaBytes.read();
    assertEquals(4, responseBody.length);
    assertArrayEquals(new byte[] { mediaBytesData[3], mediaBytesData[4], mediaBytesData[5], mediaBytesData[6] }, responseBody);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ByteSource(com.google.common.io.ByteSource) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 49 with PortalResponse

use of com.enonic.xp.portal.PortalResponse in project xp by enonic.

the class AttachmentHandlerTest method cacheHeader_fingerprint_missmatch.

@Test
void cacheHeader_fingerprint_missmatch() throws Exception {
    this.request.setEndpointPath("/_/attachment/inline/123456:123456/logo.png");
    final PortalResponse res = (PortalResponse) this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertThat(res.getHeaders()).doesNotContainKey("Cache-Control");
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 50 with PortalResponse

use of com.enonic.xp.portal.PortalResponse in project xp by enonic.

the class AttachmentHandlerTest method byteServingSuffixFrom.

@Test
void byteServingSuffixFrom() throws Exception {
    this.request.setEndpointPath("/_/attachment/inline/123456/logo.png");
    this.request.getHeaders().put("Range", "bytes=4-");
    final PortalResponse res = (PortalResponse) this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.PARTIAL_CONTENT, res.getStatus());
    assertEquals(MediaType.PNG.withoutParameters(), res.getContentType());
    assertEquals("bytes", res.getHeaders().get("accept-ranges"));
    assertNull(res.getHeaders().get("Content-Disposition"));
    final byte[] responseBody = ((ByteSource) res.getBody()).read();
    final byte[] mediaBytesData = mediaBytes.read();
    assertEquals(3, responseBody.length);
    assertArrayEquals(new byte[] { mediaBytesData[4], mediaBytesData[5], mediaBytesData[6] }, responseBody);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) ByteSource(com.google.common.io.ByteSource) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Aggregations

PortalResponse (com.enonic.xp.portal.PortalResponse)104 Test (org.junit.jupiter.api.Test)78 PortalRequest (com.enonic.xp.portal.PortalRequest)21 WebException (com.enonic.xp.web.WebException)14 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)14 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)13 PostProcessInstruction (com.enonic.xp.portal.postprocess.PostProcessInstruction)10 WebResponse (com.enonic.xp.web.WebResponse)10 ContentService (com.enonic.xp.content.ContentService)9 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 RenderMode (com.enonic.xp.portal.RenderMode)8 HtmlTag (com.enonic.xp.portal.postprocess.HtmlTag)8 PostProcessInjection (com.enonic.xp.portal.postprocess.PostProcessInjection)8 ByteSource (com.google.common.io.ByteSource)8 InputStream (java.io.InputStream)8 StandardCharsets (java.nio.charset.StandardCharsets)8 Arrays (java.util.Arrays)8 Collections (java.util.Collections)8 List (java.util.List)8