Skip to content
Merged
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 @@ -18,8 +18,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
Expand All @@ -30,7 +28,6 @@
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpStatus;
import org.commonjava.test.http.common.CommonMethod;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -106,7 +103,7 @@ private String getPath( final String testUrl )
}
catch ( final MalformedURLException e )
{
e.printStackTrace();
//Do nothing
}
return testUrl;
}
Expand Down Expand Up @@ -145,22 +142,31 @@ public void expect( String method, String testUrl, ExpectationHandler handler )
protected void service( final HttpServletRequest req, final HttpServletResponse resp )
throws ServletException, IOException
{

String wholePath = getWholePath( req );
final String key = getAccessKey( req.getMethod(), wholePath );

logger.info( "Looking up expectation for: {}", key );
String key = getAccessKey( req.getMethod(), wholePath );
accessesByPath.merge(key, 1, Integer::sum);

final Integer i = accessesByPath.get( key );
if ( i == null )
boolean handled = handle( key, req, resp );
if (!handled)
{
accessesByPath.put( key, 1 );
// try simple path
String simplePath = getSimplePath(req);
if (!simplePath.equals(wholePath))
{
key = getAccessKey(req.getMethod(), simplePath);
handled = handle(key, req, resp);
}
}
else

if (!handled)
{
accessesByPath.put( key, i + 1 );
resp.setStatus( 404 );
}
}

private boolean handle(String key, HttpServletRequest req, HttpServletResponse resp)
throws IOException, ServletException
{
logger.info( "Looking for error: '{}' in:\n{}", key, errors );
if ( errors.containsKey( key ) )
{
Expand All @@ -170,7 +176,6 @@ protected void service( final HttpServletRequest req, final HttpServletResponse
if ( error.handler() != null )
{
error.handler().handle( req, resp );
return;
}
else if ( error.body() != null )
{
Expand All @@ -185,29 +190,26 @@ else if ( error.bodyStream() != null )
{
resp.sendError( error.code() );
}

return;
return true;
}

logger.info( "Looking for expectation: '{}'", key );
final ContentResponse expectation = expectations.get( key );
if ( expectation != null )
if ( expectations.containsKey( key ) )
{
final ContentResponse expectation = expectations.get( key );
logger.info( "Responding via registered expectation: {}", expectation );

if ( expectation.handler() != null )
{
expectation.handler().handle( req, resp );
logger.info( "Using handler..." );
return;
}
else if ( expectation.body() != null )
{
resp.setStatus( expectation.code() );

logger.info( "Set status: {} with body string", expectation.code() );
resp.getWriter()
.write( expectation.body() );
resp.getWriter().write( expectation.body() );
}
else if ( expectation.bodyStream() != null )
{
Expand All @@ -221,13 +223,15 @@ else if ( expectation.bodyStream() != null )
resp.setStatus( expectation.code() );
logger.info( "Set status: {} with no body", expectation.code() );
}

return;
return true;
}

resp.setStatus( 404 );
return false;
}

/**
* Get path with query string.
*/
private String getWholePath( HttpServletRequest request )
{
String requestURI = request.getRequestURI();
Expand All @@ -243,6 +247,11 @@ private String getWholePath( HttpServletRequest request )
}
}

private String getSimplePath( HttpServletRequest request )
{
return request.getRequestURI();
}

public String getAccessKey( final CommonMethod method, final String path )
{
return getAccessKey( method.name(), path );
Expand Down