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{
@@ -97,20 +104,17 @@ public String getAccessKey( final String method, final String path )
97104 return method .toUpperCase () + " " + path ;
98105 }
99106
100- private String getPath ( final String path )
107+ private String getPath ( final String testUrl )
101108 {
102- String realPath = path ;
103109 try
104110 {
105- final URL u = new URL ( path );
106- realPath = u .getPath ();
111+ return new URL ( testUrl ).getFile (); // path plus query parameters
107112 }
108113 catch ( final MalformedURLException e )
109114 {
110115 //Do nothing
111116 }
112-
113- return realPath ;
117+ return testUrl ;
114118 }
115119
116120 public void expect ( final String method , final String testUrl , final int responseCode , final String body )
@@ -144,28 +148,31 @@ public void expect( String method, String testUrl, ExpectationHandler handler )
144148 protected void service ( final HttpServletRequest req , final HttpServletResponse resp )
145149 throws ServletException , IOException
146150 {
147- String wholePath ;
148- try
149- {
150- wholePath = new URI ( req . getRequestURI () ). getPath ();
151- }
152- catch ( final URISyntaxException e )
151+ String wholePath = getWholePath ( req ) ;
152+ String key = getAccessKey ( req . getMethod (), wholePath );
153+ accessesByPath . merge ( key , 1 , Integer :: sum );
154+
155+ boolean handled = handle ( key , req , resp );
156+ if (! handled )
153157 {
154- throw new ServletException ( "Cannot parse request URI" , e );
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+ }
155165 }
156166
157- String path = wholePath ;
158- if ( path .length () > 1 )
167+ if (!handled )
159168 {
160- path = path . substring ( 1 );
169+ resp . setStatus ( 404 );
161170 }
171+ }
162172
163- final String key = getAccessKey ( req .getMethod (), wholePath );
164-
165- logger .info ( "Looking up expectation for: {}" , key );
166-
167- accessesByPath .merge ( key , 1 , Integer ::sum );
168-
173+ private boolean handle (String key , HttpServletRequest req , HttpServletResponse resp )
174+ throws IOException , ServletException
175+ {
169176 logger .info ( "Looking for error: '{}' in:\n {}" , key , errors );
170177 if ( errors .containsKey ( key ) )
171178 {
@@ -175,7 +182,6 @@ protected void service( final HttpServletRequest req, final HttpServletResponse
175182 if ( error .handler () != null )
176183 {
177184 error .handler ().handle ( req , resp );
178- return ;
179185 }
180186 else if ( error .body () != null )
181187 {
@@ -190,21 +196,19 @@ else if ( error.bodyStream() != null )
190196 {
191197 resp .sendError ( error .code () );
192198 }
193-
194- return ;
199+ return true ;
195200 }
196201
197202 logger .info ( "Looking for expectation: '{}'" , key );
198- final ContentResponse expectation = expectations .get ( key );
199- if ( expectation != null )
203+ if ( expectations .containsKey ( key ) )
200204 {
205+ final ContentResponse expectation = expectations .get ( key );
201206 logger .info ( "Responding via registered expectation: {}" , expectation );
202207
203208 if ( expectation .handler () != null )
204209 {
205210 expectation .handler ().handle ( req , resp );
206211 logger .info ( "Using handler..." );
207- return ;
208212 }
209213 else if ( expectation .body () != null )
210214 {
@@ -225,11 +229,33 @@ else if ( expectation.bodyStream() != null )
225229 resp .setStatus ( expectation .code () );
226230 logger .info ( "Set status: {} with no body" , expectation .code () );
227231 }
232+ return true ;
233+ }
234+
235+ return false ;
236+ }
237+
238+ /**
239+ * Get path with query string.
240+ */
241+ private String getWholePath ( HttpServletRequest request )
242+ {
243+ String requestURI = request .getRequestURI ();
244+ String queryString = request .getQueryString ();
228245
229- return ;
246+ if ( queryString == null )
247+ {
248+ return requestURI ;
230249 }
250+ else
251+ {
252+ return requestURI + "?" + queryString ;
253+ }
254+ }
231255
232- resp .setStatus ( 404 );
256+ private String getSimplePath ( HttpServletRequest request )
257+ {
258+ return request .getRequestURI ();
233259 }
234260
235261 public String getAccessKey ( final CommonMethod method , final String path )
0 commit comments