Search in sources :

Example 26 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class PortalUrlServiceImpl_generateUrlTest method createUrl_withVirtualHost.

@Test
public void createUrl_withVirtualHost() {
    final GenerateUrlParams params = new GenerateUrlParams().type(UrlTypeConstants.ABSOLUTE).portalRequest(this.portalRequest).url("/admin").param("a", 3);
    // Mocks a virtual host and the HTTP request
    final VirtualHost virtualHost = Mockito.mock(VirtualHost.class);
    when(req.getAttribute(VirtualHost.class.getName())).thenReturn(virtualHost);
    when(req.getServerName()).thenReturn("localhost");
    when(req.getScheme()).thenReturn("http");
    when(req.getServerPort()).thenReturn(80);
    // Calls the method with a virtual mapping /main -> /
    Mockito.when(virtualHost.getSource()).thenReturn("/main");
    Mockito.when(virtualHost.getTarget()).thenReturn("/");
    String url = this.service.generateUrl(params);
    assertEquals("http://localhost/main/admin?a=3", url);
    // Calls the method with a virtual mapping /main -> /site/default/draft/context
    Mockito.when(virtualHost.getSource()).thenReturn("/studio");
    Mockito.when(virtualHost.getTarget()).thenReturn("/admin");
    url = this.service.generateUrl(params);
    assertEquals("http://localhost/studio?a=3", url);
    // Calls the method with a virtual mapping /main -> /site/default/draft/context
    Mockito.when(virtualHost.getSource()).thenReturn("/");
    Mockito.when(virtualHost.getTarget()).thenReturn("/admin");
    url = this.service.generateUrl(params);
    assertEquals("http://localhost/?a=3", url);
    // Post treatment
    ServletRequestHolder.setRequest(null);
}
Also used : GenerateUrlParams(com.enonic.xp.portal.url.GenerateUrlParams) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) Test(org.junit.jupiter.api.Test)

Example 27 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class VirtualHostFilterTest method testMapping_found.

@Test
public void testMapping_found() throws Exception {
    addMapping();
    when(this.virtualHostService.isEnabled()).thenReturn(true);
    when(req.getServerName()).thenReturn("enonic.com");
    when(req.getRequestURI()).thenReturn("/rest/status");
    final RequestDispatcher requestDispatcher = mock(RequestDispatcher.class);
    when(req.getRequestDispatcher("/admin/rest/status")).thenReturn(requestDispatcher);
    when(req.getAttribute(VirtualHost.class.getName())).thenReturn(virtualHosts.get(0));
    VirtualHostFilter filter = new VirtualHostFilter(virtualHostService, new VirtualHostResolverImpl(virtualHostService));
    filter.doFilter(this.req, this.res, this.chain);
    verify(this.chain, times(0)).doFilter(this.req, this.res);
    verify(requestDispatcher).forward(this.req, this.res);
    assertTrue(VirtualHostHelper.hasVirtualHost(this.req));
    final VirtualHost virtualHost = VirtualHostHelper.getVirtualHost(this.req);
    assertNotNull(virtualHost);
    assertEquals("test", virtualHost.getName());
}
Also used : VirtualHost(com.enonic.xp.web.vhost.VirtualHost) RequestDispatcher(javax.servlet.RequestDispatcher) Test(org.junit.jupiter.api.Test)

Example 28 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class VirtualHostResolverImplTest method test_matchesVHostWithLongestSourceAndEqualHosts.

@Test
public void test_matchesVHostWithLongestSourceAndEqualHosts() {
    final List<VirtualHost> virtualHosts = new ArrayList<>();
    virtualHosts.add(createVirtualHostMapping("a", "no.domain.com", "/source", "/other/a", 0));
    virtualHosts.add(createVirtualHostMapping("b", "~(?<sub>.+)\\.domain\\.com", "/source/path", "/other/b/${sub}", 0));
    when(virtualHostService.getVirtualHosts()).thenReturn(virtualHosts);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getServerName()).thenReturn("no.domain.com");
    when(req.getRequestURI()).thenReturn(URI.create("https://no.domain.com/source/path/123").getPath());
    final VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    VirtualHost mapping = virtualHostResolver.resolveVirtualHost(req);
    assertNotNull(mapping);
    assertEquals("b", mapping.getName());
    assertEquals("/other/b/no", mapping.getTarget());
    assertEquals("no.domain.com", mapping.getHost());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) VirtualHostResolver(com.enonic.xp.web.vhost.VirtualHostResolver) ArrayList(java.util.ArrayList) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) Test(org.junit.jupiter.api.Test)

Example 29 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class VirtualHostServiceImplTest method testLoadedConfig_none.

@Test
public void testLoadedConfig_none() throws Exception {
    final VirtualHostServiceImpl virtualHostService = new VirtualHostServiceImpl(loadConfig("none"));
    assertFalse(virtualHostService.isEnabled());
    assertNotNull(virtualHostService.getVirtualHosts());
    final List<VirtualHost> mappings = virtualHostService.getVirtualHosts();
    assertEquals(0, mappings.size());
}
Also used : VirtualHostServiceImpl(com.enonic.xp.web.vhost.impl.config.VirtualHostServiceImpl) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) Test(org.junit.jupiter.api.Test)

Example 30 with VirtualHost

use of com.enonic.xp.web.vhost.VirtualHost in project xp by enonic.

the class VirtualHostServiceImplTest method testNoConfig.

@Test
public void testNoConfig() {
    final VirtualHostServiceImpl virtualHostService = new VirtualHostServiceImpl(Map.of());
    assertFalse(virtualHostService.isEnabled());
    assertNotNull(virtualHostService.getVirtualHosts());
    final List<VirtualHost> mappings = virtualHostService.getVirtualHosts();
    assertEquals(0, mappings.size());
}
Also used : VirtualHostServiceImpl(com.enonic.xp.web.vhost.impl.config.VirtualHostServiceImpl) VirtualHost(com.enonic.xp.web.vhost.VirtualHost) Test(org.junit.jupiter.api.Test)

Aggregations

VirtualHost (com.enonic.xp.web.vhost.VirtualHost)32 Test (org.junit.jupiter.api.Test)27 HttpServletRequest (javax.servlet.http.HttpServletRequest)12 VirtualHostResolver (com.enonic.xp.web.vhost.VirtualHostResolver)8 ArrayList (java.util.ArrayList)8 VirtualHostServiceImpl (com.enonic.xp.web.vhost.impl.config.VirtualHostServiceImpl)6 BaseHandlerTest (com.enonic.xp.web.handler.BaseHandlerTest)3 PortalResponse (com.enonic.xp.portal.PortalResponse)2 IdProviderKey (com.enonic.xp.security.IdProviderKey)2 WebResponse (com.enonic.xp.web.WebResponse)2 HashMap (java.util.HashMap)2 RequestDispatcher (javax.servlet.RequestDispatcher)2 PortalRequest (com.enonic.xp.portal.PortalRequest)1 IdProviderControllerExecutionParams (com.enonic.xp.portal.idprovider.IdProviderControllerExecutionParams)1 ContentResolver (com.enonic.xp.portal.impl.ContentResolver)1 AbstractControllerTest (com.enonic.xp.portal.impl.controller.AbstractControllerTest)1 AssetUrlParams (com.enonic.xp.portal.url.AssetUrlParams)1 GenerateUrlParams (com.enonic.xp.portal.url.GenerateUrlParams)1 IdentityUrlParams (com.enonic.xp.portal.url.IdentityUrlParams)1 ResourceKey (com.enonic.xp.resource.ResourceKey)1