Skip to content

Prohibit empty keys in routing context #357

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ public Map<String,String> asMap()
return context;
}

@Override
public String toString()
{
return "RoutingContext" + context;
}

private static Map<String,String> parseParameters( URI uri )
{
String query = uri.getQuery();
Expand All @@ -71,8 +77,12 @@ private static Map<String,String> parseParameters( URI uri )
"Invalid parameters: '" + pair + "' in URI '" + uri + "'" );
}

String key = keyValue[0];
String value = keyValue[1];
String key = trimAndVerify( keyValue[0], "key", uri );
String value = trimAndVerify( keyValue[1], "value", uri );
if ( value.isEmpty() )
{
throw new IllegalArgumentException( "Illegal empty value in URI query '" + uri + "'" );
}
String previousValue = parameters.put( key, value );

if ( previousValue != null )
Expand All @@ -83,4 +93,14 @@ private static Map<String,String> parseParameters( URI uri )
}
return parameters;
}

private static String trimAndVerify( String string, String name, URI uri )
{
String result = string.trim();
if ( result.isEmpty() )
{
throw new IllegalArgumentException( "Illegal empty " + name + " in URI query '" + uri + "'" );
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,14 @@ public void emptyContextInEmptyMap()
@Test
public void uriWithoutQueryIsParsedToEmptyContext()
{
URI uri = URI.create( "bolt+routing://localhost:7687/" );
RoutingContext context = new RoutingContext( uri );
testEmptyRoutingContext( URI.create( "bolt+routing://localhost:7687/" ) );
}

assertFalse( context.isDefined() );
assertTrue( context.asMap().isEmpty() );
@Test
public void uriWithEmptyQueryIsParsedToEmptyContext()
{
testEmptyRoutingContext( URI.create( "bolt+routing://localhost:7687?" ) );
testEmptyRoutingContext( URI.create( "bolt+routing://localhost:7687/?" ) );
}

@Test
Expand All @@ -73,33 +76,25 @@ public void uriWithQueryIsParsed()
@Test
public void throwsForInvalidUriQuery()
{
URI uri = URI.create( "bolt+routing://localhost:7687/?justKey" );
testIllegalUri( URI.create( "bolt+routing://localhost:7687/?justKey" ) );
}

try
{
new RoutingContext( uri );
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( IllegalArgumentException.class ) );
}
@Test
public void throwsForInvalidUriQueryKey()
{
testIllegalUri( URI.create( "bolt+routing://localhost:7687/?=value1&key2=value2" ) );
}

@Test
public void throwsForDuplicatedUriQueryParameters()
public void throwsForInvalidUriQueryValue()
{
URI uri = URI.create( "bolt+routing://localhost:7687/?key1=value1&key2=value2&key1=value2" );
testIllegalUri( URI.create( "bolt+routing://localhost:7687/key1?=value1&key2=" ) );
}

try
{
new RoutingContext( uri );
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( IllegalArgumentException.class ) );
}
@Test
public void throwsForDuplicatedUriQueryParameters()
{
testIllegalUri( URI.create( "bolt+routing://localhost:7687/?key1=value1&key2=value2&key1=value2" ) );
}

@Test
Expand All @@ -122,4 +117,25 @@ public void mapRepresentationIsUnmodifiable()

assertEquals( singletonMap( "key1", "value1" ), context.asMap() );
}

private static void testIllegalUri( URI uri )
{
try
{
new RoutingContext( uri );
fail( "Exception expected" );
}
catch ( Exception e )
{
assertThat( e, instanceOf( IllegalArgumentException.class ) );
}
}

private static void testEmptyRoutingContext( URI uri )
{
RoutingContext context = new RoutingContext( uri );

assertFalse( context.isDefined() );
assertTrue( context.asMap().isEmpty() );
}
}