Search in sources :

Example 1 with VirtualHostResolver

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

the class VirtualHostResolverImplTest method testMatches_wrongHost.

@Test
public void testMatches_wrongHost() {
    this.virtualHostMapping = new VirtualHostMapping("mymapping", "foo.no", "/", "/a", VirtualHostIdProvidersMapping.create().build(), 0);
    when(virtualHostService.getVirtualHosts()).thenReturn(List.of(this.virtualHostMapping));
    VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getServerName()).thenReturn("localhost");
    when(req.getRequestURI()).thenReturn("/a/b");
    assertNull(virtualHostResolver.resolveVirtualHost(req));
}
Also used : VirtualHostResolver(com.enonic.xp.web.vhost.VirtualHostResolver) HttpServletRequest(javax.servlet.http.HttpServletRequest) VirtualHostMapping(com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping) Test(org.junit.jupiter.api.Test)

Example 2 with VirtualHostResolver

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

the class VirtualHostResolverImplTest method testResolve.

@Test
public void testResolve() {
    final List<VirtualHost> virtualHosts = new ArrayList<>();
    virtualHosts.add(createVirtualHostMapping("a", "localhost", "/", "/other/a", 0));
    virtualHosts.add(createVirtualHostMapping("b", "enonic.com ~example\\.(?<cc>.+)", "/", "/other/d", 0));
    when(virtualHostService.getVirtualHosts()).thenReturn(virtualHosts);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getServerName()).thenReturn("eXampLe.com");
    final VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    final VirtualHost mapping = virtualHostResolver.resolveVirtualHost(req);
    assertNotNull(mapping);
    assertEquals("b", mapping.getName());
}
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 3 with VirtualHostResolver

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

the class VirtualHostResolverImplTest method testMatchesHostInLowerCase.

@Test
public void testMatchesHostInLowerCase() {
    final List<VirtualHost> virtualHosts = new ArrayList<>();
    virtualHosts.add(createVirtualHostMapping("a", "doMain.com", "/source", "/other/a", 1));
    virtualHosts.add(createVirtualHostMapping("b", "no.domain.com", "/", "/other/b", 1));
    when(virtualHostService.getVirtualHosts()).thenReturn(virtualHosts);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getRequestURI()).thenReturn(URI.create("https://domain.com/source").getPath());
    when(req.getServerName()).thenReturn("DoMaIn.com");
    final VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    VirtualHost mapping = virtualHostResolver.resolveVirtualHost(req);
    assertNotNull(mapping);
    assertEquals("a", mapping.getName());
    assertEquals("/other/a", mapping.getTarget());
}
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 4 with VirtualHostResolver

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

the class VirtualHostResolverImplTest method testResolve_multipleHosts_sortedBySource_reversed.

@Test
public void testResolve_multipleHosts_sortedBySource_reversed() {
    final List<VirtualHost> virtualHosts = new ArrayList<>();
    virtualHosts.add(createVirtualHostMapping("a", "no.domain.com", "/source", "/other/a", 1));
    virtualHosts.add(createVirtualHostMapping("b", "domain.com ~(?<sub>.+)\\.domain\\.com", "/", "/other/b/${sub}", 1));
    virtualHosts.add(createVirtualHostMapping("c", "domain.com", "/", "/other/c", 1));
    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").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("/", mapping.getSource());
}
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 5 with VirtualHostResolver

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

the class VirtualHostResolverImplTest method testResolve_notFound.

@Test
public void testResolve_notFound() {
    final List<VirtualHost> virtualHosts = new ArrayList<>();
    virtualHosts.add(createVirtualHostMapping("a", "localhost", "/", "/other/a", 0));
    virtualHosts.add(createVirtualHostMapping("b", "domain.com", "/", "/other/b", 0));
    when(virtualHostService.getVirtualHosts()).thenReturn(virtualHosts);
    HttpServletRequest req = mock(HttpServletRequest.class);
    when(req.getServerName()).thenReturn("foo.no");
    final VirtualHostResolver virtualHostResolver = new VirtualHostResolverImpl(virtualHostService);
    final VirtualHost mapping = virtualHostResolver.resolveVirtualHost(req);
    assertNull(mapping);
}
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)

Aggregations

VirtualHostResolver (com.enonic.xp.web.vhost.VirtualHostResolver)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 Test (org.junit.jupiter.api.Test)11 VirtualHost (com.enonic.xp.web.vhost.VirtualHost)8 ArrayList (java.util.ArrayList)8 VirtualHostMapping (com.enonic.xp.web.vhost.impl.mapping.VirtualHostMapping)3