You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2012/03/01 23:10:49 UTC

svn commit: r1295938 - /httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java

Author: olegk
Date: Thu Mar  1 22:10:48 2012
New Revision: 1295938

URL: http://svn.apache.org/viewvc?rev=1295938&view=rev
Log:
Test case for HTTPCLIENT-1171

Modified:
    httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java

Modified: httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java?rev=1295938&r1=1295937&r2=1295938&view=diff
==============================================================================
--- httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java (original)
+++ httpcomponents/httpclient/branches/4.1.x/httpclient/src/test/java/org/apache/http/impl/client/TestClientAuthentication.java Thu Mar  1 22:10:48 2012
@@ -28,12 +28,16 @@ package org.apache.http.impl.client;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 
+import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpResponse;
 import org.apache.http.HttpStatus;
+import org.apache.http.auth.AuthScheme;
+import org.apache.http.auth.AuthSchemeFactory;
 import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.AuthenticationException;
 import org.apache.http.auth.Credentials;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.ClientProtocolException;
@@ -42,14 +46,17 @@ import org.apache.http.client.NonRepeata
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpPut;
+import org.apache.http.client.params.AuthPolicy;
 import org.apache.http.entity.InputStreamEntity;
 import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.auth.BasicScheme;
 import org.apache.http.localserver.BasicAuthTokenExtractor;
 import org.apache.http.localserver.BasicServerTestBase;
 import org.apache.http.localserver.LocalTestServer;
 import org.apache.http.localserver.RequestBasicAuth;
 import org.apache.http.localserver.ResponseBasicUnauthorized;
 import org.apache.http.params.CoreProtocolPNames;
+import org.apache.http.params.HttpParams;
 import org.apache.http.protocol.BasicHttpContext;
 import org.apache.http.protocol.BasicHttpProcessor;
 import org.apache.http.protocol.HTTP;
@@ -396,4 +403,44 @@ public class TestClientAuthentication ex
         Assert.assertEquals(1, authHandler.getCount());
     }
 
+    @Test
+    public void testBasicAuthenticationException() throws Exception {
+        localServer.register("*", new AuthHandler());
+        localServer.start();
+
+        BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
+        credsProvider.setCredentials(AuthScope.ANY,
+                new UsernamePasswordCredentials("test", "test"));
+
+        DefaultHttpClient httpclient = new DefaultHttpClient();
+        httpclient.getAuthSchemes().register(AuthPolicy.BASIC, new AuthSchemeFactory() {
+            
+            public AuthScheme newInstance(final HttpParams params) {
+                
+                return new BasicScheme() {
+
+                    @Override
+                    public Header authenticate(
+                            final Credentials credentials, 
+                            final HttpRequest request) throws AuthenticationException {
+                        throw new AuthenticationException("Oopsie");
+                    }
+                    
+                };
+            }
+        });
+        
+        httpclient.setCredentialsProvider(credsProvider);
+
+        HttpContext context = new BasicHttpContext();
+
+        HttpGet httpget = new HttpGet("/");
+
+        HttpResponse response = httpclient.execute(getServerHttp(), httpget, context);
+        HttpEntity entity = response.getEntity();
+        Assert.assertEquals(HttpStatus.SC_UNAUTHORIZED, response.getStatusLine().getStatusCode());
+        Assert.assertNotNull(entity);
+        EntityUtils.consume(entity);
+    }
+
 }