diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java index 6614b05cc3..df55de7d3a 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java @@ -27,13 +27,13 @@ public class HttpClientAuthLiveTest { private static final String DEFAULT_USER = "user1"; private static final String DEFAULT_PASS = "user1Pass"; - private CloseableHttpClient instance; + private CloseableHttpClient client; private CloseableHttpResponse response; @Before public final void before() { - instance = HttpClientBuilder.create().build(); + client = HttpClientBuilder.create().build(); } @After @@ -57,9 +57,9 @@ public class HttpClientAuthLiveTest { @Test public final void whenExecutingBasicGetRequestWithBasicAuthenticationEnabled_thenSuccess() throws ClientProtocolException, IOException { - instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).build(); + client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).build(); - response = instance.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION)); + response = client.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION)); final int statusCode = response.getStatusLine().getStatusCode(); assertThat(statusCode, equalTo(HttpStatus.SC_OK)); diff --git a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientSandboxLiveTest.java b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientSandboxLiveTest.java index e3661ffbd9..d8e687df1b 100644 --- a/httpclient/src/test/java/org/baeldung/httpclient/HttpClientSandboxLiveTest.java +++ b/httpclient/src/test/java/org/baeldung/httpclient/HttpClientSandboxLiveTest.java @@ -10,12 +10,10 @@ import org.apache.http.client.ClientProtocolException; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; -import org.apache.http.client.protocol.HttpClientContext; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.junit.After; -import org.junit.Before; import org.junit.Test; public class HttpClientSandboxLiveTest { @@ -24,11 +22,6 @@ public class HttpClientSandboxLiveTest { private CloseableHttpResponse response; - @Before - public final void before() { - client = HttpClientBuilder.create().build(); - } - @After public final void after() throws IllegalStateException, IOException { if (response == null) { @@ -54,11 +47,12 @@ public class HttpClientSandboxLiveTest { @Test public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); - final AuthScope authscp = new AuthScope("api.calltrackingmetrics.com", 443); - credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("mykey", "mysecret")); - final HttpClientContext localContext = HttpClientContext.create(); - localContext.setCredentialsProvider(credentialsProvider); - final HttpGet httpGet = new HttpGet("https://api.calltrackingmetrics.com/api/v1/accounts/myaccout/calls.json"); + final AuthScope authscp = new AuthScope("localhost", 8080); + credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass")); + + client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build(); + + final HttpGet httpGet = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1"); response = client.execute(httpGet); System.out.println(response.getStatusLine()); diff --git a/httpclient/src/test/resources/sandbox.txt b/httpclient/src/test/resources/sandbox.txt index 9925166fa6..e0352ba839 100644 --- a/httpclient/src/test/resources/sandbox.txt +++ b/httpclient/src/test/resources/sandbox.txt @@ -1,4 +1,3 @@ -web - 2014-01-30 20:48:07,161 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match web - 2014-01-30 20:48:07,171 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context web - 2014-01-30 20:48:07,172 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://localhost:8080][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20] web - 2014-01-30 20:48:07,185 [main] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 0][route: {}->http://localhost:8080][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]