Skip to content

Commit 4a4cf84

Browse files
author
eugenp
committed
basic authentication work
1 parent 648493d commit 4a4cf84

1 file changed

Lines changed: 71 additions & 0 deletions

File tree

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

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,27 @@
55

66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.nio.charset.Charset;
89

10+
import org.apache.commons.codec.binary.Base64;
911
import org.apache.http.HttpEntity;
12+
import org.apache.http.HttpHeaders;
13+
import org.apache.http.HttpHost;
1014
import org.apache.http.HttpStatus;
1115
import org.apache.http.auth.AuthScope;
1216
import org.apache.http.auth.UsernamePasswordCredentials;
17+
import org.apache.http.client.AuthCache;
1318
import org.apache.http.client.ClientProtocolException;
1419
import org.apache.http.client.CredentialsProvider;
1520
import org.apache.http.client.methods.CloseableHttpResponse;
1621
import org.apache.http.client.methods.HttpGet;
22+
import org.apache.http.client.protocol.HttpClientContext;
23+
import org.apache.http.impl.auth.BasicScheme;
24+
import org.apache.http.impl.client.BasicAuthCache;
1725
import org.apache.http.impl.client.BasicCredentialsProvider;
1826
import org.apache.http.impl.client.CloseableHttpClient;
1927
import org.apache.http.impl.client.HttpClientBuilder;
28+
import org.apache.http.protocol.HttpContext;
2029
import org.junit.After;
2130
import org.junit.Before;
2231
import org.junit.Test;
@@ -65,6 +74,42 @@ public final void whenExecutingBasicGetRequestWithBasicAuthenticationEnabled_the
6574
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
6675
}
6776

77+
@Test
78+
public final void givenAuthenticationIsPreemptive_whenExecutingBasicGetRequestWithBasicAuthenticationEnabled_thenSuccess() throws ClientProtocolException, IOException {
79+
client = HttpClientBuilder.create().build();
80+
response = client.execute(new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION), context());
81+
82+
final int statusCode = response.getStatusLine().getStatusCode();
83+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
84+
}
85+
86+
@Test
87+
public final void givenAuthorizationHeaderIsSetManually_whenExecutingGetRequest_thenSuccess() throws ClientProtocolException, IOException {
88+
client = HttpClientBuilder.create().build();
89+
90+
final HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
91+
request.setHeader(HttpHeaders.AUTHORIZATION, authorizationHeader(DEFAULT_USER, DEFAULT_PASS));
92+
response = client.execute(request);
93+
94+
final int statusCode = response.getStatusLine().getStatusCode();
95+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
96+
}
97+
98+
@Test
99+
public final void givenAuthorizationHeaderIsSetManually_whenExecutingGetRequest_thenSuccess2() throws ClientProtocolException, IOException {
100+
final HttpGet request = new HttpGet(URL_SECURED_BY_BASIC_AUTHENTICATION);
101+
final String auth = DEFAULT_USER + ":" + DEFAULT_PASS;
102+
final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
103+
final String authHeader = "Basic " + new String(encodedAuth);
104+
request.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
105+
106+
client = HttpClientBuilder.create().build();
107+
response = client.execute(request);
108+
109+
final int statusCode = response.getStatusLine().getStatusCode();
110+
assertThat(statusCode, equalTo(HttpStatus.SC_OK));
111+
}
112+
68113
// UTILS
69114

70115
private final CredentialsProvider provider() {
@@ -74,4 +119,30 @@ private final CredentialsProvider provider() {
74119
return provider;
75120
}
76121

122+
private final HttpContext context() {
123+
final HttpHost targetHost = new HttpHost("localhost", 8080, "http");
124+
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
125+
credsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(DEFAULT_USER, DEFAULT_PASS));
126+
127+
// Create AuthCache instance
128+
final AuthCache authCache = new BasicAuthCache();
129+
// Generate BASIC scheme object and add it to the local auth cache
130+
authCache.put(targetHost, new BasicScheme());
131+
132+
// Add AuthCache to the execution context
133+
final HttpClientContext context = HttpClientContext.create();
134+
context.setCredentialsProvider(credsProvider);
135+
context.setAuthCache(authCache);
136+
137+
return context;
138+
}
139+
140+
private final String authorizationHeader(final String username, final String password) {
141+
final String auth = username + ":" + password;
142+
final byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
143+
final String authHeader = "Basic " + new String(encodedAuth);
144+
145+
return authHeader;
146+
}
147+
77148
}

0 commit comments

Comments
 (0)