Skip to content

Commit d166a30

Browse files
authored
Merge pull request #39 from ruhan1/master-queryparam
Take query params into account in expectation
2 parents f89cc81 + 13ec891 commit d166a30

File tree

2 files changed

+101
-65
lines changed

2 files changed

+101
-65
lines changed

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

Lines changed: 59 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,20 @@
2727
import java.io.IOException;
2828
import java.io.InputStream;
2929
import java.net.MalformedURLException;
30-
import java.net.URI;
31-
import java.net.URISyntaxException;
3230
import java.net.URL;
3331
import java.util.HashMap;
3432
import 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+
3744
public 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 )

junit4/src/test/java/org/commonjava/test/http/junt4/TestHttpServerTest.java

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.junit.Rule;
2828
import org.junit.Test;
2929

30+
import java.io.IOException;
3031
import java.io.InputStream;
3132

3233
import static org.hamcrest.CoreMatchers.equalTo;
@@ -50,45 +51,54 @@ public void simpleDownload()
5051
final String path = server.formatPath( subPath );
5152
server.expect( url, 200, content );
5253

54+
String result = getHttpContent( url );
55+
assertThat(result, equalTo(content));
56+
57+
final String key = server.getAccessKey( CommonMethod.GET.name(), path );
58+
System.out.println( "Getting accesses for: '" + key + "'" );
59+
assertThat( server.getAccessesByPathKey().get( key ), equalTo( 1 ) );
60+
}
61+
62+
@Test
63+
public void downloadWithQueryParams()
64+
throws Exception
65+
{
66+
final ExpectationServer server = serverRule.getServer();
67+
68+
final String subPath = "/path/to/something";
69+
final String path1 = subPath + "?version=1.0";
70+
final String path2 = subPath + "?version=2.0";
71+
72+
final String url1 = server.formatUrl(path1);
73+
final String url2 = server.formatUrl(path2);
74+
75+
final String content1 = "this is a test version 1";
76+
final String content2 = "this is a test version 2";
77+
78+
server.expect(url1, 200, content1);
79+
server.expect(url2, 200, content2);
80+
81+
String result = getHttpContent(url1);
82+
assertThat(result, equalTo(content1));
83+
84+
result = getHttpContent(url2);
85+
assertThat(result, equalTo(content2));
86+
87+
}
88+
89+
private String getHttpContent(String url) throws IOException
90+
{
5391
final HttpGet request = new HttpGet( url );
5492
final CloseableHttpClient client = HttpClients.createDefault();
55-
CloseableHttpResponse response = null;
5693

57-
InputStream stream = null;
58-
try
94+
try(CloseableHttpResponse response = client.execute( request ))
5995
{
60-
response = client.execute( request );
61-
stream = response.getEntity().getContent();
62-
final String result = IOUtils.toString( stream );
63-
64-
assertThat( result, notNullValue() );
65-
assertThat( result, equalTo( content ) );
96+
InputStream stream = response.getEntity().getContent();
97+
return IOUtils.toString( stream );
6698
}
6799
finally
68100
{
69-
IOUtils.closeQuietly( stream );
70-
if ( response != null && response.getEntity() != null )
71-
{
72-
EntityUtils.consumeQuietly( response.getEntity() );
73-
IOUtils.closeQuietly( response );
74-
}
75-
76-
if ( request != null )
77-
{
78-
request.reset();
79-
}
80-
81-
if ( client != null )
82-
{
83-
IOUtils.closeQuietly( client );
84-
}
101+
IOUtils.closeQuietly( client );
85102
}
86-
87-
System.out.println( server.getAccessesByPathKey() );
88-
89-
final String key = server.getAccessKey( CommonMethod.GET.name(), path );
90-
System.out.println( "Getting accesses for: '" + key + "'" );
91-
assertThat( server.getAccessesByPathKey().get( key ), equalTo( 1 ) );
92103
}
93-
94104
}

0 commit comments

Comments
 (0)