Search in sources :

Example 61 with PortalRequest

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

the class ExceptionRendererImplTest method setup.

@BeforeEach
void setup() {
    this.resourceService = mock(ResourceService.class);
    this.contentService = mock(ContentService.class);
    this.errorHandlerScriptFactory = mock(ErrorHandlerScriptFactory.class);
    this.postProcessor = new MockPostProcessor();
    this.renderer = new ExceptionRendererImpl(resourceService, errorHandlerScriptFactory, contentService, null, postProcessor, RunMode.DEV);
    this.request = new PortalRequest();
    final HttpServletRequest rawRequest = mock(HttpServletRequest.class);
    this.request.setRawRequest(rawRequest);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ResourceService(com.enonic.xp.resource.ResourceService) ContentService(com.enonic.xp.content.ContentService) ErrorHandlerScriptFactory(com.enonic.xp.portal.impl.error.ErrorHandlerScriptFactory) PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 62 with PortalRequest

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

the class FilterScriptImplTest method setup.

@BeforeEach
public void setup() throws Exception {
    this.portalRequest = new PortalRequest();
    this.portalResponse = PortalResponse.create().build();
    final BundleContext bundleContext = Mockito.mock(BundleContext.class);
    final Bundle bundle = Mockito.mock(Bundle.class);
    Mockito.when(bundle.getBundleContext()).thenReturn(bundleContext);
    final Application application = Mockito.mock(Application.class);
    Mockito.when(application.getBundle()).thenReturn(bundle);
    Mockito.when(application.getClassLoader()).thenReturn(getClass().getClassLoader());
    Mockito.when(application.isStarted()).thenReturn(true);
    Mockito.when(application.getConfig()).thenReturn(ConfigBuilder.create().build());
    final ApplicationService applicationService = Mockito.mock(ApplicationService.class);
    Mockito.when(applicationService.getInstalledApplication(ApplicationKey.from("myapplication"))).thenReturn(application);
    this.resourceService = Mockito.mock(ResourceService.class);
    Mockito.when(resourceService.getResource(Mockito.any())).thenAnswer(invocation -> {
        final ResourceKey resourceKey = (ResourceKey) invocation.getArguments()[0];
        final URL resourceUrl = FilterScriptImplTest.class.getResource("/" + resourceKey.getApplicationKey() + resourceKey.getPath());
        return new UrlResource(resourceKey, resourceUrl);
    });
    final ScriptAsyncService scriptAsyncService = Mockito.mock(ScriptAsyncService.class);
    final ScriptRuntimeFactoryImpl runtimeFactory = new ScriptRuntimeFactoryImpl(applicationService, this.resourceService, scriptAsyncService);
    final PortalScriptServiceImpl scriptService = new PortalScriptServiceImpl(runtimeFactory);
    scriptService.initialize();
    this.factory = new FilterScriptFactoryImpl();
    this.factory.setScriptService(scriptService);
    final HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
    ServletRequestHolder.setRequest(req);
}
Also used : Bundle(org.osgi.framework.Bundle) ResourceService(com.enonic.xp.resource.ResourceService) PortalScriptServiceImpl(com.enonic.xp.portal.impl.script.PortalScriptServiceImpl) URL(java.net.URL) PortalRequest(com.enonic.xp.portal.PortalRequest) ResourceKey(com.enonic.xp.resource.ResourceKey) ScriptRuntimeFactoryImpl(com.enonic.xp.script.impl.standard.ScriptRuntimeFactoryImpl) HttpServletRequest(javax.servlet.http.HttpServletRequest) UrlResource(com.enonic.xp.resource.UrlResource) ScriptAsyncService(com.enonic.xp.script.impl.async.ScriptAsyncService) Application(com.enonic.xp.app.Application) BundleContext(org.osgi.framework.BundleContext) ApplicationService(com.enonic.xp.app.ApplicationService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 63 with PortalRequest

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

the class PortalRequestSerializerTest method serializeMultiParams.

@Test
public void serializeMultiParams() throws Exception {
    final PortalRequest sourceRequest = new PortalRequest();
    final String jsonRequest = readResource("PortalRequestSerializer_request3.json");
    final Object obj = execute("var result = " + jsonRequest + "; result;");
    final ScriptValue value = this.factory.newValue(obj);
    PortalRequestSerializer reqSerializer = new PortalRequestSerializer(sourceRequest, value);
    final PortalRequest portalRequest = reqSerializer.serialize();
    assertNotNull(portalRequest);
    assertEquals(Map.of("key1", "value1", "key2", 42), portalRequest.getBody());
    assertEquals(0, portalRequest.getHeaders().size());
    assertEquals(0, portalRequest.getCookies().size());
    assertEquals(4, portalRequest.getParams().size());
    assertEquals("param-value", portalRequest.getParams().get("param").iterator().next());
    final List<String> param2Values = new ArrayList<>(portalRequest.getParams().get("param2"));
    assertEquals("param-value2-a", param2Values.get(0));
    assertEquals("param-value2-b", param2Values.get(1));
    assertEquals("param-value2-c", param2Values.get(2));
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) ArrayList(java.util.ArrayList) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 64 with PortalRequest

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

the class PortalRequestSerializerTest method serializeComplete.

@Test
public void serializeComplete() throws Exception {
    final PortalRequest sourceRequest = new PortalRequest();
    final String jsonRequest = readResource("PortalRequestSerializer_request.json");
    final Object obj = execute("var result = " + jsonRequest + "; result;");
    final ScriptValue value = this.factory.newValue(obj);
    PortalRequestSerializer reqSerializer = new PortalRequestSerializer(sourceRequest, value);
    final PortalRequest portalRequest = reqSerializer.serialize();
    assertNotNull(portalRequest);
    assertEquals(HttpMethod.POST, portalRequest.getMethod());
    assertEquals("https", portalRequest.getScheme());
    assertEquals("myhost", portalRequest.getHost());
    assertEquals(1234, portalRequest.getPort());
    assertEquals("/some/path", portalRequest.getPath());
    assertEquals("https://myhost:1234/some/path/url", portalRequest.getUrl());
    assertEquals("127.0.0.42", portalRequest.getRemoteAddress());
    assertEquals(RenderMode.EDIT, portalRequest.getMode());
    assertEquals(true, portalRequest.isValidTicket());
    assertEquals(Branch.from("draft"), portalRequest.getBranch());
    assertEquals("application/json", portalRequest.getContentType());
    assertEquals("POST BODY", portalRequest.getBody());
    assertEquals(2, portalRequest.getHeaders().size());
    assertEquals("header-value", portalRequest.getHeaders().get("header"));
    assertEquals("header-value2", portalRequest.getHeaders().get("header2"));
    assertEquals(2, portalRequest.getCookies().size());
    assertEquals("cookie-value", portalRequest.getCookies().get("cookie"));
    assertEquals("cookie-value2", portalRequest.getCookies().get("cookie2"));
    assertEquals(2, portalRequest.getParams().size());
    assertEquals("param-value", portalRequest.getParams().get("param").iterator().next());
    assertEquals("param-value2", portalRequest.getParams().get("param2").iterator().next());
}
Also used : ScriptValue(com.enonic.xp.script.ScriptValue) PortalRequest(com.enonic.xp.portal.PortalRequest) Test(org.junit.jupiter.api.Test)

Example 65 with PortalRequest

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

the class ErrorHandlerTest method setup.

@BeforeEach
public final void setup() throws Exception {
    this.request = new PortalRequest();
    this.handler = new ErrorHandler();
    this.request.setMethod(HttpMethod.GET);
    this.request.setEndpointPath("/_/error/401");
}
Also used : PortalRequest(com.enonic.xp.portal.PortalRequest) BeforeEach(org.junit.jupiter.api.BeforeEach)

Aggregations

PortalRequest (com.enonic.xp.portal.PortalRequest)103 Test (org.junit.jupiter.api.Test)47 BeforeEach (org.junit.jupiter.api.BeforeEach)35 PortalResponse (com.enonic.xp.portal.PortalResponse)25 Site (com.enonic.xp.site.Site)19 Content (com.enonic.xp.content.Content)12 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 ApplicationKey (com.enonic.xp.app.ApplicationKey)10 ContentPath (com.enonic.xp.content.ContentPath)10 ContentService (com.enonic.xp.content.ContentService)10 ControllerScriptFactory (com.enonic.xp.portal.controller.ControllerScriptFactory)10 ControllerScript (com.enonic.xp.portal.controller.ControllerScript)9 ResourceKey (com.enonic.xp.resource.ResourceKey)9 ResourceService (com.enonic.xp.resource.ResourceService)9 ContentId (com.enonic.xp.content.ContentId)7 Matcher (java.util.regex.Matcher)7 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)7 Assertions.assertNull (org.junit.jupiter.api.Assertions.assertNull)7 Assertions.fail (org.junit.jupiter.api.Assertions.fail)7 Mockito.when (org.mockito.Mockito.when)7