2727import org .junit .Rule ;
2828import org .junit .Test ;
2929
30+ import java .io .IOException ;
3031import java .io .InputStream ;
3132
3233import 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