Skip to content

Commit 1aa9d52

Browse files
committed
Take query params into account in expectation
1 parent f89cc81 commit 1aa9d52

File tree

2 files changed

+62
-55
lines changed

2 files changed

+62
-55
lines changed

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

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -97,20 +97,17 @@ public String getAccessKey( final String method, final String path )
9797
return method.toUpperCase() + " " + path;
9898
}
9999

100-
private String getPath( final String path )
100+
private String getPath( final String testUrl )
101101
{
102-
String realPath = path;
103102
try
104103
{
105-
final URL u = new URL( path );
106-
realPath = u.getPath();
104+
return new URL( testUrl ).getFile(); // path plus query parameters
107105
}
108106
catch ( final MalformedURLException e )
109107
{
110-
//Do nothing
108+
e.printStackTrace();
111109
}
112-
113-
return realPath;
110+
return testUrl;
114111
}
115112

116113
public void expect( final String method, final String testUrl, final int responseCode, final String body )
@@ -144,22 +141,7 @@ public void expect( String method, String testUrl, ExpectationHandler handler )
144141
protected void service( final HttpServletRequest req, final HttpServletResponse resp )
145142
throws ServletException, IOException
146143
{
147-
String wholePath;
148-
try
149-
{
150-
wholePath = new URI( req.getRequestURI() ).getPath();
151-
}
152-
catch ( final URISyntaxException e )
153-
{
154-
throw new ServletException( "Cannot parse request URI", e );
155-
}
156-
157-
String path = wholePath;
158-
if ( path.length() > 1 )
159-
{
160-
path = path.substring( 1 );
161-
}
162-
144+
String wholePath = getWholePath( req );
163145
final String key = getAccessKey( req.getMethod(), wholePath );
164146

165147
logger.info( "Looking up expectation for: {}", key );
@@ -232,6 +214,21 @@ else if ( expectation.bodyStream() != null )
232214
resp.setStatus( 404 );
233215
}
234216

217+
private String getWholePath( HttpServletRequest request )
218+
{
219+
String requestURI = request.getRequestURI();
220+
String queryString = request.getQueryString();
221+
222+
if ( queryString == null )
223+
{
224+
return requestURI;
225+
}
226+
else
227+
{
228+
return requestURI + "?" + queryString;
229+
}
230+
}
231+
235232
public String getAccessKey( final CommonMethod method, final String path )
236233
{
237234
return getAccessKey( method.name(), 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)