hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r1600737 - in /httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http: impl/client/integration/TestClientAuthentication.java localserver/ResponseBasicUnauthorized.java
Date Thu, 05 Jun 2014 19:30:05 GMT
Author: olegk
Date: Thu Jun  5 19:30:05 2014
New Revision: 1600737

URL: http://svn.apache.org/r1600737
Log:
Test case for HTTPCLIENT-1490

Modified:
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java?rev=1600737&r1=1600736&r2=1600737&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/integration/TestClientAuthentication.java
Thu Jun  5 19:30:05 2014
@@ -38,6 +38,7 @@ import org.apache.http.HttpInetConnectio
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
+import org.apache.http.auth.AUTH;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.Credentials;
 import org.apache.http.auth.UsernamePasswordCredentials;
@@ -398,6 +399,82 @@ public class TestClientAuthentication ex
         Assert.assertEquals(1, authStrategy.getCount());
     }
 
+    static class RealmAuthHandler implements HttpRequestHandler {
+
+        final String realm;
+        final String realmCreds;
+
+        public RealmAuthHandler(final String realm, final String realmCreds) {
+            this.realm = realm;
+            this.realmCreds = realmCreds;
+        }
+
+        @Override
+        public void handle(
+                final HttpRequest request,
+                final HttpResponse response,
+                final HttpContext context) throws HttpException, IOException {
+            final String givenCreds = (String) context.getAttribute("creds");
+            if (givenCreds == null || !givenCreds.equals(this.realmCreds)) {
+                response.setStatusCode(HttpStatus.SC_UNAUTHORIZED);
+                response.addHeader(AUTH.WWW_AUTH, "Basic realm=\"" + this.realm + "\"");
+            } else {
+                response.setStatusCode(HttpStatus.SC_OK);
+                final StringEntity entity = new StringEntity("success", Consts.ASCII);
+                response.setEntity(entity);
+            }
+        }
+
+    }
+
+    @Test
+    public void testAuthenticationCredentialsCachingReauthenticationOnDifferentRealm() throws
Exception {
+        this.localServer.register("/this", new RealmAuthHandler("this realm", "test:this"));
+        this.localServer.register("/that", new RealmAuthHandler("that realm", "test:that"));
+
+        final HttpHost targethost = getServerHttp();
+
+        final TestTargetAuthenticationStrategy authStrategy = new TestTargetAuthenticationStrategy();
+        final BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
+        credsProvider.setCredentials(new AuthScope(targethost, "this realm", null),
+                new UsernamePasswordCredentials("test", "this"));
+        credsProvider.setCredentials(new AuthScope(targethost, "that realm", null),
+                new UsernamePasswordCredentials("test", "that"));
+
+        this.httpclient = HttpClients.custom()
+                .setDefaultCredentialsProvider(credsProvider)
+                .setTargetAuthenticationStrategy(authStrategy)
+                .build();
+
+        final HttpClientContext context = HttpClientContext.create();
+
+        final HttpGet httpget1 = new HttpGet("/this");
+
+        final HttpResponse response1 = this.httpclient.execute(targethost, httpget1, context);
+        final HttpEntity entity1 = response1.getEntity();
+        Assert.assertEquals(HttpStatus.SC_OK, response1.getStatusLine().getStatusCode());
+        Assert.assertNotNull(entity1);
+        EntityUtils.consume(entity1);
+
+        final HttpGet httpget2 = new HttpGet("/this");
+
+        final HttpResponse response2 = this.httpclient.execute(targethost, httpget2, context);
+        final HttpEntity entity2 = response1.getEntity();
+        Assert.assertEquals(HttpStatus.SC_OK, response2.getStatusLine().getStatusCode());
+        Assert.assertNotNull(entity2);
+        EntityUtils.consume(entity2);
+
+        final HttpGet httpget3 = new HttpGet("/that");
+
+        final HttpResponse response3 = this.httpclient.execute(targethost, httpget3, context);
+        final HttpEntity entity3 = response1.getEntity();
+        Assert.assertEquals(HttpStatus.SC_OK, response3.getStatusLine().getStatusCode());
+        Assert.assertNotNull(entity3);
+        EntityUtils.consume(entity3);
+
+        Assert.assertEquals(2, authStrategy.getCount());
+    }
+
     @Test
     public void testAuthenticationUserinfoInRequestSuccess() throws Exception {
         this.localServer.register("*", new AuthHandler());

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java?rev=1600737&r1=1600736&r2=1600737&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java
(original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/localserver/ResponseBasicUnauthorized.java
Thu Jun  5 19:30:05 2014
@@ -43,7 +43,9 @@ public class ResponseBasicUnauthorized i
             final HttpResponse response,
             final HttpContext context) throws HttpException, IOException {
         if (response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED) {
-            response.addHeader(AUTH.WWW_AUTH, "Basic realm=\"test realm\"");
+            if (!response.containsHeader(AUTH.WWW_AUTH)) {
+                response.addHeader(AUTH.WWW_AUTH, "Basic realm=\"test realm\"");
+            }
         }
     }
 



Mime
View raw message