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