2727import java .io .IOException ;
2828import java .io .InputStream ;
2929import java .net .MalformedURLException ;
30- import java .net .URI ;
31- import java .net .URISyntaxException ;
3230import java .net .URL ;
3331import java .util .HashMap ;
3432import java .util .Map ;
3533
36- @ SuppressWarnings ( "unused" )
34+ import javax .servlet .ServletException ;
35+ import javax .servlet .http .HttpServlet ;
36+ import javax .servlet .http .HttpServletRequest ;
37+ import javax .servlet .http .HttpServletResponse ;
38+
39+ import org .apache .commons .io .IOUtils ;
40+ import org .commonjava .test .http .common .CommonMethod ;
41+ import org .slf4j .Logger ;
42+ import org .slf4j .LoggerFactory ;
43+
3744public final class ExpectationServlet
3845 extends HttpServlet
3946{
@@ -105,7 +112,7 @@ private String getPath( final String testUrl )
105112 }
106113 catch ( final MalformedURLException e )
107114 {
108- e . printStackTrace ();
115+ //Do nothing
109116 }
110117 return testUrl ;
111118 }
@@ -142,12 +149,30 @@ protected void service( final HttpServletRequest req, final HttpServletResponse
142149 throws ServletException , IOException
143150 {
144151 String wholePath = getWholePath ( req );
145- final String key = getAccessKey ( req .getMethod (), wholePath );
152+ String key = getAccessKey ( req .getMethod (), wholePath );
153+ accessesByPath .merge (key , 1 , Integer ::sum );
146154
147- logger .info ( "Looking up expectation for: {}" , key );
155+ boolean handled = handle ( key , req , resp );
156+ if (!handled )
157+ {
158+ // try simple path
159+ String simplePath = getSimplePath (req );
160+ if (!simplePath .equals (wholePath ))
161+ {
162+ key = getAccessKey (req .getMethod (), simplePath );
163+ handled = handle (key , req , resp );
164+ }
165+ }
148166
149- accessesByPath .merge ( key , 1 , Integer ::sum );
167+ if (!handled )
168+ {
169+ resp .setStatus ( 404 );
170+ }
171+ }
150172
173+ private boolean handle (String key , HttpServletRequest req , HttpServletResponse resp )
174+ throws IOException , ServletException
175+ {
151176 logger .info ( "Looking for error: '{}' in:\n {}" , key , errors );
152177 if ( errors .containsKey ( key ) )
153178 {
@@ -157,7 +182,6 @@ protected void service( final HttpServletRequest req, final HttpServletResponse
157182 if ( error .handler () != null )
158183 {
159184 error .handler ().handle ( req , resp );
160- return ;
161185 }
162186 else if ( error .body () != null )
163187 {
@@ -172,21 +196,19 @@ else if ( error.bodyStream() != null )
172196 {
173197 resp .sendError ( error .code () );
174198 }
175-
176- return ;
199+ return true ;
177200 }
178201
179202 logger .info ( "Looking for expectation: '{}'" , key );
180- final ContentResponse expectation = expectations .get ( key );
181- if ( expectation != null )
203+ if ( expectations .containsKey ( key ) )
182204 {
205+ final ContentResponse expectation = expectations .get ( key );
183206 logger .info ( "Responding via registered expectation: {}" , expectation );
184207
185208 if ( expectation .handler () != null )
186209 {
187210 expectation .handler ().handle ( req , resp );
188211 logger .info ( "Using handler..." );
189- return ;
190212 }
191213 else if ( expectation .body () != null )
192214 {
@@ -207,13 +229,15 @@ else if ( expectation.bodyStream() != null )
207229 resp .setStatus ( expectation .code () );
208230 logger .info ( "Set status: {} with no body" , expectation .code () );
209231 }
210-
211- return ;
232+ return true ;
212233 }
213234
214- resp . setStatus ( 404 ) ;
235+ return false ;
215236 }
216237
238+ /**
239+ * Get path with query string.
240+ */
217241 private String getWholePath ( HttpServletRequest request )
218242 {
219243 String requestURI = request .getRequestURI ();
@@ -229,6 +253,11 @@ private String getWholePath( HttpServletRequest request )
229253 }
230254 }
231255
256+ private String getSimplePath ( HttpServletRequest request )
257+ {
258+ return request .getRequestURI ();
259+ }
260+
232261 public String getAccessKey ( final CommonMethod method , final String path )
233262 {
234263 return getAccessKey ( method .name (), path );
0 commit comments