Return-Path: Delivered-To: apmail-jakarta-commons-dev-archive@www.apache.org Received: (qmail 71626 invoked from network); 27 Feb 2004 19:01:41 -0000 Received: from daedalus.apache.org (HELO mail.apache.org) (208.185.179.12) by minotaur-2.apache.org with SMTP; 27 Feb 2004 19:01:41 -0000 Received: (qmail 40585 invoked by uid 500); 27 Feb 2004 19:01:29 -0000 Delivered-To: apmail-jakarta-commons-dev-archive@jakarta.apache.org Received: (qmail 40315 invoked by uid 500); 27 Feb 2004 19:01:26 -0000 Mailing-List: contact commons-dev-help@jakarta.apache.org; run by ezmlm Precedence: bulk List-Unsubscribe: List-Subscribe: List-Help: List-Post: List-Id: "Jakarta Commons Developers List" Reply-To: "Jakarta Commons Developers List" Delivered-To: mailing list commons-dev@jakarta.apache.org Received: (qmail 40302 invoked by uid 500); 27 Feb 2004 19:01:26 -0000 Received: (qmail 40298 invoked from network); 27 Feb 2004 19:01:26 -0000 Received: from unknown (HELO minotaur.apache.org) (209.237.227.194) by daedalus.apache.org with SMTP; 27 Feb 2004 19:01:26 -0000 Received: (qmail 71498 invoked by uid 1624); 27 Feb 2004 19:01:34 -0000 Date: 27 Feb 2004 19:01:34 -0000 Message-ID: <20040227190134.71497.qmail@minotaur.apache.org> From: olegk@apache.org To: jakarta-commons-cvs@apache.org Subject: cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server ErrorResponse.java HttpRequestHandler.java HttpRequestHandlerChain.java ProxyAuthRequestHandler.java ProxyRequestHandler.java SimpleHttpServer.java SimpleHttpServerConnection.java TransparentProxyRequestHandler.java GenericResponse.java X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N X-Spam-Rating: minotaur-2.apache.org 1.6.2 0/1000/N olegk 2004/02/27 11:01:34 Modified: httpclient/src/test/org/apache/commons/httpclient HttpClientTestBase.java TestAuthenticator.java TestBadContentLength.java TestNoHost.java httpclient/src/test/org/apache/commons/httpclient/server ErrorResponse.java HttpRequestHandler.java HttpRequestHandlerChain.java ProxyAuthRequestHandler.java ProxyRequestHandler.java SimpleHttpServer.java SimpleHttpServerConnection.java TransparentProxyRequestHandler.java Removed: httpclient/src/test/org/apache/commons/httpclient/server GenericResponse.java Log: Changelog * SimpleRequest and SimpleResponse classes factored out * Interface HttpService can be used instead of HttpRequestHandler to implement test cases in a way very similar to writing servlets. * Basic authentication test cases ported to the new testing framework Contributed by Oleg Kalnichevski Revision Changes Path 1.2 +4 -37 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java Index: HttpClientTestBase.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/HttpClientTestBase.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- HttpClientTestBase.java 25 Feb 2004 22:33:58 -0000 1.1 +++ HttpClientTestBase.java 27 Feb 2004 19:01:33 -0000 1.2 @@ -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(); } } 1.41 +7 -154 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java Index: TestAuthenticator.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestAuthenticator.java,v retrieving revision 1.40 retrieving revision 1.41 diff -u -r1.40 -r1.41 --- TestAuthenticator.java 22 Feb 2004 18:08:49 -0000 1.40 +++ TestAuthenticator.java 27 Feb 2004 19:01:33 -0000 1.41 @@ -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 1.6 +10 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBadContentLength.java Index: TestBadContentLength.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestBadContentLength.java,v retrieving revision 1.5 retrieving revision 1.6 diff -u -r1.5 -r1.6 --- TestBadContentLength.java 22 Feb 2004 18:08:49 -0000 1.5 +++ TestBadContentLength.java 27 Feb 2004 19:01:33 -0000 1.6 @@ -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())) { 1.32 +10 -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.31 retrieving revision 1.32 diff -u -r1.31 -r1.32 --- TestNoHost.java 22 Feb 2004 18:08:49 -0000 1.31 +++ TestNoHost.java 27 Feb 2004 19:01:33 -0000 1.32 @@ -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()); 1.4 +24 -19 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ErrorResponse.java Index: ErrorResponse.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ErrorResponse.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ErrorResponse.java 22 Feb 2004 18:08:52 -0000 1.3 +++ ErrorResponse.java 27 Feb 2004 19:01:33 -0000 1.4 @@ -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; } } 1.4 +8 -4 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandler.java Index: HttpRequestHandler.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandler.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HttpRequestHandler.java 22 Feb 2004 18:08:52 -0000 1.3 +++ HttpRequestHandler.java 27 Feb 2004 19:01:33 -0000 1.4 @@ -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; } 1.4 +8 -6 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java Index: HttpRequestHandlerChain.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpRequestHandlerChain.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- HttpRequestHandlerChain.java 22 Feb 2004 18:08:52 -0000 1.3 +++ HttpRequestHandlerChain.java 27 Feb 2004 19:01:34 -0000 1.4 @@ -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; } 1.9 +8 -15 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java Index: ProxyAuthRequestHandler.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyAuthRequestHandler.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- ProxyAuthRequestHandler.java 22 Feb 2004 18:08:52 -0000 1.8 +++ ProxyAuthRequestHandler.java 27 Feb 2004 19:01:34 -0000 1.9 @@ -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; } } 1.4 +16 -9 jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java Index: ProxyRequestHandler.java =================================================================== RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/ProxyRequestHandler.java,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- ProxyRequestHandler.java 22 Feb 2004 18:08:52 -0000 1.3 +++ ProxyRequestHandler.java 27 Feb 2004 19:01:34 -0000 1.4 @@ -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