Skip to content

Commit 648493d

Browse files
author
eugenp
committed
auth work
1 parent 281cb6a commit 648493d

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

httpclient/src/test/java/org/baeldung/httpclient/HttpClientAuthLiveTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@ public class HttpClientAuthLiveTest {
2727
private static final String DEFAULT_USER = "user1";
2828
private static final String DEFAULT_PASS = "user1Pass";
2929

30-
private CloseableHttpClient instance;
30+
private CloseableHttpClient client;
3131

3232
private CloseableHttpResponse response;
3333

3434
@Before
3535
public final void before() {
36-
instance = HttpClientBuilder.create().build();
36+
client = HttpClientBuilder.create().build();
3737
}
3838

3939
@After
@@ -57,9 +57,9 @@ public final void after() throws IllegalStateException, IOException {
5757

5858
@Test
5959
public final void whenExecutingBasicGetRequestWithBasicAuthenticationEnabled_thenSuccess() throws ClientProtocolException, IOException {
60-
instance = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).build();
60+
client = HttpClientBuilder.create().setDefaultCredentialsProvider(provider()).build();
6161

62-
response = instance.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION));
62+
response = client.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION));
6363

6464
final int statusCode = response.getStatusLine().getStatusCode();
6565
assertThat(statusCode, equalTo(HttpStatus.SC_OK));

httpclient/src/test/java/org/baeldung/httpclient/HttpClientSandboxLiveTest.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@
1010
import org.apache.http.client.CredentialsProvider;
1111
import org.apache.http.client.methods.CloseableHttpResponse;
1212
import org.apache.http.client.methods.HttpGet;
13-
import org.apache.http.client.protocol.HttpClientContext;
1413
import org.apache.http.impl.client.BasicCredentialsProvider;
1514
import org.apache.http.impl.client.CloseableHttpClient;
1615
import org.apache.http.impl.client.HttpClientBuilder;
1716
import org.junit.After;
18-
import org.junit.Before;
1917
import org.junit.Test;
2018

2119
public 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());

httpclient/src/test/resources/sandbox.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
web - 2014-01-30 20:48:07,161 [main] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match
21
web - 2014-01-30 20:48:07,171 [main] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
32
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]
43
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]

0 commit comments

Comments
 (0)