auth work

This commit is contained in:
eugenp 2014-01-30 21:14:12 +02:00
parent 281cb6a31c
commit 648493db94
3 changed files with 10 additions and 17 deletions

View File

@ -27,13 +27,13 @@ public class HttpClientAuthLiveTest {
private static final String DEFAULT_USER = "user1"; private static final String DEFAULT_USER = "user1";
private static final String DEFAULT_PASS = "user1Pass"; private static final String DEFAULT_PASS = "user1Pass";
private CloseableHttpClient instance; private CloseableHttpClient client;
private CloseableHttpResponse response; private CloseableHttpResponse response;
@Before @Before
public final void before() { public final void before() {
instance = HttpClientBuilder.create().build(); client = HttpClientBuilder.create().build();
} }
@After @After
@ -57,9 +57,9 @@ public class HttpClientAuthLiveTest {
@Test @Test
public final void whenExecutingBasicGetRequestWithBasicAuthenticationEnabled_thenSuccess() throws ClientProtocolException, IOException { 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(); final int statusCode = response.getStatusLine().getStatusCode();
assertThat(statusCode, equalTo(HttpStatus.SC_OK)); assertThat(statusCode, equalTo(HttpStatus.SC_OK));

View File

@ -10,12 +10,10 @@ import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider; import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet; 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.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClientBuilder;
import org.junit.After; import org.junit.After;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class HttpClientSandboxLiveTest { public class HttpClientSandboxLiveTest {
@ -24,11 +22,6 @@ public class HttpClientSandboxLiveTest {
private CloseableHttpResponse response; private CloseableHttpResponse response;
@Before
public final void before() {
client = HttpClientBuilder.create().build();
}
@After @After
public final void after() throws IllegalStateException, IOException { public final void after() throws IllegalStateException, IOException {
if (response == null) { if (response == null) {
@ -54,11 +47,12 @@ public class HttpClientSandboxLiveTest {
@Test @Test
public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException { public final void givenGetRequestExecuted_whenAnalyzingTheResponse_thenCorrectStatusCode() throws ClientProtocolException, IOException {
final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
final AuthScope authscp = new AuthScope("api.calltrackingmetrics.com", 443); final AuthScope authscp = new AuthScope("localhost", 8080);
credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("mykey", "mysecret")); credentialsProvider.setCredentials(authscp, new UsernamePasswordCredentials("user1", "user1Pass"));
final HttpClientContext localContext = HttpClientContext.create();
localContext.setCredentialsProvider(credentialsProvider); client = HttpClientBuilder.create().setDefaultCredentialsProvider(credentialsProvider).build();
final HttpGet httpGet = new HttpGet("https://api.calltrackingmetrics.com/api/v1/accounts/myaccout/calls.json");
final HttpGet httpGet = new HttpGet("http://localhost:8080/spring-security-rest-basic-auth/api/foos/1");
response = client.execute(httpGet); response = client.execute(httpGet);
System.out.println(response.getStatusLine()); System.out.println(response.getStatusLine());

View File

@ -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,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,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] 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]