Skip to content

Commit 808e1e3

Browse files
authored
Handle expectation with and without query string (#40)
1 parent 2375e09 commit 808e1e3

File tree

1 file changed

+33
-24
lines changed

1 file changed

+33
-24
lines changed

src/main/java/org/commonjava/test/http/expect/ExpectationServlet.java

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import java.io.IOException;
1919
import java.io.InputStream;
2020
import java.net.MalformedURLException;
21-
import java.net.URI;
22-
import java.net.URISyntaxException;
2321
import java.net.URL;
2422
import java.util.HashMap;
2523
import java.util.Map;
@@ -30,7 +28,6 @@
3028
import javax.servlet.http.HttpServletResponse;
3129

3230
import org.apache.commons.io.IOUtils;
33-
import org.apache.http.HttpStatus;
3431
import org.commonjava.test.http.common.CommonMethod;
3532
import org.slf4j.Logger;
3633
import org.slf4j.LoggerFactory;
@@ -106,7 +103,7 @@ private String getPath( final String testUrl )
106103
}
107104
catch ( final MalformedURLException e )
108105
{
109-
e.printStackTrace();
106+
//Do nothing
110107
}
111108
return testUrl;
112109
}
@@ -145,22 +142,31 @@ public void expect( String method, String testUrl, ExpectationHandler handler )
145142
protected void service( final HttpServletRequest req, final HttpServletResponse resp )
146143
throws ServletException, IOException
147144
{
148-
149145
String wholePath = getWholePath( req );
150-
final String key = getAccessKey( req.getMethod(), wholePath );
151-
152-
logger.info( "Looking up expectation for: {}", key );
146+
String key = getAccessKey( req.getMethod(), wholePath );
147+
accessesByPath.merge(key, 1, Integer::sum);
153148

154-
final Integer i = accessesByPath.get( key );
155-
if ( i == null )
149+
boolean handled = handle( key, req, resp );
150+
if (!handled)
156151
{
157-
accessesByPath.put( key, 1 );
152+
// try simple path
153+
String simplePath = getSimplePath(req);
154+
if (!simplePath.equals(wholePath))
155+
{
156+
key = getAccessKey(req.getMethod(), simplePath);
157+
handled = handle(key, req, resp);
158+
}
158159
}
159-
else
160+
161+
if (!handled)
160162
{
161-
accessesByPath.put( key, i + 1 );
163+
resp.setStatus( 404 );
162164
}
165+
}
163166

167+
private boolean handle(String key, HttpServletRequest req, HttpServletResponse resp)
168+
throws IOException, ServletException
169+
{
164170
logger.info( "Looking for error: '{}' in:\n{}", key, errors );
165171
if ( errors.containsKey( key ) )
166172
{
@@ -170,7 +176,6 @@ protected void service( final HttpServletRequest req, final HttpServletResponse
170176
if ( error.handler() != null )
171177
{
172178
error.handler().handle( req, resp );
173-
return;
174179
}
175180
else if ( error.body() != null )
176181
{
@@ -185,29 +190,26 @@ else if ( error.bodyStream() != null )
185190
{
186191
resp.sendError( error.code() );
187192
}
188-
189-
return;
193+
return true;
190194
}
191195

192196
logger.info( "Looking for expectation: '{}'", key );
193-
final ContentResponse expectation = expectations.get( key );
194-
if ( expectation != null )
197+
if ( expectations.containsKey( key ) )
195198
{
199+
final ContentResponse expectation = expectations.get( key );
196200
logger.info( "Responding via registered expectation: {}", expectation );
197201

198202
if ( expectation.handler() != null )
199203
{
200204
expectation.handler().handle( req, resp );
201205
logger.info( "Using handler..." );
202-
return;
203206
}
204207
else if ( expectation.body() != null )
205208
{
206209
resp.setStatus( expectation.code() );
207210

208211
logger.info( "Set status: {} with body string", expectation.code() );
209-
resp.getWriter()
210-
.write( expectation.body() );
212+
resp.getWriter().write( expectation.body() );
211213
}
212214
else if ( expectation.bodyStream() != null )
213215
{
@@ -221,13 +223,15 @@ else if ( expectation.bodyStream() != null )
221223
resp.setStatus( expectation.code() );
222224
logger.info( "Set status: {} with no body", expectation.code() );
223225
}
224-
225-
return;
226+
return true;
226227
}
227228

228-
resp.setStatus( 404 );
229+
return false;
229230
}
230231

232+
/**
233+
* Get path with query string.
234+
*/
231235
private String getWholePath( HttpServletRequest request )
232236
{
233237
String requestURI = request.getRequestURI();
@@ -243,6 +247,11 @@ private String getWholePath( HttpServletRequest request )
243247
}
244248
}
245249

250+
private String getSimplePath( HttpServletRequest request )
251+
{
252+
return request.getRequestURI();
253+
}
254+
246255
public String getAccessKey( final CommonMethod method, final String path )
247256
{
248257
return getAccessKey( method.name(), path );

0 commit comments

Comments
 (0)