Search in sources :

Example 1 with PostProcessInstruction

use of com.enonic.xp.portal.postprocess.PostProcessInstruction 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 2 with PostProcessInstruction

use of com.enonic.xp.portal.postprocess.PostProcessInstruction in project xp by enonic.

the class PostProcessEvaluatorTest method testEvaluateInstructionSetContribution.

@Test
public void testEvaluateInstructionSetContribution() throws Exception {
    final PostProcessInstruction setCookieInstruction = (portalRequest, instruction) -> {
        if (instruction.startsWith("INSTRUCTION")) {
            return PortalResponse.create().contribution(HtmlTag.BODY_END, "<script src='my-script.js'/>").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.getContributions(HtmlTag.BODY_END).size());
    assertEquals("<script src='my-script.js'/>", result.getContributions(HtmlTag.BODY_END).get(0));
}
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 3 with PostProcessInstruction

use of com.enonic.xp.portal.postprocess.PostProcessInstruction in project xp by enonic.

the class PostProcessEvaluatorTest method testEvaluateInstructions.

@Test
public void testEvaluateInstructions() throws Exception {
    final PostProcessInstruction uppercaseInstruction = (portalRequest, instruction) -> {
        if (instruction.startsWith("UPPERCASE ")) {
            return PortalResponse.create().body(instruction.substring("UPPERCASE ".length()).toUpperCase()).build();
        }
        return null;
    };
    final PostProcessInstruction expandInstruction = (portalRequest, instruction) -> {
        if (instruction.startsWith("EXPAND ")) {
            return PortalResponse.create().body("<!--#UPPERCASE " + instruction.substring("EXPAND ".length()) + "-->").build();
        }
        return null;
    };
    final PostProcessEvaluator evaluator = new PostProcessEvaluator();
    evaluator.input = readResource("postProcessEvalSource3.html");
    evaluator.injections = Collections.emptyList();
    evaluator.instructions = List.of(uppercaseInstruction, expandInstruction);
    evaluator.portalResponse = PortalResponse.create().build();
    final PortalResponse result = evaluator.evaluate();
    assertEqualsTrimmed(readResource("postProcessEvalResult3.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) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) Test(org.junit.jupiter.api.Test)

Example 4 with PostProcessInstruction

use of com.enonic.xp.portal.postprocess.PostProcessInstruction in project xp by enonic.

the class PostProcessEvaluatorTest method testEvaluateInstructionSetApplyFilters.

@Test
public void testEvaluateInstructionSetApplyFilters() throws Exception {
    final PostProcessInstruction setCookieInstruction = (portalRequest, instruction) -> {
        if (instruction.startsWith("INSTRUCTION")) {
            return PortalResponse.create().applyFilters(false).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(false, result.applyFilters());
}
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 5 with PostProcessInstruction

use of com.enonic.xp.portal.postprocess.PostProcessInstruction in project xp by enonic.

the class PostProcessEvaluatorTest method testEvaluateInstructionSetCookie.

@Test
public void testEvaluateInstructionSetCookie() throws Exception {
    final PostProcessInstruction setCookieInstruction = (portalRequest, instruction) -> {
        if (instruction.startsWith("INSTRUCTION")) {
            return PortalResponse.create().cookie(new Cookie("cookie-name", "cookie-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.getCookies().size());
    assertEquals("cookie-name", result.getCookies().get(0).getName());
    assertEquals("cookie-value", result.getCookies().get(0).getValue());
}
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) Cookie(javax.servlet.http.Cookie) PortalResponse(com.enonic.xp.portal.PortalResponse) PostProcessInstruction(com.enonic.xp.portal.postprocess.PostProcessInstruction) Test(org.junit.jupiter.api.Test)

Aggregations

PortalResponse (com.enonic.xp.portal.PortalResponse)6 PostProcessInstruction (com.enonic.xp.portal.postprocess.PostProcessInstruction)6 HtmlTag (com.enonic.xp.portal.postprocess.HtmlTag)5 PostProcessInjection (com.enonic.xp.portal.postprocess.PostProcessInjection)5 InputStream (java.io.InputStream)5 StandardCharsets (java.nio.charset.StandardCharsets)5 Arrays (java.util.Arrays)5 Collections (java.util.Collections)5 List (java.util.List)5 Collectors.joining (java.util.stream.Collectors.joining)5 Stream (java.util.stream.Stream)5 Cookie (javax.servlet.http.Cookie)5 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)5 Test (org.junit.jupiter.api.Test)5 HtmlBlockParser (com.enonic.xp.portal.impl.parser.HtmlBlockParser)1