Index: test/org/apache/commons/httpclient/HttpClientTestBase.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java,v retrieving revision 1.1 diff -u -r1.1 HttpClientTestBase.java --- test/org/apache/commons/httpclient/HttpClientTestBase.java 25 Feb 2004 22:33:58 -0000 1.1 +++ test/org/apache/commons/httpclient/HttpClientTestBase.java 27 Feb 2004 17:04:13 -0000 @@ -37,10 +37,7 @@ import junit.framework.TestSuite; import org.apache.commons.httpclient.protocol.Protocol; -import org.apache.commons.httpclient.server.RequestLine; -import org.apache.commons.httpclient.server.ResponseWriter; import org.apache.commons.httpclient.server.SimpleHttpServer; -import org.apache.commons.httpclient.server.SimpleHttpServerConnection; /** * Base class for test cases using @@ -92,35 +89,5 @@ this.client = null; this.server.destroy(); this.server = null; - } - - protected static void respondNotImplemented(SimpleHttpServerConnection conn) - throws IOException - { - RequestLine requestLine = conn.getRequestLine(); - ResponseWriter out = conn.getWriter(); - String content = requestLine.getMethod() + " method not supported"; - out.println("HTTP/1.1 501 Not implemented"); - out.println("Content-Type: text/plain"); - out.println("Content-Length: " + content.length()); - out.println("Connection: close"); - out.println(); - out.println(content); - out.flush(); - } - - protected static void respondNotFound(SimpleHttpServerConnection conn) - throws IOException - { - RequestLine requestLine = conn.getRequestLine(); - ResponseWriter out = conn.getWriter(); - String content = requestLine.getUri() + " not found"; - out.println("HTTP/1.1 404 Not found"); - out.println("Content-Type: text/plain"); - out.println("Content-Length: " + content.length()); - out.println("Connection: close"); - out.println(); - out.println(content); - out.flush(); } } Index: test/org/apache/commons/httpclient/TestAuthenticator.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v retrieving revision 1.40 diff -u -r1.40 TestAuthenticator.java --- test/org/apache/commons/httpclient/TestAuthenticator.java 22 Feb 2004 18:08:49 -0000 1.40 +++ test/org/apache/commons/httpclient/TestAuthenticator.java 27 Feb 2004 17:04:13 -0000 @@ -35,16 +35,13 @@ import junit.framework.Test; import junit.framework.TestSuite; -import org.apache.commons.codec.binary.Base64; import org.apache.commons.httpclient.auth.AuthChallengeParser; import org.apache.commons.httpclient.auth.AuthScheme; import org.apache.commons.httpclient.auth.AuthenticationException; -import org.apache.commons.httpclient.auth.BasicScheme; import org.apache.commons.httpclient.auth.CredentialsNotAvailableException; import org.apache.commons.httpclient.auth.DigestScheme; import org.apache.commons.httpclient.auth.MalformedChallengeException; import org.apache.commons.httpclient.auth.NTLMScheme; -import org.apache.commons.httpclient.util.EncodingUtil; /** * Unit tests for {@link Authenticator}. @@ -138,7 +135,7 @@ ) throws AuthenticationException { return doAuthenticate(authscheme, method, conn, state, true); } - // ---------------------------------- Test Methods for BasicScheme Authentication + public void testCredentialConstructors() { try { @@ -166,153 +163,9 @@ assertNotNull(creds.getHost()); } + // ---------------------------------- Test Methods for BasicScheme Authentication - public void testBasicAuthenticationWithNoCreds() { - String challenge = "Basic realm=\"realm1\""; - HttpState state = new HttpState(); - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge)); - try { - AuthScheme authscheme = new BasicScheme(); - authscheme.processChallenge(challenge); - authenticate(authscheme, method, null, state); - fail("Should have thrown HttpException"); - } catch(HttpException e) { - // expected - } - } - - public void testBasicAuthenticationWithNoRealm() { - String challenge = "Basic"; - HttpState state = new HttpState(); - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge)); - try { - AuthScheme authscheme = new BasicScheme(); - authscheme.processChallenge(challenge); - authenticate(authscheme, method, null, state); - fail("Should have thrown HttpException"); - } catch(HttpException e) { - // expected - } - } - - public void testBasicAuthenticationWithNoRealm2() { - String challenge = "Basic "; - HttpState state = new HttpState(); - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge)); - try { - AuthScheme authscheme = new BasicScheme(); - authscheme.processChallenge(challenge); - authenticate(authscheme, method, null, state); - fail("Should have thrown HttpException"); - } catch(HttpException e) { - // expected - } - } - - public void testBasicAuthenticationWithNullHttpState() throws Exception { - String challenge = "Basic realm=\"realm1\""; - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge)); - try { - AuthScheme authscheme = new BasicScheme(); - authscheme.processChallenge(challenge); - authenticate(authscheme, method, null, null); - fail("Should have thrown IllegalArgumentException"); - } catch(IllegalArgumentException e) { - // expected - } - } - - public void testBasicAuthenticationCaseInsensitivity() throws Exception { - String challenge = "bAsIc ReAlM=\"realm1\""; - HttpState state = new HttpState(); - state.setCredentials(null, null, new UsernamePasswordCredentials("username","password")); - HttpMethod method = new SimpleHttpMethod(new Header("WwW-AuThEnTiCaTe", challenge)); - AuthScheme authscheme = new BasicScheme(); - authscheme.processChallenge(challenge); - assertTrue(authenticate(authscheme, method, null, state)); - assertTrue(null != method.getRequestHeader("Authorization")); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password"))); - assertEquals(expected,method.getRequestHeader("Authorization").getValue()); - } - - - public void testBasicAuthenticationWithDefaultCreds() throws Exception { - HttpState state = new HttpState(); - state.setCredentials(null, null, new UsernamePasswordCredentials("username","password")); - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate","Basic realm=\"realm1\"")); - assertTrue(authenticate(new BasicScheme(), method, null, state)); - assertTrue(null != method.getRequestHeader("Authorization")); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password"))); - assertEquals(expected,method.getRequestHeader("Authorization").getValue()); - } - - public void testBasicAuthentication() throws Exception { - String challenge = "Basic realm=\"realm\""; - HttpState state = new HttpState(); - state.setCredentials("realm", null, new UsernamePasswordCredentials("username","password")); - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge)); - AuthScheme authscheme = new BasicScheme(); - authscheme.processChallenge(challenge); - assertTrue(authenticate(authscheme, method, null, state)); - assertTrue(null != method.getRequestHeader("Authorization")); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password"))); - assertEquals(expected,method.getRequestHeader("Authorization").getValue()); - } - - public void testBasicAuthenticationWith88591Chars() throws Exception { - int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc }; - StringBuffer buffer = new StringBuffer(); - for (int i = 0; i < germanChars.length; i++) { - buffer.append((char)germanChars[i]); - } - - UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("dh", buffer.toString()); - assertEquals("Basic ZGg65C32Lfw=", - BasicScheme.authenticate(credentials, "ISO-8859-1")); - } - - public void testBasicAuthenticationWithMutlipleRealms() throws Exception { - String challenge1 = "Basic realm=\"realm1\""; - String challenge2 = "Basic realm=\"realm2\""; - HttpState state = new HttpState(); - state.setCredentials("realm1", null, new UsernamePasswordCredentials("username","password")); - state.setCredentials("realm2", null, new UsernamePasswordCredentials("uname2","password2")); - AuthScheme authscheme1 = new BasicScheme(); - authscheme1.processChallenge(challenge1); - AuthScheme authscheme2 = new BasicScheme(); - authscheme2.processChallenge(challenge2); - { - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate",challenge1)); - assertTrue(authenticate(authscheme1, method, null, state)); - assertTrue(null != method.getRequestHeader("Authorization")); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password"))); - assertEquals(expected,method.getRequestHeader("Authorization").getValue()); - } - { - HttpMethod method = new SimpleHttpMethod(new Header("WWW-Authenticate", challenge2)); - assertTrue(authenticate(authscheme2, method, null, state)); - assertTrue(null != method.getRequestHeader("Authorization")); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("uname2:password2"))); - assertEquals(expected,method.getRequestHeader("Authorization").getValue()); - } - } - - public void testPreemptiveAuthorizationTrueWithCreds() throws Exception { - HttpState state = new HttpState(); - HttpMethod method = new SimpleHttpMethod(); - state.setCredentials(null, null, new UsernamePasswordCredentials("username","password")); - - assertTrue(authenticate(new BasicScheme(), method, null, state)); - assertTrue(null != method.getRequestHeader("Authorization")); - String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("username:password"))); - assertEquals(expected, method.getRequestHeader("Authorization").getValue()); - } + // Moved to a separate test case based on a new testing framework // --------------------------------- Test Methods for DigestScheme Authentication Index: test/org/apache/commons/httpclient/TestBadContentLength.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBadContentLength.java,v retrieving revision 1.5 diff -u -r1.5 TestBadContentLength.java --- test/org/apache/commons/httpclient/TestBadContentLength.java 22 Feb 2004 18:08:49 -0000 1.5 +++ test/org/apache/commons/httpclient/TestBadContentLength.java 27 Feb 2004 17:04:13 -0000 @@ -41,6 +41,7 @@ import org.apache.commons.httpclient.server.ResponseWriter; import org.apache.commons.httpclient.server.SimpleHttpServer; import org.apache.commons.httpclient.server.SimpleHttpServerConnection; +import org.apache.commons.httpclient.server.SimpleRequest; /** * Tests HttpClient's behaviour when receiving more response data than expected. @@ -166,8 +167,11 @@ private class MyHttpRequestHandler implements HttpRequestHandler { private int requestNo = 0; - public boolean processRequest(SimpleHttpServerConnection conn) throws IOException { - RequestLine requestLine = conn.getRequestLine(); + public boolean processRequest( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException + { + RequestLine requestLine = request.getRequestLine(); ResponseWriter out = conn.getWriter(); if ("GET".equals(requestLine.getMethod()) && "/".equals(requestLine.getUri())) { Index: test/org/apache/commons/httpclient/TestNoHost.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v retrieving revision 1.31 diff -u -r1.31 TestNoHost.java --- test/org/apache/commons/httpclient/TestNoHost.java 22 Feb 2004 18:08:49 -0000 1.31 +++ test/org/apache/commons/httpclient/TestNoHost.java 27 Feb 2004 17:04:13 -0000 @@ -30,7 +30,11 @@ package org.apache.commons.httpclient; -import junit.framework.*; +import junit.framework.Test; +import junit.framework.TestCase; +import junit.framework.TestSuite; + +import org.apache.commons.httpclient.auth.TestBasicAuth; /** * Tests that don't require any external host. @@ -58,6 +62,7 @@ suite.addTest(TestHeaderElement.suite()); suite.addTest(TestChallengeParser.suite()); suite.addTest(TestAuthenticator.suite()); + suite.addTest(TestBasicAuth.suite()); suite.addTest(TestHttpUrlMethod.suite()); suite.addTest(TestURI.suite()); suite.addTest(TestURIUtil.suite()); Index: test/org/apache/commons/httpclient/auth/TestBasicAuth.java =================================================================== RCS file: test/org/apache/commons/httpclient/auth/TestBasicAuth.java diff -N test/org/apache/commons/httpclient/auth/TestBasicAuth.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/org/apache/commons/httpclient/auth/TestBasicAuth.java 27 Feb 2004 17:04:13 -0000 @@ -0,0 +1,401 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed 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 + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient.auth; + +import java.io.IOException; + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.apache.commons.codec.binary.Base64; +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.UsernamePasswordCredentials; +import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.httpclient.server.HttpService; +import org.apache.commons.httpclient.server.RequestLine; +import org.apache.commons.httpclient.server.SimpleRequest; +import org.apache.commons.httpclient.server.SimpleResponse; +import org.apache.commons.httpclient.util.EncodingUtil; + +/** + * Basic authentication test cases. + * + * @author Oleg Kalnichevski + * + * @version $Id$ + */ +public class TestBasicAuth extends HttpClientTestBase { + + // ------------------------------------------------------------ Constructor + public TestBasicAuth(String testName) { + super(testName); + } + + // ------------------------------------------------------------------- Main + public static void main(String args[]) { + String[] testCaseName = { TestBasicAuth.class.getName() }; + junit.textui.TestRunner.main(testCaseName); + } + + // ------------------------------------------------------- TestCase Methods + + public static Test suite() { + return new TestSuite(TestBasicAuth.class); + } + + private class BasicAuthService implements HttpService { + + public BasicAuthService() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + Header challenge = new Header("WWW-Authenticate", "Basic realm=\"test\""); + RequestLine requestLine = request.getRequestLine(); + Header auth = request.getFirstHeader("Authorization"); + if (auth == null) { + response.setStatusLine("HTTP/1.1 401 Unauthorized"); + response.addHeader(challenge); + response.setBodyString("Authorization required"); + return true; + } + boolean pass = false; + String s = auth.getValue(); + int i = s.indexOf(" "); + if (i != -1) { + String authtype = s.substring(0, i); + if ("BASIC".equalsIgnoreCase(authtype)) { + String creds = s.substring(i + 1, s.length()); + creds = EncodingUtil.getAsciiString( + Base64.decodeBase64( + EncodingUtil.getAsciiBytes(creds))); + if (creds.equals("test:test")) { + pass = true; + } + } + } + if (!pass) { + response.setStatusLine("HTTP/1.1 403 Forbidden"); + response.addHeader(challenge); + response.setBodyString("Access forbidden"); + return true; + } + response.setStatusLine("HTTP/1.1 200 OK"); + response.setBodyString("Authorization successful"); + return true; + } + } + + private class BasicAuthService2 implements HttpService { + + public BasicAuthService2() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + Header challenge = new Header("WWW-Authenticate", "Basic realm=\"test2\""); + RequestLine requestLine = request.getRequestLine(); + Header auth = request.getFirstHeader("Authorization"); + if (auth == null) { + response.setStatusLine("HTTP/1.1 401 Unauthorized"); + response.addHeader(challenge); + response.setBodyString("Authorization required"); + return true; + } + boolean pass = false; + String s = auth.getValue(); + int i = s.indexOf(" "); + if (i != -1) { + String authtype = s.substring(0, i); + if ("BASIC".equalsIgnoreCase(authtype)) { + String creds = s.substring(i + 1, s.length()); + creds = EncodingUtil.getAsciiString( + Base64.decodeBase64( + EncodingUtil.getAsciiBytes(creds))); + if (creds.equals("test2:test2")) { + pass = true; + } + } + } + if (!pass) { + response.setStatusLine("HTTP/1.1 403 Forbidden"); + response.addHeader(challenge); + response.setBodyString("Access forbidden"); + return true; + } + response.setStatusLine("HTTP/1.1 200 OK"); + response.setBodyString("Authorization successful"); + return true; + } + } + + private class BasicAuthService3 implements HttpService { + + public BasicAuthService3() { + super(); + } + + public boolean process(final SimpleRequest request, final SimpleResponse response) + throws IOException + { + Header challenge = new Header("WwW-AuThEnTiCaTe", "bAsIc ReAlM=\"test\""); + RequestLine requestLine = request.getRequestLine(); + Header auth = request.getFirstHeader("Authorization"); + if (auth == null) { + response.setStatusLine("HTTP/1.1 401 Unauthorized"); + response.addHeader(challenge); + response.setBodyString("Authorization required"); + return true; + } + boolean pass = false; + String s = auth.getValue(); + int i = s.indexOf(" "); + if (i != -1) { + String authtype = s.substring(0, i); + if ("BASIC".equalsIgnoreCase(authtype)) { + String creds = s.substring(i + 1, s.length()); + creds = EncodingUtil.getAsciiString( + Base64.decodeBase64( + EncodingUtil.getAsciiBytes(creds))); + if (creds.equals("test:test")) { + pass = true; + } + } + } + if (!pass) { + response.setStatusLine("HTTP/1.1 403 Forbidden"); + response.addHeader(challenge); + response.setBodyString("Access forbidden"); + return true; + } + response.setStatusLine("HTTP/1.1 200 OK"); + response.setBodyString("Authorization successful"); + return true; + } + } + + public void testBasicAuthenticationWithNoCreds() throws IOException { + this.server.setHttpService(new BasicAuthService()); + GetMethod httpget = new GetMethod("/test/"); + try { + this.client.executeMethod(httpget); + assertNotNull(httpget.getStatusLine()); + assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode()); + } finally { + httpget.releaseConnection(); + } + } + + public void testBasicAuthenticationWithNoRealm() { + String challenge = "Basic"; + try { + AuthScheme authscheme = new BasicScheme(); + authscheme.processChallenge(challenge); + fail("Should have thrown MalformedChallengeException"); + } catch(MalformedChallengeException e) { + // expected + } + } + + public void testBasicAuthenticationWith88591Chars() throws Exception { + int[] germanChars = { 0xE4, 0x2D, 0xF6, 0x2D, 0xFc }; + StringBuffer buffer = new StringBuffer(); + for (int i = 0; i < germanChars.length; i++) { + buffer.append((char)germanChars[i]); + } + + UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("dh", buffer.toString()); + assertEquals("Basic ZGg65C32Lfw=", + BasicScheme.authenticate(credentials, "ISO-8859-1")); + } + + public void testBasicAuthenticationWithDefaultCreds() throws Exception { + HttpState state = new HttpState(); + state.setCredentials(new HttpAuthRealm(), new UsernamePasswordCredentials("test", "test")); + this.client.setState(state); + this.server.setHttpService(new BasicAuthService()); + 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("test:test"))); + assertEquals(expected, auth.getValue()); + } + + public void testBasicAuthentication() throws Exception { + HttpState state = new HttpState(); + HttpAuthRealm realm = new HttpAuthRealm( + this.server.getLocalAddress(), + this.server.getLocalPort(), + "test"); + state.setCredentials(realm, new UsernamePasswordCredentials("test", "test")); + this.client.setState(state); + this.server.setHttpService(new BasicAuthService()); + 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("test:test"))); + assertEquals(expected, auth.getValue()); + } + + public void testBasicAuthenticationWithInvalidCredentials() throws Exception { + HttpState state = new HttpState(); + HttpAuthRealm realm = new HttpAuthRealm( + this.server.getLocalAddress(), + this.server.getLocalPort(), + "test"); + state.setCredentials(realm, new UsernamePasswordCredentials("test", "stuff")); + this.client.setState(state); + this.server.setHttpService(new BasicAuthService()); + GetMethod httpget = new GetMethod("/test/"); + try { + this.client.executeMethod(httpget); + } finally { + httpget.releaseConnection(); + } + assertNotNull(httpget.getStatusLine()); + assertEquals(HttpStatus.SC_FORBIDDEN, httpget.getStatusLine().getStatusCode()); + } + + public void testBasicAuthenticationWithMutlipleRealms() throws Exception { + HttpState state = new HttpState(); + HttpAuthRealm realm1 = new HttpAuthRealm( + this.server.getLocalAddress(), + this.server.getLocalPort(), + "test"); + HttpAuthRealm realm2 = new HttpAuthRealm( + this.server.getLocalAddress(), + this.server.getLocalPort(), + "test2"); + state.setCredentials(realm1, new UsernamePasswordCredentials("test","test")); + state.setCredentials(realm2, new UsernamePasswordCredentials("test2","test2")); + this.client.setState(state); + { + this.server.setHttpService(new BasicAuthService()); + 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("test:test"))); + assertEquals(expected, auth.getValue()); + } + { + this.server.setHttpService(new BasicAuthService2()); + 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("test2:test2"))); + assertEquals(expected, auth.getValue()); + } + } + + public void testPreemptiveAuthorizationTrueWithCreds() throws Exception { + HttpState state = new HttpState(); + state.setCredentials(new HttpAuthRealm(), new UsernamePasswordCredentials("test", "test")); + this.client.setState(state); + this.client.getParams().setAuthenticationPreemptive(true); + this.server.setHttpService(new BasicAuthService()); + 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("test:test"))); + assertEquals(expected, auth.getValue()); + } + + public void testBasicAuthenticationCaseInsensitivity() throws Exception { + HttpState state = new HttpState(); + HttpAuthRealm realm = new HttpAuthRealm( + this.server.getLocalAddress(), + this.server.getLocalPort(), + "test"); + state.setCredentials(realm, new UsernamePasswordCredentials("test", "test")); + this.client.setState(state); + this.server.setHttpService(new BasicAuthService3()); + 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("test:test"))); + assertEquals(expected, auth.getValue()); + } +} Index: test/org/apache/commons/httpclient/server/ErrorResponse.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ErrorResponse.java,v retrieving revision 1.3 diff -u -r1.3 ErrorResponse.java --- test/org/apache/commons/httpclient/server/ErrorResponse.java 22 Feb 2004 18:08:52 -0000 1.3 +++ test/org/apache/commons/httpclient/server/ErrorResponse.java 27 Feb 2004 17:04:13 -0000 @@ -52,28 +52,33 @@ private final HashMap responses = new HashMap(); private ErrorResponse() { + super(); } - public void setResponse(int statusCode, GenericResponse r) { + public void setResponse(int statusCode, SimpleResponse response) { Integer code = new Integer(statusCode); - responses.put(code, r); + responses.put(code, response); } - public GenericResponse getResponse(int statusCode) { + public SimpleResponse getResponse(int statusCode) { Integer code = new Integer(statusCode); - GenericResponse r = (GenericResponse)responses.get(code); - if(r == null) { - String text = statusCode+" "+HttpStatus.getStatusText(statusCode); - r = new GenericResponse("HTTP/1.0 "+text, - "\n" - + "\n" - + ""+text+"" - + "\n" - + "

"+text+"

\n\n", - "text/html"); - - responses.put(code, r); + SimpleResponse response = (SimpleResponse)responses.get(code); + if (response == null) { + StringBuffer buffer = new StringBuffer(); + buffer.append(statusCode); + String s = HttpStatus.getStatusText(statusCode); + if (s != null) { + buffer.append(' '); + buffer.append(s); + } + response = new SimpleResponse(buffer.toString()); + response.setContentType("text/plain"); + if (s == null) { + s = "Error " + code; + } + response.setBodyString(s); + responses.put(code, response); } - return r; + return response; } } Index: test/org/apache/commons/httpclient/server/GenericResponse.java =================================================================== RCS file: test/org/apache/commons/httpclient/server/GenericResponse.java diff -N test/org/apache/commons/httpclient/server/GenericResponse.java --- test/org/apache/commons/httpclient/server/GenericResponse.java 22 Feb 2004 18:08:52 -0000 1.3 +++ /dev/null 1 Jan 1970 00:00:00 -0000 @@ -1,186 +0,0 @@ -/* - * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/GenericResponse.java,v 1.3 2004/02/22 18:08:52 olegk Exp $ - * $Revision: 1.3 $ - * $Date: 2004/02/22 18:08:52 $ - * - * ==================================================================== - * - * Copyright 1999-2004 The Apache Software Foundation - * - * Licensed 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 - * . - * - * [Additional notices, if required by prior licensing conditions] - * - */ - -package org.apache.commons.httpclient.server; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import org.apache.commons.httpclient.Header; - -/** - * A generic HTTP response. - * - * @author Christian Kohlschuetter - */ -public class GenericResponse implements HttpRequestHandler { - private ByteArrayOutputStream bos = new ByteArrayOutputStream(); - private String statusLine, contentType; - private String bodyString; - private byte[] bodyBytes; - private Header[] responseHeaders; - - public GenericResponse() throws IOException { - this("HTTP/1.0 200 OK", "text/plain"); - } - public GenericResponse(String statusLine, String contentType) { - this(statusLine, contentType, (Header[])null); - } - - public GenericResponse( - String statusLine, - String contentType, - Header[] headers) { - - this(statusLine, (String) null, contentType, headers); - } - - public GenericResponse( - String statusLine, - String bodyString, - String contentType) { - - this(statusLine, bodyString, contentType, null); - } - - public GenericResponse( - String statusLine, - String bodyString, - String contentType, - Header[] headers) { - - setStatusLine(statusLine); - setContentType(contentType); - setBodyString(bodyString); - setupBody(); - } - public GenericResponse( - String statusLine, - byte[] bodyBytes, - String contentType, - Header[] headers) { - setStatusLine(statusLine); - setContentType(contentType); - setBodyBytes(bodyBytes); - setupBody(); - } - - public String getContentType() { - return contentType; - } - public void setContentType(String string) { - this.contentType = string; - } - - public void setBodyString(String string) { - bodyString = string; - bodyBytes = null; - } - public void setBodyBytes(byte[] body) { - bodyBytes = body; - bodyString = null; - } - - public String getStatusLine() { - return statusLine; - } - - public void setStatusLine(String string) { - statusLine = string; - } - - public Header[] getResponseHeaders() { - return responseHeaders; - } - public void setResponseHeaders(Header[] headers) { - responseHeaders = headers; - } - - public void setupBody() { - try { - if (bodyString != null) { - ResponseWriter body = new ResponseWriter(bos); - - if (bodyString != null) { - body.print(bodyString); - } else if (bodyBytes != null) { - body.write(bodyBytes); - } - - body.close(); - } - } catch (IOException e) { - e.printStackTrace(System.err); - } - } - - public boolean processRequest(SimpleHttpServerConnection conn) throws IOException { - - boolean haveContentLength = false; - boolean haveContentType = false; - ResponseWriter out = conn.getWriter(); - out.println(getStatusLine()); - if (responseHeaders != null) { - for (int i = 0; i < responseHeaders.length; i++) { - Header h = responseHeaders[i]; - String name = h.getName(); - if (name.equals("Content-Type")) { - haveContentType = true; - } else if (name.equals("Content-Length")) { - haveContentLength = true; - } - - String value = h.getValue(); - out.println( - ((null == name ? "" : name) - + ": " - + (null == value ? "" : value))); - } - } - if (!haveContentLength) { - out.print("Content-Length: "); - out.println(bos.size()); - } - if (!haveContentType && getContentType() != null) { - out.print("Content-Type: "); - out.print(getContentType()); - if (out.getEncoding() != null) { - out.print("; charset="); - out.println(out.getEncoding()); - } - } - out.println(); - out.write(bos.toByteArray()); - - bos.close(); - return true; - } -} Index: test/org/apache/commons/httpclient/server/HttpRequestHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandler.java,v retrieving revision 1.3 diff -u -r1.3 HttpRequestHandler.java --- test/org/apache/commons/httpclient/server/HttpRequestHandler.java 22 Feb 2004 18:08:52 -0000 1.3 +++ test/org/apache/commons/httpclient/server/HttpRequestHandler.java 27 Feb 2004 17:04:13 -0000 @@ -37,6 +37,7 @@ * Defines an HTTP request handler for the SimpleHttpServer * * @author Christian Kohlschuetter + * @author Oleg Kalnichevski */ public interface HttpRequestHandler { /** @@ -53,9 +54,12 @@ * {@link HttpRequestHandlerChain} structures). * * @param conn The Connection object to which this request belongs to. + * @param request The request object. * @return true if this handler handled the request and no other handlers in the * chain should be called, false otherwise. * @throws IOException */ - public boolean processRequest(SimpleHttpServerConnection conn) throws IOException; + public boolean processRequest( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException; } Index: test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java,v retrieving revision 1.3 diff -u -r1.3 HttpRequestHandlerChain.java --- test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java 22 Feb 2004 18:08:52 -0000 1.3 +++ test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java 27 Feb 2004 17:04:13 -0000 @@ -62,11 +62,13 @@ subhandlers.add(handler); } - public synchronized boolean processRequest(SimpleHttpServerConnection conn) throws IOException { - + public synchronized boolean processRequest( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException + { for(Iterator it=subhandlers.iterator();it.hasNext();) { HttpRequestHandler h = (HttpRequestHandler)it.next(); - boolean stop = h.processRequest(conn); + boolean stop = h.processRequest(conn, request); if (stop) { return true; } Index: test/org/apache/commons/httpclient/server/HttpService.java =================================================================== RCS file: test/org/apache/commons/httpclient/server/HttpService.java diff -N test/org/apache/commons/httpclient/server/HttpService.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/org/apache/commons/httpclient/server/HttpService.java 27 Feb 2004 17:04:13 -0000 @@ -0,0 +1,54 @@ +/* + * $Header$ + * $Revision$ + * $Date$ + * + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed 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 + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient.server; + +import java.io.IOException; + +/** + * Defines an HTTP request/response service for the SimpleHttpServer + * + * @author Oleg Kalnichevski + */ +public interface HttpService { + /** + * This interface represents a serice to process HTTP requests. + * + * @param request The HTTP request object. + * @param response The HTTP response object. + * @return true if this service was able to handle the request, false otherwise. + * + * @throws IOException + */ + public boolean process( + final SimpleRequest request, final SimpleResponse response) + throws IOException; +} Index: test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java,v retrieving revision 1.8 diff -u -r1.8 ProxyAuthRequestHandler.java --- test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java 22 Feb 2004 18:08:52 -0000 1.8 +++ test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java 27 Feb 2004 17:04:13 -0000 @@ -71,9 +71,11 @@ this.credentials = creds; } - public boolean processRequest(SimpleHttpServerConnection conn) throws IOException { - Header[] headers = conn.getHeaders(); - Header clientAuth = findHeader(headers, PROXY_AUTH_RESP); + public boolean processRequest( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException + { + Header clientAuth = request.getFirstHeader(PROXY_AUTH_RESP); if (clientAuth != null) { boolean ok = checkAuthorization(clientAuth); if (ok) @@ -124,15 +126,6 @@ (UsernamePasswordCredentials)credentials, "ISO-8859-1"); return expectedAuthString.equals(clientAuth.getValue()); - } - - private Header findHeader(Header[] headers, String name) { - for (int i = 0; i < headers.length; i++) { - Header header = headers[i]; - if (header.getName().equalsIgnoreCase(name)) - return header; - } - return null; } } Index: test/org/apache/commons/httpclient/server/ProxyRequestHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java,v retrieving revision 1.3 diff -u -r1.3 ProxyRequestHandler.java --- test/org/apache/commons/httpclient/server/ProxyRequestHandler.java 22 Feb 2004 18:08:52 -0000 1.3 +++ test/org/apache/commons/httpclient/server/ProxyRequestHandler.java 27 Feb 2004 17:04:13 -0000 @@ -55,22 +55,29 @@ /** * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection) */ - public boolean processRequest(SimpleHttpServerConnection conn) throws IOException { - RequestLine line = conn.getRequestLine(); + public boolean processRequest( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException + { + RequestLine line = request.getRequestLine(); String method = line.getMethod(); //TODO add POST method handling if (!"GET".equalsIgnoreCase(method)) return false; - URI url = new HttpURL(line.getUri()); - httpProxy(conn, url); + httpProxy(conn, request); return true; } /** * @param conn */ - private void httpProxy(SimpleHttpServerConnection conn, URI url) throws IOException { + private void httpProxy( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException + { Log wireLog = LogFactory.getLog("httpclient.wire"); + URI url = new HttpURL(request.getRequestLine().getUri()); + HttpClient client = new HttpClient(); HostConfiguration hc = new HostConfiguration(); hc.setHost(url); @@ -78,7 +85,7 @@ //TODO support other methods HttpMethod method = new GetMethod(url.getPathQuery()); - Header[] headers = conn.getHeaders(); + Header[] headers = request.getHeaders(); for (int i=0; i. + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient.server; + +import java.util.Iterator; + +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HeaderElement; +import org.apache.commons.httpclient.HeaderGroup; +import org.apache.commons.httpclient.NameValuePair; + +/** + * A generic HTTP request. + * + * @author Oleg Kalnichevski + */ +public class SimpleRequest { + + private RequestLine requestLine = null; + private String contentType = "text/plain"; + private String charSet = null; + private String bodyString = null; + private HeaderGroup headers = new HeaderGroup(); + + public SimpleRequest() { + super(); + } + + public SimpleRequest( + final RequestLine requestLine, + final Header[] headers, + final String bodyString) + { + super(); + if (requestLine == null) { + throw new IllegalArgumentException("Request line may not be null"); + } + this.requestLine = requestLine; + if (headers != null) { + this.headers.setHeaders(headers); + Header content = this.headers.getFirstHeader("Content-Type"); + if (content != null) { + HeaderElement values[] = content.getElements(); + if (values.length == 1) { + this.contentType = values[0].getName(); + NameValuePair param = values[0].getParameterByName("charset"); + if (param != null) { + this.charSet = param.getValue(); + } + } + } + } + this.bodyString = bodyString; + + + } + + public String getContentType() { + return this.contentType; + } + + public String getCharSet() { + return this.charSet; + } + + public String getBodyString() { + return this.bodyString; + } + + public RequestLine getRequestLine() { + return this.requestLine; + } + + public boolean containsHeader(final String name) { + return this.headers.containsHeader(name); + } + + public Header[] getHeaders() { + return this.headers.getAllHeaders(); + } + + public Header getFirstHeader(final String s) { + return this.headers.getFirstHeader(s); + } + + public Iterator getHeaderIterator() { + return this.headers.getIterator(); + } +} Index: test/org/apache/commons/httpclient/server/SimpleResponse.java =================================================================== RCS file: test/org/apache/commons/httpclient/server/SimpleResponse.java diff -N test/org/apache/commons/httpclient/server/SimpleResponse.java --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ test/org/apache/commons/httpclient/server/SimpleResponse.java 27 Feb 2004 17:04:13 -0000 @@ -0,0 +1,121 @@ +/* + * $Header: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/GenericResponse.java,v 1.3 2004/02/22 18:08:52 olegk Exp $ + * $Revision: 1.3 $ + * $Date: 2004/02/22 18:08:52 $ + * + * ==================================================================== + * + * Copyright 1999-2004 The Apache Software Foundation + * + * Licensed 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 + * . + * + * [Additional notices, if required by prior licensing conditions] + * + */ + +package org.apache.commons.httpclient.server; + +import java.util.Iterator; + +import org.apache.commons.httpclient.Header; +import org.apache.commons.httpclient.HeaderGroup; + +/** + * A generic HTTP response. + * + * @author Christian Kohlschuetter + * @author Oleg Kalnichevski + */ +public class SimpleResponse { + + private String statusLine = "HTTP/1.0 200 OK"; + private String contentType = "text/plain"; + private String bodyString = null; + private HeaderGroup headers = new HeaderGroup(); + + public SimpleResponse() { + super(); + } + + public SimpleResponse(final String statusLine) { + super(); + this.statusLine = statusLine; + } + + public String getContentType() { + return this.contentType; + } + + public void setContentType(String string) { + this.contentType = string; + } + + public void setBodyString(String string) { + this.bodyString = string; + } + + public String getBodyString() { + return this.bodyString; + } + + public String getStatusLine() { + return this.statusLine; + } + + public void setStatusLine(String string) { + this.statusLine = string; + } + + public boolean containsHeader(final String name) { + return this.headers.containsHeader(name); + } + + public Header[] getHeaders() { + return this.headers.getAllHeaders(); + } + + public void setHeader(final Header header) { + if (header == null) { + return; + } + Header[] headers = this.headers.getHeaders(header.getName()); + for (int i = 0; i < headers.length; i++) { + this.headers.removeHeader(headers[i]); + } + this.headers.addHeader(header); + } + + public void addHeader(final Header header) { + if (header == null) { + return; + } + this.headers.addHeader(header); + } + + public void setHeaders(final Header[] headers) { + if (headers == null) { + return; + } + this.headers.setHeaders(headers); + } + + public Iterator getHeaderIterator() { + return this.headers.getIterator(); + } +} Index: test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java =================================================================== RCS file: /home/cvspublic/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java,v retrieving revision 1.5 diff -u -r1.5 TransparentProxyRequestHandler.java --- test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java 22 Feb 2004 18:08:52 -0000 1.5 +++ test/org/apache/commons/httpclient/server/TransparentProxyRequestHandler.java 27 Feb 2004 17:04:13 -0000 @@ -56,8 +56,11 @@ * * @see org.apache.commons.httpclient.server.HttpRequestHandler#processRequest(org.apache.commons.httpclient.server.SimpleHttpServerConnection) */ - public boolean processRequest(SimpleHttpServerConnection conn) throws IOException { - RequestLine line = conn.getRequestLine(); + public boolean processRequest( + final SimpleHttpServerConnection conn, + final SimpleRequest request) throws IOException + { + RequestLine line = request.getRequestLine(); String method = line.getMethod(); if (!"CONNECT".equalsIgnoreCase(method)) return false;