Skip to content

Commit 2375e09

Browse files
authored
Take query params into account in expectation (#38)
1 parent b6bbba2 commit 2375e09

File tree

2 files changed

+100
-21
lines changed

2 files changed

+100
-21
lines changed

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

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

101-
private String getPath( final String path )
101+
private String getPath( final String testUrl )
102102
{
103-
String realPath = path;
104103
try
105104
{
106-
final URL u = new URL( path );
107-
realPath = u.getPath();
105+
return new URL( testUrl ).getFile(); // path plus query parameters
108106
}
109107
catch ( final MalformedURLException e )
110108
{
109+
e.printStackTrace();
111110
}
112-
113-
return realPath;
111+
return testUrl;
114112
}
115113

116114
public void expect( final String method, final String testUrl, final int responseCode, final String body )
@@ -147,22 +145,8 @@ public void expect( String method, String testUrl, ExpectationHandler handler )
147145
protected void service( final HttpServletRequest req, final HttpServletResponse resp )
148146
throws ServletException, IOException
149147
{
150-
String wholePath;
151-
try
152-
{
153-
wholePath = new URI( req.getRequestURI() ).getPath();
154-
}
155-
catch ( final URISyntaxException e )
156-
{
157-
throw new ServletException( "Cannot parse request URI", e );
158-
}
159-
160-
String path = wholePath;
161-
if ( path.length() > 1 )
162-
{
163-
path = path.substring( 1 );
164-
}
165148

149+
String wholePath = getWholePath( req );
166150
final String key = getAccessKey( req.getMethod(), wholePath );
167151

168152
logger.info( "Looking up expectation for: {}", key );
@@ -244,6 +228,21 @@ else if ( expectation.bodyStream() != null )
244228
resp.setStatus( 404 );
245229
}
246230

231+
private String getWholePath( HttpServletRequest request )
232+
{
233+
String requestURI = request.getRequestURI();
234+
String queryString = request.getQueryString();
235+
236+
if ( queryString == null )
237+
{
238+
return requestURI;
239+
}
240+
else
241+
{
242+
return requestURI + "?" + queryString;
243+
}
244+
}
245+
247246
public String getAccessKey( final CommonMethod method, final String path )
248247
{
249248
return getAccessKey( method.name(), path );
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* Copyright (C) 2015-2022 Red Hat, Inc. (http://github.com/Commonjava/http-testserver)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.commonjava.test.http.expect;
17+
18+
import org.apache.commons.io.IOUtils;
19+
import org.apache.http.client.methods.CloseableHttpResponse;
20+
import org.apache.http.client.methods.HttpGet;
21+
import org.apache.http.impl.client.CloseableHttpClient;
22+
import org.apache.http.impl.client.HttpClients;
23+
import org.commonjava.test.http.common.CommonMethod;
24+
import org.junit.Rule;
25+
import org.junit.Test;
26+
27+
import java.io.IOException;
28+
import java.io.InputStream;
29+
30+
import static org.hamcrest.CoreMatchers.equalTo;
31+
import static org.junit.Assert.assertThat;
32+
33+
public class ExpectationServerTest
34+
{
35+
36+
@Rule
37+
public ExpectationServer server = new ExpectationServer( "repos" );
38+
39+
private final String subPath = "/path/to/something";
40+
41+
@Test
42+
public void downloadWithQueryParams()
43+
throws Exception
44+
{
45+
final String path1 = subPath + "?version=1.0";
46+
final String path2 = subPath + "?version=2.0";
47+
48+
final String url1 = server.formatUrl(path1);
49+
final String url2 = server.formatUrl(path2);
50+
51+
final String content1 = "this is a test version 1";
52+
final String content2 = "this is a test version 2";
53+
54+
server.expect(url1, 200, content1);
55+
server.expect(url2, 200, content2);
56+
57+
String result = getHttpContent(url1);
58+
assertThat(result, equalTo(content1));
59+
60+
result = getHttpContent(url2);
61+
assertThat(result, equalTo(content2));
62+
63+
}
64+
65+
private String getHttpContent(String url) throws IOException
66+
{
67+
final HttpGet request = new HttpGet( url );
68+
final CloseableHttpClient client = HttpClients.createDefault();
69+
70+
try(CloseableHttpResponse response = client.execute( request ))
71+
{
72+
InputStream stream = response.getEntity().getContent();
73+
return IOUtils.toString( stream );
74+
}
75+
finally
76+
{
77+
IOUtils.closeQuietly( client );
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)