55
66import java .io .IOException ;
77import java .io .InputStream ;
8+ import java .nio .charset .Charset ;
89
10+ import org .apache .commons .codec .binary .Base64 ;
911import org .apache .http .HttpEntity ;
12+ import org .apache .http .HttpHeaders ;
13+ import org .apache .http .HttpHost ;
1014import org .apache .http .HttpStatus ;
1115import org .apache .http .auth .AuthScope ;
1216import org .apache .http .auth .UsernamePasswordCredentials ;
17+ import org .apache .http .client .AuthCache ;
1318import org .apache .http .client .ClientProtocolException ;
1419import org .apache .http .client .CredentialsProvider ;
1520import org .apache .http .client .methods .CloseableHttpResponse ;
1621import 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 ;
1725import org .apache .http .impl .client .BasicCredentialsProvider ;
1826import org .apache .http .impl .client .CloseableHttpClient ;
1927import org .apache .http .impl .client .HttpClientBuilder ;
28+ import org .apache .http .protocol .HttpContext ;
2029import org .junit .After ;
2130import org .junit .Before ;
2231import 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