Skip to content

Commit

Permalink
vhost root mapping inconsistency #10354 (#10358)
Browse files Browse the repository at this point in the history
(cherry picked from commit b35410c)
  • Loading branch information
rymsha committed Dec 5, 2023
1 parent 285cb7e commit face522
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,31 @@
public final class VirtualHostInternalHelper
{

private VirtualHostInternalHelper()
{
}

public static String getFullTargetPath( final VirtualHost virtualHost, final HttpServletRequest req )
{
String path = req.getRequestURI();
if ( !"/".equals( virtualHost.getSource() ) && path.startsWith( virtualHost.getSource() ) )
final String source = virtualHost.getSource();
final String target = virtualHost.getTarget();
final String requestURI = req.getRequestURI();

if ( source.equals( requestURI ) )
{
return target;
}

if ( "/".equals( source ) )
{
return "/".equals( target ) ? requestURI : target + requestURI;
}

if ( "/".equals( target ) )
{
path = path.substring( virtualHost.getSource().length() );
return requestURI.substring( source.length() );
}

return virtualHost.getTarget() + path;
return target + requestURI.substring( source.length() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

public class VirtualHostInternalHelperTest
{

@Test
public void testGetFullTargetPath()
{
Expand All @@ -25,7 +24,49 @@ public void testGetFullTargetPath()

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( fullTarget, "/b/c/other/service" );
assertEquals( "/b/c/other/service", fullTarget );
}

@Test
public void testGetFullTargetPath_root_target()
{
final VirtualHostMapping virtualHostMapping =
new VirtualHostMapping( "host", "foo.no", "/a", "/", VirtualHostIdProvidersMapping.create().build(), 0 );

final HttpServletRequest req = mock( HttpServletRequest.class );
when( req.getRequestURI() ).thenReturn( "/a/other/service" );

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( "/other/service", fullTarget );
}

@Test
public void testGetFullTargetPath_source_equals_request()
{
final VirtualHostMapping virtualHostMapping =
new VirtualHostMapping( "host", "foo.no", "/a", "/", VirtualHostIdProvidersMapping.create().build(), 0 );

final HttpServletRequest req = mock( HttpServletRequest.class );
when( req.getRequestURI() ).thenReturn( "/a" );

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( "/", fullTarget );
}

@Test
public void testGetFullTargetPath_source_equals_request_2()
{
final VirtualHostMapping virtualHostMapping =
new VirtualHostMapping( "host", "foo.no", "/a", "/foo/", VirtualHostIdProvidersMapping.create().build(), 0 );

final HttpServletRequest req = mock( HttpServletRequest.class );
when( req.getRequestURI() ).thenReturn( "/a" );

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( "/foo/", fullTarget );
}

@Test
Expand All @@ -39,6 +80,49 @@ public void testGetFullTargetPathWithTrailingSlash()

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( fullTarget, "/b/c/other/service/" );
assertEquals( "/b/c/other/service/", fullTarget );
}


@Test
void testGetFullTargetPath_root_source()
{
final VirtualHostMapping virtualHostMapping =
new VirtualHostMapping( "host", "foo.no", "/", "/b/c", VirtualHostIdProvidersMapping.create().build(), 0 );

final HttpServletRequest req = mock( HttpServletRequest.class );
when( req.getRequestURI() ).thenReturn( "/" );

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( "/b/c", fullTarget );
}

@Test
void testGetFullTargetPath_root_source_2()
{
final VirtualHostMapping virtualHostMapping =
new VirtualHostMapping( "host", "foo.no", "/", "/b/c", VirtualHostIdProvidersMapping.create().build(), 0 );

final HttpServletRequest req = mock( HttpServletRequest.class );
when( req.getRequestURI() ).thenReturn( "/d/e" );

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( "/b/c/d/e", fullTarget );
}

@Test
void testGetFullTargetPath_root_source_and_target()
{
final VirtualHostMapping virtualHostMapping =
new VirtualHostMapping( "host", "foo.no", "/", "/", VirtualHostIdProvidersMapping.create().build(), 0 );

final HttpServletRequest req = mock( HttpServletRequest.class );
when( req.getRequestURI() ).thenReturn( "/b/c" );

final String fullTarget = VirtualHostInternalHelper.getFullTargetPath( virtualHostMapping, req );

assertEquals( "/b/c", fullTarget );
}
}

0 comments on commit face522

Please sign in to comment.