Search in sources :

Example 21 with PortalResponse

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

the class LiveEditAttributeInjectionTest method injectEmptyHtml.

@Test
public void injectEmptyHtml() throws Exception {
    final String html = "";
    final LiveEditAttributeInjection liveEditAttributeInjection = new LiveEditAttributeInjection();
    final PortalResponse.Builder responseBuilder = PortalResponse.create().body(html);
    final PortalResponse portalResponse = liveEditAttributeInjection.injectLiveEditAttribute(responseBuilder.build(), TextComponentType.INSTANCE);
    final String outputHtml = portalResponse.getBody().toString();
    final String expectedResult = "";
    assertEquals(expectedResult, outputHtml);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Test(org.junit.jupiter.api.Test)

Example 22 with PortalResponse

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

the class PostProcessEvaluatorTest method testEvaluateDuplicatedContributions.

@Test
public void testEvaluateDuplicatedContributions() throws Exception {
    final PostProcessInjection contributionsInjection = (portalRequest, portalResponse, tag) -> {
        switch(tag) {
            case HEAD_BEGIN:
                return Arrays.asList("<!-- HEAD BEGIN-->", "<!-- HEAD BEGIN DUPLICATED-1 -->", "<!-- HEAD BEGIN DUPLICATED-2 -->");
            case HEAD_END:
                return Arrays.asList("<!-- HEAD END -->");
            case BODY_BEGIN:
                return Arrays.asList("<!-- BODY BEGIN -->");
            case BODY_END:
                return Arrays.asList("<!-- BODY END -->");
            default:
                return null;
        }
    };
    final PostProcessInjection contributionsInjection2 = (portalRequest, portalResponse, tag) -> {
        switch(tag) {
            case HEAD_BEGIN:
                return Arrays.asList("<!-- HEAD BEGIN DUPLICATED-2 -->", "<!-- HEAD BEGIN DUPLICATED-1 -->");
            case BODY_END:
                return Arrays.asList("<!-- BODY END -->");
            default:
                return null;
        }
    };
    final PostProcessEvaluator evaluator = new PostProcessEvaluator();
    evaluator.input = readResource("postProcessEvalSource5.html");
    evaluator.injections = List.of(contributionsInjection, contributionsInjection2);
    evaluator.instructions = Collections.emptyList();
    evaluator.portalResponse = PortalResponse.create().build();
    final PortalResponse result = evaluator.evaluate();
    assertEqualsTrimmed(readResource("postProcessEvalResult5.html"), result.getAsString());
}
Also used : Arrays(java.util.Arrays) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) HtmlTag(com.enonic.xp.portal.postprocess.HtmlTag) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Cookie(javax.servlet.http.Cookie) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) Collections(java.util.Collections) PostProcessInjection(com.enonic.xp.portal.postprocess.PostProcessInjection) InputStream(java.io.InputStream) PortalResponse(com.enonic.xp.portal.PortalResponse) PostProcessInjection(com.enonic.xp.portal.postprocess.PostProcessInjection) Test(org.junit.jupiter.api.Test)

Example 23 with PortalResponse

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

the class PostProcessEvaluatorTest method testEvaluateInstructionSetHeader.

@Test
public void testEvaluateInstructionSetHeader() throws Exception {
    final PostProcessInstruction setCookieInstruction = (portalRequest, instruction) -> {
        if (instruction.startsWith("INSTRUCTION")) {
            return PortalResponse.create().header("header-name", "header-value").build();
        }
        return null;
    };
    final PostProcessEvaluator evaluator = new PostProcessEvaluator();
    evaluator.input = readResource("postProcessEvalSource6.html");
    evaluator.injections = Collections.emptyList();
    evaluator.instructions = List.of(setCookieInstruction);
    evaluator.portalResponse = PortalResponse.create().build();
    final PortalResponse result = evaluator.evaluate();
    assertEquals(1, result.getHeaders().size());
    assertEquals("header-value", result.getHeaders().get("header-name"));
}
Also used : Arrays(java.util.Arrays) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) HtmlTag(com.enonic.xp.portal.postprocess.HtmlTag) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Cookie(javax.servlet.http.Cookie) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) Collections(java.util.Collections) PostProcessInjection(com.enonic.xp.portal.postprocess.PostProcessInjection) InputStream(java.io.InputStream) PortalResponse(com.enonic.xp.portal.PortalResponse) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) Test(org.junit.jupiter.api.Test)

Example 24 with PortalResponse

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

the class PostProcessEvaluatorTest method testEvaluateMultipleContributions.

@Test
public void testEvaluateMultipleContributions() throws Exception {
    final PostProcessInjection contributionsInjection = (portalRequest, portalResponse, tag) -> {
        switch(tag) {
            case HEAD_BEGIN:
                return Arrays.asList("<!-- HEAD BEGIN -->");
            case HEAD_END:
                return Arrays.asList("<!-- HEAD END -->");
            case BODY_BEGIN:
                return Arrays.asList("<!-- BODY BEGIN -->");
            case BODY_END:
                return Arrays.asList("<!-- BODY END -->");
            default:
                return null;
        }
    };
    final PostProcessInjection contributionsInjection2 = (portalRequest, portalResponse, tag) -> {
        switch(tag) {
            case HEAD_BEGIN:
                return Arrays.asList("<!-- HEAD BEGIN EXTRA-->");
            case BODY_END:
                return Arrays.asList("<!-- BODY END EXTRA-->");
            default:
                return null;
        }
    };
    final PostProcessEvaluator evaluator = new PostProcessEvaluator();
    evaluator.input = readResource("postProcessEvalSource4.html");
    evaluator.injections = List.of(contributionsInjection, contributionsInjection2);
    evaluator.instructions = Collections.emptyList();
    evaluator.portalResponse = PortalResponse.create().build();
    final PortalResponse result = evaluator.evaluate();
    assertEqualsTrimmed(readResource("postProcessEvalResult4.html"), result.getAsString());
}
Also used : Arrays(java.util.Arrays) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) HtmlTag(com.enonic.xp.portal.postprocess.HtmlTag) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Cookie(javax.servlet.http.Cookie) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) Collections(java.util.Collections) PostProcessInjection(com.enonic.xp.portal.postprocess.PostProcessInjection) InputStream(java.io.InputStream) PortalResponse(com.enonic.xp.portal.PortalResponse) PostProcessInjection(com.enonic.xp.portal.postprocess.PostProcessInjection) Test(org.junit.jupiter.api.Test)

Example 25 with PortalResponse

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

the class PostProcessEvaluatorTest method testEvaluateContributions.

@Test
public void testEvaluateContributions() throws Exception {
    final PostProcessInjection contributionsInjection = (portalRequest, portalResponse, tag) -> {
        switch(tag) {
            case HEAD_BEGIN:
                return Arrays.asList("<!-- HEAD BEGIN -->");
            case HEAD_END:
                return Arrays.asList("<!-- HEAD END -->");
            case BODY_BEGIN:
                return Arrays.asList("<!-- BODY BEGIN -->");
            case BODY_END:
                return Arrays.asList("<!-- BODY END -->");
            default:
                return null;
        }
    };
    final PostProcessEvaluator evaluator = new PostProcessEvaluator();
    evaluator.input = readResource("postProcessEvalSource2.html");
    evaluator.injections = List.of(contributionsInjection);
    evaluator.instructions = Collections.emptyList();
    evaluator.portalResponse = PortalResponse.create().build();
    final PortalResponse result = evaluator.evaluate();
    assertEqualsTrimmed(readResource("postProcessEvalResult2.html"), result.getAsString());
}
Also used : Arrays(java.util.Arrays) StandardCharsets(java.nio.charset.StandardCharsets) Collectors.joining(java.util.stream.Collectors.joining) Test(org.junit.jupiter.api.Test) List(java.util.List) Stream(java.util.stream.Stream) HtmlTag(com.enonic.xp.portal.postprocess.HtmlTag) PortalResponse(com.enonic.xp.portal.PortalResponse) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Cookie(javax.servlet.http.Cookie) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) Collections(java.util.Collections) PostProcessInjection(com.enonic.xp.portal.postprocess.PostProcessInjection) InputStream(java.io.InputStream) PortalResponse(com.enonic.xp.portal.PortalResponse) PostProcessInjection(com.enonic.xp.portal.postprocess.PostProcessInjection) 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