1010import org .apache .http .client .CredentialsProvider ;
1111import org .apache .http .client .methods .CloseableHttpResponse ;
1212import org .apache .http .client .methods .HttpGet ;
13- import org .apache .http .client .protocol .HttpClientContext ;
1413import org .apache .http .impl .client .BasicCredentialsProvider ;
1514import org .apache .http .impl .client .CloseableHttpClient ;
1615import org .apache .http .impl .client .HttpClientBuilder ;
1716import org .junit .After ;
18- import org .junit .Before ;
1917import org .junit .Test ;
2018
2119public class HttpClientSandboxLiveTest {
@@ -24,11 +22,6 @@ public class HttpClientSandboxLiveTest {
2422
2523 private CloseableHttpResponse response ;
2624
27- @ Before
28- public final void before () {
29- client = HttpClientBuilder .create ().build ();
30- }
31-
3225 @ After
3326 public final void after () throws IllegalStateException , IOException {
3427 if (response == null ) {
@@ -54,11 +47,12 @@ public final void after() throws IllegalStateException, IOException {
5447 @ Test
5548 public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode () throws ClientProtocolException , IOException {
5649 final CredentialsProvider credentialsProvider = new BasicCredentialsProvider ();
57- final AuthScope authscp = new AuthScope ("api.calltrackingmetrics.com" , 443 );
58- credentialsProvider .setCredentials (authscp , new UsernamePasswordCredentials ("mykey" , "mysecret" ));
59- final HttpClientContext localContext = HttpClientContext .create ();
60- localContext .setCredentialsProvider (credentialsProvider );
61- final HttpGet httpGet = new HttpGet ("https://api.calltrackingmetrics.com/api/v1/accounts/myaccout/calls.json" );
50+ final AuthScope authscp = new AuthScope ("localhost" , 8080 );
51+ credentialsProvider .setCredentials (authscp , new UsernamePasswordCredentials ("user1" , "user1Pass" ));
52+
53+ client = HttpClientBuilder .create ().setDefaultCredentialsProvider (credentialsProvider ).build ();
54+
55+ final HttpGet httpGet = new HttpGet ("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1" );
6256 response = client .execute (httpGet );
6357
6458 System .out .println (response .getStatusLine ());
0 commit comments