olegk 2004/11/20 09:56:40 Modified: httpclient/src/test/org/apache/commons/httpclient EchoService.java TestNoHost.java TestProxy.java TestWebapp.java httpclient/src/test/org/apache/commons/httpclient/auth TestBasicAuth.java httpclient/src/test/org/apache/commons/httpclient/server SimpleConnManager.java Added: httpclient/src/test/org/apache/commons/httpclient FeedbackService.java httpclient/src/test/org/apache/commons/httpclient/server AuthRequestHandler.java Log: * Proxy test cases ported to the SimpleHttpServer/SimpleProxy framework and no longer require the test webapp * Basic authentication test cases refactored Contributed by Oleg Kalnichevski Revision Changes Path 1.2 +4 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/EchoService.java Index: EchoService.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/EchoService.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- EchoService.java 6 Oct 2004 03:39:58 -0000 1.1 +++ EchoService.java 20 Nov 2004 17:56:39 -0000 1.2 @@ -38,7 +38,7 @@ /** * A service that echos the request body. */ -class EchoService implements HttpService { +public class EchoService implements HttpService { public EchoService() { super(); 1.48 +7 -5 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java Index: TestNoHost.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v retrieving revision 1.47 retrieving revision 1.48 diff -u -r1.47 -r1.48 --- TestNoHost.java 20 Nov 2004 11:16:48 -0000 1.47 +++ TestNoHost.java 20 Nov 2004 17:56:39 -0000 1.48 @@ -97,8 +97,10 @@ suite.addTest(TestPostMethod.suite()); suite.addTest(TestPartsNoHost.suite()); suite.addTest(TestMultipartPost.suite()); - + // Non compliant behaviour suite.addTest(TestNoncompliant.suite()); + // Proxy + suite.addTest(TestProxy.suite()); return suite; } 1.8 +90 -66 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java Index: TestProxy.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestProxy.java,v retrieving revision 1.7 retrieving revision 1.8 diff -u -r1.7 -r1.8 --- TestProxy.java 13 Nov 2004 12:21:28 -0000 1.7 +++ TestProxy.java 20 Nov 2004 17:56:39 -0000 1.8 @@ -32,6 +32,10 @@ import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.methods.StringRequestEntity; import org.apache.commons.httpclient.protocol.Protocol; +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.server.SimpleHttpServer; import org.apache.commons.httpclient.server.SimpleProxy; import junit.framework.Test; @@ -45,8 +49,9 @@ */ public class TestProxy extends TestCase { - private static String TARGET_HOST = null; - private static int TARGET_PORT = -1; + private SimpleProxy proxy = null; + private SimpleHttpServer httpserver = null; + private HttpClient httpclient = null; public TestProxy(String testName) { super(testName); @@ -58,75 +63,94 @@ protected void setUp() throws Exception { super.setUp(); - TARGET_HOST = System.getProperty("httpclient.test.localHost", "localhost"); - TARGET_PORT = Integer.parseInt(System.getProperty("httpclient.test.localPort", "8080")); + this.proxy = new SimpleProxy(); + this.httpserver = new SimpleHttpServer(); + this.httpclient = new HttpClient(); + this.httpclient.getHostConfiguration().setHost( + this.httpserver.getLocalAddress(), + this.httpserver.getLocalPort(), + Protocol.getProtocol("http")); + this.httpclient.getHostConfiguration().setProxy( + this.proxy.getLocalAddress(), + this.proxy.getLocalPort()); + } + + protected void tearDown() throws Exception { + this.httpclient = null; + this.proxy.destroy(); + this.proxy = null; + this.httpserver.destroy(); + this.httpserver = null; + super.tearDown(); } - + public void testSimpleGet() throws Exception { - SimpleProxy proxy = new SimpleProxy(); - - HttpClient client = new HttpClient(); - HostConfiguration hc = new HostConfiguration(); - hc.setHost(TARGET_HOST, TARGET_PORT, Protocol.getProtocol("http")); - hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort()); - client.setHostConfiguration(hc); - + this.httpserver.setHttpService(new FeedbackService()); GetMethod get = new GetMethod("/"); - client.executeMethod(get); - assertEquals(200, get.getStatusCode()); - get.releaseConnection(); - } - - public void testAuthGet() throws Exception { - Credentials creds = new UsernamePasswordCredentials("user", "password"); - SimpleProxy proxy = new SimpleProxy(); - proxy.requireCredentials(creds); - - HttpClient client = new HttpClient(); - HostConfiguration hc = new HostConfiguration(); - hc.setHost(TARGET_HOST, TARGET_PORT, Protocol.getProtocol("http")); - hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort()); - client.setHostConfiguration(hc); - client.getState().setProxyCredentials(AuthScope.ANY, creds); - + try { + this.httpclient.executeMethod(get); + assertEquals(200, get.getStatusCode()); + } finally { + get.releaseConnection(); + } + } + + public void testAuthHostGet() throws Exception { + + UsernamePasswordCredentials creds = + new UsernamePasswordCredentials("testuser", "testpass"); + + this.httpclient.getState().setCredentials(AuthScope.ANY, creds); + + HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); + handlerchain.appendHandler(new AuthRequestHandler(creds)); + handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); + + this.httpserver.setRequestHandler(handlerchain); + GetMethod get = new GetMethod("/"); - client.executeMethod(get); - assertEquals(200, get.getStatusCode()); - get.releaseConnection(); - } - - public void testSimplePost() throws Exception { - SimpleProxy proxy = new SimpleProxy(); - - HttpClient client = new HttpClient(); - HostConfiguration hc = new HostConfiguration(); - hc.setHost(TARGET_HOST, TARGET_PORT, Protocol.getProtocol("http")); - hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort()); - client.setHostConfiguration(hc); - - PostMethod post = new PostMethod("/httpclienttest/body"); + try { + this.httpclient.executeMethod(get); + assertEquals(200, get.getStatusCode()); + } finally { + get.releaseConnection(); + } + } + + public void testSimplePost() throws Exception { + this.httpserver.setHttpService(new FeedbackService()); + PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff")); - client.executeMethod(post); - assertEquals(200, post.getStatusCode()); - post.releaseConnection(); + try { + this.httpclient.executeMethod(post); + assertEquals(200, post.getStatusCode()); + assertNotNull(post.getResponseBodyAsString()); + } finally { + post.releaseConnection(); + } } public void testAuthPost() throws Exception { - Credentials creds = new UsernamePasswordCredentials("user", "password"); - SimpleProxy proxy = new SimpleProxy(); - proxy.requireCredentials(creds); - - HttpClient client = new HttpClient(); - HostConfiguration hc = new HostConfiguration(); - hc.setHost(TARGET_HOST, TARGET_PORT, Protocol.getProtocol("http")); - hc.setProxy(proxy.getLocalAddress(), proxy.getLocalPort()); - client.setHostConfiguration(hc); - client.getState().setProxyCredentials(AuthScope.ANY, creds); - - PostMethod post = new PostMethod("/httpclienttest/body"); + UsernamePasswordCredentials creds = + new UsernamePasswordCredentials("testuser", "testpass"); + + this.httpclient.getState().setCredentials(AuthScope.ANY, creds); + + HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain(); + handlerchain.appendHandler(new AuthRequestHandler(creds)); + handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService())); + + this.httpserver.setRequestHandler(handlerchain); + + PostMethod post = new PostMethod("/"); post.setRequestEntity(new StringRequestEntity("Like tons of stuff")); - client.executeMethod(post); - assertEquals(200, post.getStatusCode()); - post.releaseConnection(); + try { + this.httpclient.executeMethod(post); + assertEquals(200, post.getStatusCode()); + assertNotNull(post.getResponseBodyAsString()); + } finally { + post.releaseConnection(); + } } + } 1.15 +4 -5 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebapp.java Index: TestWebapp.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebapp.java,v retrieving revision 1.14 retrieving revision 1.15 diff -u -r1.14 -r1.15 --- TestWebapp.java 14 Nov 2004 23:38:00 -0000 1.14 +++ TestWebapp.java 20 Nov 2004 17:56:39 -0000 1.15 @@ -65,7 +65,6 @@ suite.addTest(TestWebappParameters.suite()); suite.addTest(TestWebappHeaders.suite()); suite.addTest(TestWebappPostMethod.suite()); - suite.addTest(TestProxy.suite()); return suite; } 1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/FeedbackService.java Index: FeedbackService.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/FeedbackService.java,v 1.1 2004/11/20 17:56:39 olegk Exp $ * $Revision: 1.1 $ * $Date: 2004/11/20 17:56:39 $ * * ==================================================================== * * Copyright 2002-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 * <http://www.apache.org/>. * */ package org.apache.commons.httpclient; import java.io.IOException; import java.io.StringWriter; 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; public class FeedbackService implements HttpService { public FeedbackService() { super(); } public boolean process(final SimpleRequest request, final SimpleResponse response) throws IOException { RequestLine requestline = request.getRequestLine(); HttpVersion httpversion = requestline.getHttpVersion(); StringWriter buffer = new StringWriter(100); buffer.write("Method type: "); buffer.write(requestline.getMethod()); buffer.write("\r\n"); buffer.write("Requested resource: "); buffer.write(requestline.getUri()); buffer.write("\r\n"); buffer.write("Protocol version: "); buffer.write(httpversion.toString()); buffer.write("\r\n"); String requestbody = request.getBodyString(); if (requestbody != null && !requestbody.equals("")) { buffer.write("\r\n"); buffer.write("Request body: "); buffer.write(requestbody); buffer.write("\r\n"); } response.setStatusLine(httpversion, HttpStatus.SC_OK); response.setBodyString(buffer.toString()); return true; } } 1.9 +158 -216 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java Index: TestBasicAuth.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/auth/TestBasicAuth.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- TestBasicAuth.java 9 Nov 2004 04:25:05 -0000 1.8 +++ TestBasicAuth.java 20 Nov 2004 17:56:40 -0000 1.9 @@ -24,8 +24,6 @@ * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * - * [Additional notices, if required by prior licensing conditions] - * */ package org.apache.commons.httpclient.auth; @@ -36,11 +34,12 @@ 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.HttpVersion; import org.apache.commons.httpclient.ProxyTestDecorator; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.methods.GetMethod; @@ -48,10 +47,9 @@ 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.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.server.AuthRequestHandler; +import org.apache.commons.httpclient.server.HttpRequestHandlerChain; +import org.apache.commons.httpclient.server.HttpServiceHandler; import org.apache.commons.httpclient.util.EncodingUtil; /** @@ -82,152 +80,16 @@ return suite; } - 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(); - HttpVersion ver = requestLine.getHttpVersion(); - Header auth = request.getFirstHeader("Authorization"); - if (auth == null) { - response.setStatusLine(ver, HttpStatus.SC_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(ver, HttpStatus.SC_FORBIDDEN); - response.addHeader(challenge); - response.setBodyString("Access forbidden"); - return true; - } - response.setStatusLine(ver, HttpStatus.SC_OK); - String requestBody = request.getBodyString(); - if (requestBody != null && requestBody.length() > 0) { - // echo the body if there is one - response.setBodyString(requestBody); - } else { - 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(); - HttpVersion ver = requestLine.getHttpVersion(); - Header auth = request.getFirstHeader("Authorization"); - if (auth == null) { - response.setStatusLine(ver, HttpStatus.SC_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(ver, HttpStatus.SC_FORBIDDEN); - response.addHeader(challenge); - response.setBodyString("Access forbidden"); - return true; - } - response.setStatusLine(ver, HttpStatus.SC_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(); - HttpVersion ver = requestLine.getHttpVersion(); - Header auth = request.getFirstHeader("Authorization"); - if (auth == null) { - response.setStatusLine(ver, HttpStatus.SC_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(ver, HttpStatus.SC_FORBIDDEN); - response.addHeader(challenge); - response.setBodyString("Access forbidden"); - return true; - } - response.setStatusLine(ver, HttpStatus.SC_OK); - response.setBodyString("Authorization successful"); - return true; - } - } - public void testBasicAuthenticationWithNoCreds() throws IOException { - this.server.setHttpService(new BasicAuthService()); + + 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); @@ -243,7 +105,15 @@ } public void testBasicAuthenticationWithNoCredsRetry() throws IOException { - this.server.setHttpService(new BasicAuthService()); + 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); @@ -259,7 +129,7 @@ // now try with credentials httpget = new GetMethod("/test/"); try { - this.client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("test", "test")); + this.client.getState().setCredentials(AuthScope.ANY, creds); this.client.executeMethod(httpget); assertNotNull(httpget.getStatusLine()); assertEquals(HttpStatus.SC_OK, httpget.getStatusLine().getStatusCode()); @@ -292,10 +162,19 @@ } 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, new UsernamePasswordCredentials("test", "test")); + state.setCredentials(AuthScope.ANY, creds); this.client.setState(state); - this.server.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + GetMethod httpget = new GetMethod("/test/"); try { this.client.executeMethod(httpget); @@ -307,7 +186,7 @@ Header auth = httpget.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); + Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); assertEquals(expected, auth.getValue()); AuthState authstate = httpget.getHostAuthState(); assertNotNull(authstate.getAuthScheme()); @@ -316,14 +195,23 @@ } 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, new UsernamePasswordCredentials("test", "test")); + state.setCredentials(authscope, creds); this.client.setState(state); - this.server.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + GetMethod httpget = new GetMethod("/test/"); try { this.client.executeMethod(httpget); @@ -335,7 +223,7 @@ Header auth = httpget.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); + Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); assertEquals(expected, auth.getValue()); AuthState authstate = httpget.getHostAuthState(); assertNotNull(authstate.getAuthScheme()); @@ -344,6 +232,13 @@ } public void testBasicAuthenticationWithInvalidCredentials() 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(), @@ -351,7 +246,9 @@ "test"); state.setCredentials(authscope, new UsernamePasswordCredentials("test", "stuff")); this.client.setState(state); - this.server.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + GetMethod httpget = new GetMethod("/test/"); try { this.client.executeMethod(httpget); @@ -359,7 +256,7 @@ httpget.releaseConnection(); } assertNotNull(httpget.getStatusLine()); - assertEquals(HttpStatus.SC_FORBIDDEN, httpget.getStatusLine().getStatusCode()); + assertEquals(HttpStatus.SC_UNAUTHORIZED, httpget.getStatusLine().getStatusCode()); AuthState authstate = httpget.getHostAuthState(); assertNotNull(authstate.getAuthScheme()); assertTrue(authstate.getAuthScheme() instanceof BasicScheme); @@ -367,6 +264,13 @@ } public void testBasicAuthenticationWithMutlipleRealms1() 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 realm1 = new AuthScope( this.server.getLocalAddress(), @@ -376,11 +280,12 @@ this.server.getLocalAddress(), this.server.getLocalPort(), "test2"); - state.setCredentials(realm1, new UsernamePasswordCredentials("test","test")); - state.setCredentials(realm2, new UsernamePasswordCredentials("test2","test2")); + state.setCredentials(realm1, new UsernamePasswordCredentials("testuser","testpass")); + state.setCredentials(realm2, new UsernamePasswordCredentials("testuser2","testpass2")); this.client.setState(state); - this.server.setHttpService(new BasicAuthService()); + this.server.setRequestHandler(handlerchain); + GetMethod httpget = new GetMethod("/test/"); try { this.client.executeMethod(httpget); @@ -392,7 +297,7 @@ Header auth = httpget.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); + Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); assertEquals(expected, auth.getValue()); AuthState authstate = httpget.getHostAuthState(); assertNotNull(authstate.getAuthScheme()); @@ -401,6 +306,13 @@ } 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(), @@ -410,11 +322,12 @@ this.server.getLocalAddress(), this.server.getLocalPort(), "test2"); - state.setCredentials(realm1, new UsernamePasswordCredentials("test","test")); - state.setCredentials(realm2, new UsernamePasswordCredentials("test2","test2")); + state.setCredentials(realm1, new UsernamePasswordCredentials("testuser","testpass")); + state.setCredentials(realm2, new UsernamePasswordCredentials("testuser2","testpass2")); this.client.setState(state); - this.server.setHttpService(new BasicAuthService2()); + this.server.setRequestHandler(handlerchain); + GetMethod httpget = new GetMethod("/test2/"); try { this.client.executeMethod(httpget); @@ -426,7 +339,7 @@ Header auth = httpget.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test2:test2"))); + Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser2:testpass2"))); assertEquals(expected, auth.getValue()); AuthState authstate = httpget.getHostAuthState(); assertNotNull(authstate.getAuthScheme()); @@ -435,11 +348,20 @@ } 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, new UsernamePasswordCredentials("test", "test")); + state.setCredentials(AuthScope.ANY, creds); this.client.setState(state); this.client.getParams().setAuthenticationPreemptive(true); - this.server.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + GetMethod httpget = new GetMethod("/test/"); try { this.client.executeMethod(httpget); @@ -451,7 +373,7 @@ Header auth = httpget.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); + Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); assertEquals(expected, auth.getValue()); AuthState authstate = httpget.getHostAuthState(); assertNotNull(authstate.getAuthScheme()); @@ -461,10 +383,19 @@ } 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.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + GetMethod httpget = new GetMethod("/test/"); try { this.client.executeMethod(httpget); @@ -482,35 +413,19 @@ assertFalse(authstate.isPreemptive()); } - public void testBasicAuthenticationCaseInsensitivity() throws Exception { - HttpState state = new HttpState(); - AuthScope authscope = new AuthScope( - this.server.getLocalAddress(), - this.server.getLocalPort(), - "test"); - state.setCredentials(authscope, 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()); - } - public void testCustomAuthorizationHeader() throws Exception { - String authResponse = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); - this.server.setHttpService(new BasicAuthService()); + 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); @@ -522,14 +437,23 @@ } 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, new UsernamePasswordCredentials("test", "test")); + state.setCredentials(authscope, creds); this.client.setState(state); - this.server.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + HeadMethod head = new HeadMethod("/test/"); try { this.client.executeMethod(head); @@ -541,23 +465,32 @@ Header auth = head.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); + 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, new UsernamePasswordCredentials("test", "test")); + state.setCredentials(authscope, creds); this.client.setState(state); - this.server.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + PostMethod post = new PostMethod("/test/"); post.setRequestEntity(new StringRequestEntity("Test body")); try { @@ -571,7 +504,7 @@ Header auth = post.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); + Base64.encodeBase64(EncodingUtil.getAsciiBytes("testuser:testpass"))); assertEquals(expected, auth.getValue()); AuthState authstate = post.getHostAuthState(); assertNotNull(authstate.getAuthScheme()); @@ -580,14 +513,23 @@ } 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, new UsernamePasswordCredentials("test", "test")); + state.setCredentials(authscope, creds); this.client.setState(state); - this.server.setHttpService(new BasicAuthService()); + + this.server.setRequestHandler(handlerchain); + PutMethod put = new PutMethod("/test/"); put.setRequestEntity(new StringRequestEntity("Test body")); try { @@ -601,12 +543,12 @@ Header auth = put.getRequestHeader("Authorization"); assertNotNull(auth); String expected = "Basic " + EncodingUtil.getAsciiString( - Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test"))); + 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()); } - + } 1.2 +4 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnManager.java Index: SimpleConnManager.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleConnManager.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SimpleConnManager.java 13 Nov 2004 12:21:28 -0000 1.1 +++ SimpleConnManager.java 20 Nov 2004 17:56:40 -0000 1.2 @@ -62,7 +62,7 @@ SimpleConnList connlist = (SimpleConnList)this.connsets.get(host); if (connlist != null) { conn = connlist.removeFirst(); - if (!conn.isOpen()) { + if (conn != null && !conn.isOpen()) { conn = null; } } 1.1 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/AuthRequestHandler.java Index: AuthRequestHandler.java =================================================================== /* * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/AuthRequestHandler.java,v 1.1 2004/11/20 17:56:40 olegk Exp $ * $Revision: 1.1 $ * $Date: 2004/11/20 17:56:40 $ * * ==================================================================== * * 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 * <http://www.apache.org/>. * */ package org.apache.commons.httpclient.server; import java.io.IOException; import org.apache.commons.httpclient.Credentials; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.BasicScheme; /** * This request handler guards access to the http server when used in a request handler * chain. It checks the headers for valid credentials and performs the * authentication handshake if necessary. * * @author Ortwin Glueck * @author Oleg Kalnichevski */ public class AuthRequestHandler implements HttpRequestHandler { private Credentials credentials = null; private String realm = null; private boolean keepalive = true; /** * The authenticate response header. */ public static final String AUTH_RESP = "Authorization"; /** * TODO replace creds parameter with a class specific to an auth scheme * encapsulating all required information for a specific scheme * * @param creds */ public AuthRequestHandler(final Credentials creds, final String realm, boolean keepalive) { if (creds == null) throw new IllegalArgumentException("Credentials may not be null"); this.credentials = creds; this.keepalive = keepalive; if (realm != null) { this.realm = realm; } else { this.realm = "test"; } } public AuthRequestHandler(final Credentials creds, final String realm) { this(creds, realm, true); } public AuthRequestHandler(final Credentials creds) { this(creds, null, true); } public boolean processRequest( final SimpleHttpServerConnection conn, final SimpleRequest request) throws IOException { Header clientAuth = request.getFirstHeader(AUTH_RESP); if (clientAuth != null && checkAuthorization(clientAuth)) { return false; } else { SimpleResponse response = performBasicHandshake(conn, request); // Make sure the request body is fully consumed request.getBodyBytes(); conn.writeResponse(response); return true; } } //TODO add more auth schemes private SimpleResponse performBasicHandshake( final SimpleHttpServerConnection conn, final SimpleRequest request) throws IOException { SimpleResponse response = new SimpleResponse(); response.setStatusLine( request.getRequestLine().getHttpVersion(), HttpStatus.SC_UNAUTHORIZED); if (!request.getRequestLine().getMethod().equalsIgnoreCase("HEAD")) { response.setBodyString("unauthorized"); } response.addHeader(new Header("WWW-Authenticate", "basic realm=\"" + this.realm + "\"")); if (this.keepalive) { response.addHeader(new Header("Connection", "keep-alive")); conn.setKeepAlive(true); } else { response.addHeader(new Header("Connection", "close")); conn.setKeepAlive(false); } return response; } /** * Checks if the credentials provided by the client match the required * credentials * * @return true if the client is authorized, false if not. * @param clientAuth */ private boolean checkAuthorization(final Header clientAuth) { String expectedAuthString = BasicScheme.authenticate( (UsernamePasswordCredentials)credentials, "ISO-8859-1"); return expectedAuthString.equals(clientAuth.getValue()); } }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]