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);
}
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());
}
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());
}
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());
}
}
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);
}
Aggregations