Search in sources :

Example 81 with PortalResponse

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

the class LiveEditAttributeInjectionTest method injectWithLeadingWhitespace.

@Test
public void injectWithLeadingWhitespace() throws Exception {
    final String html = readResource("part7Source.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 = readResource("part7Rendered.html");
    assertEquals(expectedResult, outputHtml);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Test(org.junit.jupiter.api.Test)

Example 82 with PortalResponse

use of com.enonic.xp.portal.PortalResponse 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)

Example 83 with PortalResponse

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

the class ResponseProcessorExecutorTest method testExecuteResponseProcessorWithByteSourceBody.

@Test
public void testExecuteResponseProcessorWithByteSourceBody() throws Exception {
    final ByteSource data = ByteSource.wrap("DATA".getBytes(StandardCharsets.UTF_8));
    final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
    final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
    when(scriptExports.hasMethod("responseProcessor")).thenReturn(true);
    when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
    final ScriptValue result = Mockito.mock(ScriptValue.class);
    final ScriptValue body = Mockito.mock(ScriptValue.class);
    when(body.getValue()).thenReturn(data.toString());
    when(result.isObject()).thenReturn(true);
    when(result.getMember("body")).thenReturn(body);
    when(scriptExports.executeMethod(anyString(), any(PortalRequestMapper.class), any(PortalResponseMapper.class))).thenReturn(result);
    final ResponseProcessorExecutor filterExecutor = new ResponseProcessorExecutor(scriptService);
    final ResponseProcessorDescriptor filter = ResponseProcessorDescriptor.create().application(ApplicationKey.from("myApp")).name("filter1").build();
    final PortalRequest request = new PortalRequest();
    final PortalResponse response = PortalResponse.create().body(data).build();
    final PortalResponse filteredResponse = filterExecutor.execute(filter, request, response);
    assertNotNull(filteredResponse);
    assertTrue(filteredResponse.getBody() instanceof ByteSource);
    assertArrayEquals(data.read(), ((ByteSource) filteredResponse.getBody()).read());
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalResponse(com.enonic.xp.portal.PortalResponse) PortalResponseMapper(com.enonic.xp.portal.impl.mapper.PortalResponseMapper) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ByteSource(com.google.common.io.ByteSource) PortalRequestMapper(com.enonic.xp.portal.impl.mapper.PortalRequestMapper) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 84 with PortalResponse

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

the class ResponseProcessorExecutorTest method testExecuteResponseProcessorNotImplementingMethod.

@Test
public void testExecuteResponseProcessorNotImplementingMethod() throws Exception {
    final PortalScriptService scriptService = Mockito.mock(PortalScriptService.class);
    final ScriptExports scriptExports = Mockito.mock(ScriptExports.class);
    when(scriptService.execute(any(ResourceKey.class))).thenReturn(scriptExports);
    final ResponseProcessorExecutor filterExecutor = new ResponseProcessorExecutor(scriptService);
    final ResponseProcessorDescriptor filter = ResponseProcessorDescriptor.create().application(ApplicationKey.from("myApp")).name("filter1").build();
    final PortalRequest request = new PortalRequest();
    final PortalResponse response = PortalResponse.create().build();
    try {
        filterExecutor.execute(filter, request, response);
        fail("Expected exception");
    } catch (RenderException e) {
        assertEquals("Missing exported function [responseProcessor] in response filter [/site/processors/filter1.js]", e.getMessage());
    }
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) RenderException(com.enonic.xp.portal.impl.rendering.RenderException) PortalScriptService(com.enonic.xp.portal.script.PortalScriptService) ResponseProcessorDescriptor(com.enonic.xp.site.processor.ResponseProcessorDescriptor) ScriptExports(com.enonic.xp.script.ScriptExports) ResourceKey(com.enonic.xp.resource.ResourceKey) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 85 with PortalResponse

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

the class RendererDelegateImplTest method given_Renderable_matching_only_on_superType_when_getRenderer_then_Renderer_for_superType_is_returned.

@Test
public void given_Renderable_matching_only_on_superType_when_getRenderer_then_Renderer_for_superType_is_returned() {
    RendererDelegateImpl factory = new RendererDelegateImpl(mock(ContentService.class));
    final PortalResponse response = PortalResponse.create().build();
    factory.addRenderer(createRenderer(Content.class, response));
    // exercise
    final PortalResponse renderResponse = factory.render(createPageTemplate(), null);
    // verify
    assertSame(response, renderResponse);
}
Also used : PortalResponse(com.enonic.xp.portal.PortalResponse) Content(com.enonic.xp.content.Content) ContentService(com.enonic.xp.content.ContentService) 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