Search in sources :

Example 66 with PortalResponse

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

the class AttachmentHandlerTest method byteServingLongerThanFile.

@Test
void byteServingLongerThanFile() throws Exception {
    this.request.setEndpointPath("/_/attachment/inline/123456/logo.png");
    this.request.getHeaders().put("Range", "bytes=5-1000");
    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(2, responseBody.length);
    assertArrayEquals(new byte[] { 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 67 with PortalResponse

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

the class AttachmentHandlerTest method byteServingMultipleRanges.

@Test
void byteServingMultipleRanges() throws Exception {
    this.request.setEndpointPath("/_/attachment/inline/123456/logo.png");
    this.request.getHeaders().put("Range", "bytes=0-1,3-4,6-");
    final PortalResponse res = (PortalResponse) this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.PARTIAL_CONTENT, res.getStatus());
    assertEquals(MediaType.parse("multipart/byteranges"), res.getContentType().withoutParameters());
    assertEquals("bytes", res.getHeaders().get("accept-ranges"));
    final byte[] responseBody = ((ByteSource) res.getBody()).read();
    final String responseMultipartString = new String(responseBody, StandardCharsets.UTF_8);
    String[] responseMultipartLines = responseMultipartString.split("\\r?\\n");
    assertEquals("Content-Type: image/png", responseMultipartLines[2]);
    assertEquals("Content-Range: bytes 0-1/7", responseMultipartLines[3]);
    assertEquals("01", responseMultipartLines[5]);
    assertEquals("Content-Type: image/png", responseMultipartLines[7]);
    assertEquals("Content-Range: bytes 3-4/7", responseMultipartLines[8]);
    assertEquals("34", responseMultipartLines[10]);
    assertEquals("Content-Type: image/png", responseMultipartLines[12]);
    assertEquals("Content-Range: bytes 6-6/7", responseMultipartLines[13]);
    assertEquals("6", responseMultipartLines[15]);
}
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 68 with PortalResponse

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

the class AttachmentHandlerTest method cacheHeader_draft_branch.

@Test
void cacheHeader_draft_branch() throws Exception {
    this.request.setBranch(ContentConstants.BRANCH_DRAFT);
    this.request.setEndpointPath("/_/attachment/inline/123456:98765/logo.png");
    final PortalResponse res = (PortalResponse) this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertEquals("private, max-age=31536000, immutable", res.getHeaders().get("Cache-Control"));
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 69 with PortalResponse

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

the class IdentityHandlerTest method testOptions.

@Test
public void testOptions() throws Exception {
    final IdProviderControllerService idProviderControllerService = Mockito.mock(IdProviderControllerService.class);
    final PortalResponse response = PortalResponse.create().status(HttpStatus.METHOD_NOT_ALLOWED).build();
    Mockito.when(idProviderControllerService.execute(Mockito.any())).thenReturn(response);
    this.handler.setIdProviderControllerService(idProviderControllerService);
    this.request.setMethod(HttpMethod.OPTIONS);
    final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.OK, res.getStatus());
    assertEquals("GET,POST,HEAD,OPTIONS,PUT,DELETE,TRACE", res.getHeaders().get("Allow"));
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebResponse(com.enonic.xp.web.WebResponse) IdProviderControllerService(com.enonic.xp.portal.idprovider.IdProviderControllerService) BaseHandlerTest(com.enonic.xp.web.handler.BaseHandlerTest) Test(org.junit.jupiter.api.Test)

Example 70 with PortalResponse

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

the class ComponentHandlerTest method testComponentFound.

@Test
public void testComponentFound() throws Exception {
    setupSite();
    setupContent();
    setupTemplates();
    final PortalResponse portalResponse = PortalResponse.create().body("component rendered").header("some-header", "some-value").status(HttpStatus.OK).build();
    Mockito.when(this.postProcessor.processResponseInstructions(Mockito.any(), Mockito.any())).thenReturn(portalResponse);
    setRendererResult(portalResponse);
    this.request.setEndpointPath("/_/component/main-region/0");
    final WebResponse res = this.handler.handle(this.request, PortalResponse.create().build(), null);
    assertNotNull(res);
    assertEquals(HttpStatus.OK, res.getStatus());
    assertEquals(MediaType.PLAIN_TEXT_UTF_8, res.getContentType());
    assertEquals("some-value", res.getHeaders().get("some-header"));
    assertEquals("component rendered", res.getBody());
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) WebResponse(com.enonic.xp.web.WebResponse) 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