Return-Path: Delivered-To: apmail-jakarta-httpcomponents-commits-archive@www.apache.org Received: (qmail 92970 invoked from network); 10 Apr 2007 14:42:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 10 Apr 2007 14:42:07 -0000 Received: (qmail 66933 invoked by uid 500); 10 Apr 2007 14:42:14 -0000 Delivered-To: apmail-jakarta-httpcomponents-commits-archive@jakarta.apache.org Received: (qmail 66921 invoked by uid 500); 10 Apr 2007 14:42:13 -0000 Mailing-List: contact httpcomponents-commits-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: httpcomponents-dev@jakarta.apache.org Delivered-To: mailing list httpcomponents-commits@jakarta.apache.org Received: (qmail 66908 invoked by uid 99); 10 Apr 2007 14:42:13 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Apr 2007 07:42:13 -0700 X-ASF-Spam-Status: No, hits=-99.5 required=10.0 tests=ALL_TRUSTED,NO_REAL_NAME X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO eris.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 10 Apr 2007 07:42:06 -0700 Received: by eris.apache.org (Postfix, from userid 65534) id C73891A9838; Tue, 10 Apr 2007 07:41:45 -0700 (PDT) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r527146 - in /jakarta/httpcomponents/httpclient/trunk/src: java/org/apache/http/auth/ java/org/apache/http/impl/auth/ test/org/apache/http/impl/auth/ Date: Tue, 10 Apr 2007 14:41:45 -0000 To: httpcomponents-commits@jakarta.apache.org From: olegk@apache.org X-Mailer: svnmailer-1.1.0 Message-Id: <20070410144145.C73891A9838@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: olegk Date: Tue Apr 10 07:41:44 2007 New Revision: 527146 URL: http://svn.apache.org/viewvc?view=rev&rev=527146 Log: Ported UsernamePasswordCredentials, BasicScheme and related test cases from Commons HttpClient Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/auth/UsernamePasswordCredentials.java - copied, changed from r526924, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestBasicAuth.java - copied, changed from r526922, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/auth/RFC2617Scheme.java jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestAllAuthImpl.java jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestRFC2617Scheme.java Copied: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/auth/UsernamePasswordCredentials.java (from r526924, jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java) URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/auth/UsernamePasswordCredentials.java?view=diff&rev=527146&p1=jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java&r1=526924&p2=jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/auth/UsernamePasswordCredentials.java&r2=527146 ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java (original) +++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/auth/UsernamePasswordCredentials.java Tue Apr 10 07:41:44 2007 @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/java/org/apache/commons/httpclient/UsernamePasswordCredentials.java,v 1.14 2004/04/18 23:51:35 jsdever Exp $ + * $HeadURL$ * $Revision$ * $Date$ * @@ -28,9 +28,9 @@ * */ -package org.apache.commons.httpclient; +package org.apache.http.auth; -import org.apache.commons.httpclient.util.LangUtils; +import org.apache.http.util.LangUtils; /** *

Username and password {@link Credentials}.

@@ -48,16 +48,6 @@ // ----------------------------------------------------------- Constructors /** - * Default constructor. - * - * @deprecated Do not use. Null user name no longer allowed - */ - public UsernamePasswordCredentials() { - super(); - } - - - /** * The constructor with the username and password combined string argument. * * @param usernamePassword the username:password formed string @@ -111,22 +101,6 @@ /** - * User name property setter. User name may not be null. - * - * @param userName - * @see #getUserName() - * - * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable - */ - public void setUserName(String userName) { - if (userName == null) { - throw new IllegalArgumentException("Username may not be null"); - } - this.userName = userName; - } - - - /** * User name property getter. * * @return the userName @@ -138,19 +112,6 @@ /** - * Password property setter. - * - * @param password - * @see #getPassword() - * - * @deprecated Do not use. The UsernamePasswordCredentials objects should be immutable - */ - public void setPassword(String password) { - this.password = password; - } - - - /** * Password property getter. * * @return the password @@ -161,6 +122,11 @@ } + public String toText() { + return toString(); + } + + /** * Get this object string. * Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/auth/RFC2617Scheme.java URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/auth/RFC2617Scheme.java?view=diff&rev=527146&r1=527145&r2=527146 ============================================================================== --- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/auth/RFC2617Scheme.java (original) +++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/impl/auth/RFC2617Scheme.java Tue Apr 10 07:41:44 2007 @@ -84,8 +84,10 @@ if (header == null) { throw new IllegalArgumentException("Header may not be null"); } - if (!header.getName().equalsIgnoreCase(HTTPAuth.WWW_AUTH)) { - throw new MalformedChallengeException("Unexpected header name: " + header.getName()); + String authheader = header.getName(); + if (!authheader.equalsIgnoreCase(HTTPAuth.WWW_AUTH) + && !authheader.equalsIgnoreCase(HTTPAuth.PROXY_AUTH)) { + throw new MalformedChallengeException("Unexpected header name: " + authheader); } CharArrayBuffer buffer; int pos; @@ -114,6 +116,10 @@ throw new MalformedChallengeException("Invalid scheme identifier: " + s); } HeaderElement[] elements = BasicHeaderElement.parseAll(buffer, pos, buffer.length()); + if (elements.length == 0) { + throw new MalformedChallengeException("Authentication challenge is empty"); + } + this.params = new HashMap(elements.length); for (int i = 0; i < elements.length; i++) { HeaderElement element = elements[i]; Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestAllAuthImpl.java URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestAllAuthImpl.java?view=diff&rev=527146&r1=527145&r2=527146 ============================================================================== --- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestAllAuthImpl.java (original) +++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestAllAuthImpl.java Tue Apr 10 07:41:44 2007 @@ -44,6 +44,7 @@ TestSuite suite = new TestSuite(); suite.addTest(TestRFC2617Scheme.suite()); + suite.addTest(TestBasicAuth.suite()); return suite; } Copied: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestBasicAuth.java (from r526922, jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java) URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestBasicAuth.java?view=diff&rev=527146&p1=jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java&r1=526922&p2=jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestBasicAuth.java&r2=527146 ============================================================================== --- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java (original) +++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestBasicAuth.java Tue Apr 10 07:41:44 2007 @@ -1,5 +1,5 @@ /* - * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java,v 1.9 2004/11/20 17:56:40 olegk Exp $ + * $HeadURL$ * $Revision$ * $Date$ * ==================================================================== @@ -27,31 +27,25 @@ * */ -package org.apache.commons.httpclient.auth; +package org.apache.http.impl.auth; import java.io.IOException; import junit.framework.Test; +import junit.framework.TestCase; import junit.framework.TestSuite; import org.apache.commons.codec.binary.Base64; -import org.apache.commons.httpclient.EchoService; -import org.apache.commons.httpclient.FeedbackService; -import org.apache.commons.httpclient.Header; -import org.apache.commons.httpclient.HttpClientTestBase; -import org.apache.commons.httpclient.HttpState; -import org.apache.commons.httpclient.HttpStatus; -import org.apache.commons.httpclient.ProxyTestDecorator; -import org.apache.commons.httpclient.UsernamePasswordCredentials; -import org.apache.commons.httpclient.methods.GetMethod; -import org.apache.commons.httpclient.methods.HeadMethod; -import org.apache.commons.httpclient.methods.PostMethod; -import org.apache.commons.httpclient.methods.PutMethod; -import org.apache.commons.httpclient.methods.StringRequestEntity; -import org.apache.commons.httpclient.server.AuthRequestHandler; -import org.apache.commons.httpclient.server.HttpRequestHandlerChain; -import org.apache.commons.httpclient.server.HttpServiceHandler; -import org.apache.commons.httpclient.util.EncodingUtil; +import org.apache.http.Header; +import org.apache.http.HttpMessage; +import org.apache.http.HttpVersion; +import org.apache.http.auth.AuthScheme; +import org.apache.http.auth.HTTPAuth; +import org.apache.http.auth.MalformedChallengeException; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.message.BasicHeader; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.util.EncodingUtils; /** * Basic authentication test cases. @@ -60,7 +54,7 @@ * * @version $Id$ */ -public class TestBasicAuth extends HttpClientTestBase { +public class TestBasicAuth extends TestCase { // ------------------------------------------------------------ Constructor public TestBasicAuth(final String testName) throws IOException { @@ -77,73 +71,15 @@ public static Test suite() { TestSuite suite = new TestSuite(TestBasicAuth.class); - ProxyTestDecorator.addTests(suite); return suite; } - public void testBasicAuthenticationWithNoCreds() throws IOException { - - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - this.server.setRequestHandler(handlerchain); - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } finally { - httpget.releaseConnection(); - } - } - - public void testBasicAuthenticationWithNoCredsRetry() throws IOException { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } finally { - httpget.releaseConnection(); - } - // now try with credentials - httpget = new GetMethod("/test/"); - try { - this.client.getState().setCredentials(AuthScope.ANY, creds); - this.client.executeMethod(httpget); - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); - } finally { - httpget.releaseConnection(); - } - } - public void testBasicAuthenticationWithNoRealm() { String challenge = "Basic"; + Header header = new BasicHeader(HTTPAuth.WWW_AUTH, challenge); try { AuthScheme authscheme = new BasicScheme(); - authscheme.processChallenge(challenge); + authscheme.processChallenge(header); fail("Should have thrown MalformedChallengeException"); } catch(MalformedChallengeException e) { // expected @@ -158,430 +94,28 @@ } UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("dh", buffer.toString()); - assertEquals("Basic ZGg65C32Lfw=", - BasicScheme.authenticate(credentials, "ISO-8859-1")); + Header header = BasicScheme.authenticate(credentials, "ISO-8859-1"); + assertEquals("Basic ZGg65C32Lfw=", header.getValue()); } - public void testBasicAuthenticationWithDefaultCreds() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - state.setCredentials(AuthScope.ANY, creds); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); - Header auth = httpget.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } - public void testBasicAuthentication() throws Exception { UsernamePasswordCredentials creds = new UsernamePasswordCredentials("testuser", "testpass"); - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - AuthScope authscope = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - state.setCredentials(authscope, creds); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); - Header auth = httpget.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } - - public void testBasicAuthenticationWithInvalidCredentials() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); + Header challenge = new BasicHeader(HTTPAuth.WWW_AUTH, "Basic realm=\"test\""); - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - AuthScope authscope = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - state.setCredentials(authscope, new UsernamePasswordCredentials("test", "stuff")); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); + BasicScheme authscheme = new BasicScheme(); + authscheme.processChallenge(challenge); - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } - - public void testBasicAuthenticationWithMutlipleRealms1() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); + HttpMessage message = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK"); + Header authResponse = authscheme.authenticate(creds, message); - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - AuthScope realm1 = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - AuthScope realm2 = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test2"); - state.setCredentials(realm1, new UsernamePasswordCredentials("testuser","testpass")); - state.setCredentials(realm2, new UsernamePasswordCredentials("testuser2","testpass2")); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); - Header auth = httpget.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } - - public void testBasicAuthenticationWithMutlipleRealms2() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser2", "testpass2"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds, "test2")); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - AuthScope realm1 = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - AuthScope realm2 = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test2"); - state.setCredentials(realm1, new UsernamePasswordCredentials("testuser","testpass")); - state.setCredentials(realm2, new UsernamePasswordCredentials("testuser2","testpass2")); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test2/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); - Header auth = httpget.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser2:testpass2"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test2", authstate.getRealm()); + String expected = "Basic " + EncodingUtils.getAsciiString( + Base64.encodeBase64(EncodingUtils.getAsciiBytes("testuser:testpass"))); + assertEquals(expected, authResponse.getValue()); + assertEquals("test", authscheme.getRealm()); + assertTrue(authscheme.isComplete()); + assertFalse(authscheme.isConnectionBased()); } - public void testPreemptiveAuthorizationTrueWithCreds() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - state.setCredentials(AuthScope.ANY, creds); - this.client.setState(state); - this.client.getParams().setAuthenticationPreemptive(true); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); - Header auth = httpget.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertNull(authstate.getRealm()); - assertTrue(authstate.isPreemptive()); - } - - public void testPreemptiveAuthorizationTrueWithoutCreds() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - this.client.setState(state); - this.client.getParams().setAuthenticationPreemptive(true); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode()); - Header auth = httpget.getRequestHeader("Authorization"); - assertNull(auth); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertNotNull(authstate.getRealm()); - assertTrue(authstate.isPreemptive()); - } - - public void testCustomAuthorizationHeader() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - String authResponse = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - httpget.addRequestHeader(new Header("Authorization", authResponse)); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); - } - - public void testHeadBasicAuthentication() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - AuthScope authscope = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - state.setCredentials(authscope, creds); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); - - HeadMethod head = new HeadMethod("/test/"); - try { - this.client.executeMethod(head); - } finally { - head.releaseConnection(); - } - assertNotNull(head.getStatusLine()); - assertEquals(HttpStatus.SC_OK, head.getStatusLine().getStatusCode()); - Header auth = head.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = head.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } - - public void testPostBasicAuthentication() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new EchoService())); - - HttpState state = new HttpState(); - AuthScope authscope = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - state.setCredentials(authscope, creds); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); - - PostMethod post = new PostMethod("/test/"); - post.setRequestEntity(new StringRequestEntity("Test body", null, null)); - try { - this.client.executeMethod(post); - assertEquals("Test body", post.getResponseBodyAsString()); - } finally { - post.releaseConnection(); - } - assertNotNull(post.getStatusLine()); - assertEquals(HttpStatus.SC_OK, post.getStatusLine().getStatusCode()); - Header auth = post.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = post.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } - - public void testPutBasicAuthentication() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new EchoService())); - - HttpState state = new HttpState(); - AuthScope authscope = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - state.setCredentials(authscope, creds); - this.client.setState(state); - - this.server.setRequestHandler(handlerchain); - - PutMethod put = new PutMethod("/test/"); - put.setRequestEntity(new StringRequestEntity("Test body", null, null)); - try { - this.client.executeMethod(put); - assertEquals("Test body", put.getResponseBodyAsString()); - } finally { - put.releaseConnection(); - } - assertNotNull(put.getStatusLine()); - assertEquals(HttpStatus.SC_OK, put.getStatusLine().getStatusCode()); - Header auth = put.getRequestHeader("Authorization"); - assertNotNull(auth); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); - assertEquals(expected, auth.getValue()); - AuthState authstate = put.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - } - - public void testPreemptiveAuthorizationFailure() throws Exception { - UsernamePasswordCredentials creds = - new UsernamePasswordCredentials("testuser", "testpass"); - UsernamePasswordCredentials wrongcreds = - new UsernamePasswordCredentials("testuser", "garbage"); - - HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); - handlerchain.appendHandler(new AuthRequestHandler(creds)); - handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); - - HttpState state = new HttpState(); - state.setCredentials(AuthScope.ANY, wrongcreds); - this.client.setState(state); - this.client.getParams().setAuthenticationPreemptive(true); - - this.server.setRequestHandler(handlerchain); - - GetMethod httpget = new GetMethod("/test/"); - try { - this.client.executeMethod(httpget); - } finally { - httpget.releaseConnection(); - } - assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode()); - AuthState authstate = httpget.getHostAuthState(); - assertNotNull(authstate.getAuthScheme()); - assertTrue(authstate.getAuthScheme() instanceof BasicScheme); - assertEquals("test", authstate.getRealm()); - assertTrue(authstate.isPreemptive()); - } - } Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestRFC2617Scheme.java URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestRFC2617Scheme.java?view=diff&rev=527146&r1=527145&r2=527146 ============================================================================== --- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestRFC2617Scheme.java (original) +++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/impl/auth/TestRFC2617Scheme.java Tue Apr 10 07:41:44 2007 @@ -37,6 +37,8 @@ import org.apache.http.auth.HTTPAuth; import org.apache.http.auth.MalformedChallengeException; import org.apache.http.message.BasicHeader; +import org.apache.http.message.BufferedHeader; +import org.apache.http.util.CharArrayBuffer; import junit.framework.Test; import junit.framework.TestCase; @@ -97,7 +99,9 @@ public void testProcessChallengeWithLotsOfBlanks() throws Exception { TestAuthScheme authscheme = new TestAuthScheme(); - Header header = new BasicHeader(HTTPAuth.WWW_AUTH, " Test realm=\"realm1\""); + CharArrayBuffer buffer = new CharArrayBuffer(32); + buffer.append(" WWW-Authenticate: Test realm=\"realm1\""); + Header header = new BufferedHeader(buffer); authscheme.processChallenge(header); @@ -108,6 +112,17 @@ public void testInvalidHeader() throws Exception { TestAuthScheme authscheme = new TestAuthScheme(); Header header = new BasicHeader("whatever", "Test realm=\"realm1\""); + try { + authscheme.processChallenge(header); + fail("MalformedChallengeException should have been thrown"); + } catch (MalformedChallengeException ex) { + //expected + } + } + + public void testEmptyHeader() throws Exception { + TestAuthScheme authscheme = new TestAuthScheme(); + Header header = new BasicHeader(HTTPAuth.WWW_AUTH, "Test "); try { authscheme.processChallenge(header); fail("MalformedChallengeException should have been thrown");