11package org .baeldung .httpclient ;
22
3+ import static org .junit .Assert .assertEquals ;
4+
35import java .io .IOException ;
46
57import org .apache .http .Header ;
2426import org .apache .http .impl .client .HttpClients ;
2527import org .apache .http .impl .cookie .BasicClientCookie ;
2628import org .apache .http .util .EntityUtils ;
29+ import org .junit .Ignore ;
2730import org .junit .Test ;
2831
2932public class SandboxTest {
3033
34+ // original example
35+ @ Ignore
3136 @ Test
32- public final void whenInterestingDigestAuthScenario_then200OK () throws AuthenticationException , ClientProtocolException , IOException , MalformedChallengeException {
37+ public final void whenInterestingDigestAuthScenario_then401UnAuthorized () throws AuthenticationException , ClientProtocolException , IOException , MalformedChallengeException {
3338 final HttpHost targetHost = new HttpHost ("httpbin.org" , 80 , "http" );
3439
3540 // set up the credentials to run agains the server
3641 final CredentialsProvider credsProvider = new BasicCredentialsProvider ();
3742 credsProvider .setCredentials (new AuthScope (targetHost .getHostName (), targetHost .getPort ()), new UsernamePasswordCredentials ("user" , "passwd" ));
3843
39- // This endpoint need fake cookie to work properly
40- final CookieStore cookieStore = new BasicCookieStore ();
41- final BasicClientCookie cookie = new BasicClientCookie ("fake" , "fake_value" );
42- cookie .setDomain ("httpbin.org" );
43- cookie .setPath ("/" );
44- cookieStore .addCookie (cookie );
45-
4644 // We need a first run to get a 401 to seed the digest auth
4745
4846 // Make a client using those creds
49- final CloseableHttpClient client = HttpClients .custom ().setDefaultCookieStore ( cookieStore ). setDefaultCredentialsProvider (credsProvider ).build ();
47+ final CloseableHttpClient client = HttpClients .custom ().setDefaultCredentialsProvider (credsProvider ).build ();
5048
5149 // And make a call to the URL we are after
5250 final HttpGet httpget = new HttpGet ("http://httpbin.org/digest-auth/auth/user/passwd" );
@@ -91,6 +89,49 @@ public final void whenInterestingDigestAuthScenario_then200OK() throws Authentic
9189 responseGood .close ();
9290 }
9391 }
92+ }
93+
94+ @ Test
95+ public final void whenInterestingDigestAuthScenario_then200OK () throws AuthenticationException , ClientProtocolException , IOException , MalformedChallengeException {
96+ final HttpHost targetHost = new HttpHost ("httpbin.org" , 80 , "http" );
97+
98+ // set up the credentials to run agains the server
99+ final CredentialsProvider credsProvider = new BasicCredentialsProvider ();
100+ credsProvider .setCredentials (new AuthScope (targetHost .getHostName (), targetHost .getPort ()), new UsernamePasswordCredentials ("user" , "passwd" ));
101+
102+ // This endpoint need fake cookie to work properly
103+ final CookieStore cookieStore = new BasicCookieStore ();
104+ final BasicClientCookie cookie = new BasicClientCookie ("fake" , "fake_value" );
105+ cookie .setDomain ("httpbin.org" );
106+ cookie .setPath ("/" );
107+ cookieStore .addCookie (cookie );
108+
109+ // Make a client using those creds
110+ final CloseableHttpClient client = HttpClients .custom ().setDefaultCookieStore (cookieStore ).setDefaultCredentialsProvider (credsProvider ).build ();
111+
112+ // And make a call to the URL we are after
113+ final HttpGet httpget = new HttpGet ("http://httpbin.org/digest-auth/auth/user/passwd" );
114+
115+ // Create a context to use
116+ final HttpClientContext context = HttpClientContext .create ();
117+
118+ // Get a response from the sever (401 implicitly)
119+ final HttpResponse authResponse = client .execute (targetHost , httpget , context );
120+ assertEquals (200 , authResponse .getStatusLine ().getStatusCode ());
121+
122+ // HttpClient will use cached digest parameters for future requests
123+ System .out .println ("Executing request " + httpget .getRequestLine () + " to target " + targetHost );
124+
125+ for (int i = 0 ; i < 3 ; i ++) {
126+ final CloseableHttpResponse responseGood = client .execute (targetHost , httpget , context );
127+
128+ try {
129+ System .out .println ("----------------------------------------" );
130+ assertEquals (200 , responseGood .getStatusLine ().getStatusCode ());
131+ } finally {
132+ responseGood .close ();
133+ }
134+ }
94135 client .close ();
95136 }
96137
@@ -117,7 +158,7 @@ public final void whenWeKnowDigestParameters_thenNo401Status() throws Authentica
117158 // == end
118159 System .out .println ("Executing The Request knowing the digest parameters ==== " );
119160 final HttpResponse authResponse = client .execute (targetHost , httpget , context );
120- System . out . println ( authResponse .toString ());
161+ assertEquals ( 200 , authResponse .getStatusLine (). getStatusCode ());
121162 client .close ();
122163 }
123164
@@ -133,12 +174,12 @@ public final void whenDoNotKnowParameters_thenOnlyOne401() throws Authentication
133174 final HttpGet httpget = new HttpGet ("http://localhost:8080/spring-security-rest-digest-auth/api/foos/1" );
134175 System .out .println ("Executing The Request NOT knowing the digest parameters ==== " );
135176 final HttpResponse tempResponse = client .execute (targetHost , httpget , context );
136- System . out . println ( tempResponse .toString ());
177+ assertEquals ( 200 , tempResponse .getStatusLine (). getStatusCode ());
137178
138179 for (int i = 0 ; i < 3 ; i ++) {
139180 System .out .println ("No more Challenges or 401" );
140181 final CloseableHttpResponse authResponse = client .execute (targetHost , httpget , context );
141- System . out . println ( authResponse .toString ());
182+ assertEquals ( 200 , authResponse .getStatusLine (). getStatusCode ());
142183 authResponse .close ();
143184 }
144185 client .close ();
0 commit comments