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 2011/10/04 00:17:02 UTC
svn commit: r1178616 - in /httpcomponents/httpclient/trunk: .gitignore
httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java
Author: olegk
Date: Mon Oct 3 22:17:01 2011
New Revision: 1178616
URL: http://svn.apache.org/viewvc?rev=1178616&view=rev
Log:
Unit tests for AuthenticationStrategyImpl
Added:
httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java (with props)
Modified:
httpcomponents/httpclient/trunk/.gitignore
Modified: httpcomponents/httpclient/trunk/.gitignore
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/.gitignore?rev=1178616&r1=1178615&r2=1178616&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/.gitignore (original)
+++ httpcomponents/httpclient/trunk/.gitignore Mon Oct 3 22:17:01 2011
@@ -1,4 +1,5 @@
.classpath
.project
.settings
+.clover
target
Added: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java?rev=1178616&view=auto
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java (added)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java Mon Oct 3 22:17:01 2011
@@ -0,0 +1,293 @@
+/*
+ * ====================================================================
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ */
+
+package org.apache.http.impl.client;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Queue;
+
+import org.apache.http.Header;
+import org.apache.http.HttpHost;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.HttpVersion;
+import org.apache.http.auth.AUTH;
+import org.apache.http.auth.AuthOption;
+import org.apache.http.auth.AuthSchemeRegistry;
+import org.apache.http.auth.AuthScope;
+import org.apache.http.auth.UsernamePasswordCredentials;
+import org.apache.http.auth.params.AuthPNames;
+import org.apache.http.client.CredentialsProvider;
+import org.apache.http.client.params.AuthPolicy;
+import org.apache.http.client.protocol.ClientContext;
+import org.apache.http.impl.auth.BasicScheme;
+import org.apache.http.impl.auth.BasicSchemeFactory;
+import org.apache.http.impl.auth.DigestScheme;
+import org.apache.http.impl.auth.DigestSchemeFactory;
+import org.apache.http.message.BasicHeader;
+import org.apache.http.message.BasicHttpResponse;
+import org.apache.http.protocol.BasicHttpContext;
+import org.apache.http.protocol.HttpContext;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Simple tests for {@link AuthenticationStrategyImpl}.
+ */
+public class TestAuthenticationStrategy {
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testIsAuthenticationRequestedInvalidInput() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpContext context = new BasicHttpContext();
+ authStrategy.isAuthenticationRequested(null, context);
+ }
+
+ @Test
+ public void testTargetAuthRequested() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ HttpContext context = new BasicHttpContext();
+ Assert.assertTrue(authStrategy.isAuthenticationRequested(response, context));
+ }
+
+ @Test
+ public void testProxyAuthRequested() throws Exception {
+ ProxyAuthenticationStrategy authStrategy = new ProxyAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED, "UNAUTHORIZED");
+ HttpContext context = new BasicHttpContext();
+ Assert.assertTrue(authStrategy.isAuthenticationRequested(response, context));
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testGetChallengesInvalidInput() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpContext context = new BasicHttpContext();
+ authStrategy.getChallenges(null, context);
+ }
+
+ @Test
+ public void testGetChallenges() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpContext context = new BasicHttpContext();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ Header h1 = new BasicHeader(AUTH.WWW_AUTH, " Basic realm=\"test\"");
+ Header h2 = new BasicHeader(AUTH.WWW_AUTH, "\t\tDigest realm=\"realm1\", nonce=\"1234\"");
+ Header h3 = new BasicHeader(AUTH.WWW_AUTH, "WhatEver realm=\"realm1\", stuff=\"1234\"");
+ response.addHeader(h1);
+ response.addHeader(h2);
+ response.addHeader(h3);
+
+ Map<String, Header> challenges = authStrategy.getChallenges(response, context);
+
+ Assert.assertNotNull(challenges);
+ Assert.assertEquals(3, challenges.size());
+ Assert.assertSame(h1, challenges.get("basic"));
+ Assert.assertSame(h2, challenges.get("digest"));
+ Assert.assertSame(h3, challenges.get("whatever"));
+ }
+
+ @Test
+ public void testSelectInvalidInput() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ Map<String, Header> challenges = new HashMap<String, Header>();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ HttpHost authhost = new HttpHost("locahost", 80);
+ HttpContext context = new BasicHttpContext();
+ try {
+ authStrategy.select(null, authhost, response, context);
+ Assert.fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ }
+ try {
+ authStrategy.select(challenges, null, response, context);
+ Assert.fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ }
+ try {
+ authStrategy.select(challenges, authhost, null, context);
+ Assert.fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ }
+ try {
+ authStrategy.select(challenges, authhost, response, null);
+ Assert.fail("IllegalArgumentException expected");
+ } catch (IllegalArgumentException ex) {
+ }
+ }
+
+ @Test
+ public void testSelectNoSchemeRegistry() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ HttpHost authhost = new HttpHost("locahost", 80);
+ HttpContext context = new BasicHttpContext();
+
+ Map<String, Header> challenges = new HashMap<String, Header>();
+ challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""));
+ challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", nonce=\"1234\""));
+
+ Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
+ Assert.assertNotNull(options);
+ Assert.assertEquals(0, options.size());
+ }
+
+ @Test
+ public void testSelectNoCredentialsProvider() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ HttpHost authhost = new HttpHost("locahost", 80);
+ HttpContext context = new BasicHttpContext();
+
+ Map<String, Header> challenges = new HashMap<String, Header>();
+ challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"test\""));
+ challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm1\", nonce=\"1234\""));
+
+ AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
+ authSchemeRegistry.register("basic", new BasicSchemeFactory());
+ authSchemeRegistry.register("digest", new DigestSchemeFactory());
+ context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry);
+
+ Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
+ Assert.assertNotNull(options);
+ Assert.assertEquals(0, options.size());
+ }
+
+ @Test
+ public void testNoCredentials() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ HttpHost authhost = new HttpHost("locahost", 80);
+ HttpContext context = new BasicHttpContext();
+
+ Map<String, Header> challenges = new HashMap<String, Header>();
+ challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"realm1\""));
+ challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm2\", nonce=\"1234\""));
+
+ AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
+ authSchemeRegistry.register("basic", new BasicSchemeFactory());
+ authSchemeRegistry.register("digest", new DigestSchemeFactory());
+ context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry);
+
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ context.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider);
+
+ Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
+ Assert.assertNotNull(options);
+ Assert.assertEquals(0, options.size());
+ }
+
+ @Test
+ public void testCredentialsFound() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ HttpHost authhost = new HttpHost("somehost", 80);
+ HttpContext context = new BasicHttpContext();
+
+ Map<String, Header> challenges = new HashMap<String, Header>();
+ challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"realm1\""));
+ challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm2\", nonce=\"1234\""));
+
+ AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
+ authSchemeRegistry.register("basic", new BasicSchemeFactory());
+ authSchemeRegistry.register("digest", new DigestSchemeFactory());
+ context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry);
+
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(new AuthScope("somehost", 80, "realm2"),
+ new UsernamePasswordCredentials("user", "pwd"));
+ context.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider);
+
+ Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
+ Assert.assertNotNull(options);
+ Assert.assertEquals(1, options.size());
+ AuthOption option = options.remove();
+ Assert.assertTrue(option.getAuthScheme() instanceof DigestScheme);
+ }
+
+ @Test
+ public void testUnsupportedScheme() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ HttpHost authhost = new HttpHost("somehost", 80);
+ HttpContext context = new BasicHttpContext();
+
+ Map<String, Header> challenges = new HashMap<String, Header>();
+ challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"realm1\""));
+ challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm2\", nonce=\"1234\""));
+ challenges.put("whatever", new BasicHeader(AUTH.WWW_AUTH, "Whatever realm=\"realm3\""));
+
+ AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
+ authSchemeRegistry.register("basic", new BasicSchemeFactory());
+ authSchemeRegistry.register("digest", new DigestSchemeFactory());
+ context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry);
+
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(new AuthScope("somehost", 80),
+ new UsernamePasswordCredentials("user", "pwd"));
+ context.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider);
+
+ Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
+ Assert.assertNotNull(options);
+ Assert.assertEquals(2, options.size());
+ AuthOption option1 = options.remove();
+ Assert.assertTrue(option1.getAuthScheme() instanceof DigestScheme);
+ AuthOption option2 = options.remove();
+ Assert.assertTrue(option2.getAuthScheme() instanceof BasicScheme);
+ }
+
+ @Test
+ public void testCustomAuthPreference() throws Exception {
+ TargetAuthenticationStrategy authStrategy = new TargetAuthenticationStrategy();
+ HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, HttpStatus.SC_UNAUTHORIZED, "UNAUTHORIZED");
+ response.getParams().setParameter(AuthPNames.TARGET_AUTH_PREF,
+ Arrays.asList(new String[] {AuthPolicy.BASIC } ));
+ HttpHost authhost = new HttpHost("somehost", 80);
+ HttpContext context = new BasicHttpContext();
+
+ Map<String, Header> challenges = new HashMap<String, Header>();
+ challenges.put("basic", new BasicHeader(AUTH.WWW_AUTH, "Basic realm=\"realm1\""));
+ challenges.put("digest", new BasicHeader(AUTH.WWW_AUTH, "Digest realm=\"realm2\", nonce=\"1234\""));
+
+ AuthSchemeRegistry authSchemeRegistry = new AuthSchemeRegistry();
+ authSchemeRegistry.register("basic", new BasicSchemeFactory());
+ authSchemeRegistry.register("digest", new DigestSchemeFactory());
+ context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, authSchemeRegistry);
+
+ CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
+ credentialsProvider.setCredentials(new AuthScope("somehost", 80),
+ new UsernamePasswordCredentials("user", "pwd"));
+ context.setAttribute(ClientContext.CREDS_PROVIDER, credentialsProvider);
+
+ Queue<AuthOption> options = authStrategy.select(challenges, authhost, response, context);
+ Assert.assertNotNull(options);
+ Assert.assertEquals(1, options.size());
+ AuthOption option1 = options.remove();
+ Assert.assertTrue(option1.getAuthScheme() instanceof BasicScheme);
+ }
+
+}
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestAuthenticationStrategy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain