commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server HttpRequestHandlerChain.java ProxyAuthRequestHandler.java SimpleProxy.java
Date Sun, 28 Nov 2004 15:44:39 GMT
olegk       2004/11/28 07:44:39

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestProxy.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        HttpRequestHandlerChain.java
                        ProxyAuthRequestHandler.java SimpleProxy.java
  Log:
  Authenticating proxy test cases
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.10      +338 -5    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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- TestProxy.java	20 Nov 2004 19:35:30 -0000	1.9
  +++ TestProxy.java	28 Nov 2004 15:44:39 -0000	1.10
  @@ -214,7 +214,7 @@
       }
   
       /**
  -     * Tests GET via non-authenticating proxy + intercative host auth + connection keep-alive

  +     * Tests GET via non-authenticating proxy + interactive host auth + connection keep-alive

        */
       public void testGetInteractiveHostAuthConnKeepAlive() throws Exception {
   
  @@ -240,7 +240,7 @@
       }
      
       /**
  -     * Tests GET via non-authenticating proxy + intercative host auth + connection close

  +     * Tests GET via non-authenticating proxy + interactive host auth + connection close

        */
       public void testGetInteractiveHostAuthConnClose() throws Exception {
   
  @@ -266,6 +266,168 @@
       }
   
       /**
  +     * Tests GET via authenticating proxy + host auth + connection keep-alive 
  +     */
  +    public void testGetProxyAuthHostAuthConnKeepAlive() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +    
  +    /**
  +     * Tests GET via authenticating proxy
  +     */
  +    public void testGetAuthProxy() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        this.httpserver.setHttpService(new FeedbackService());
  +
  +        this.proxy.requireAuthentication(creds, "test", true);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +    
  +    /**
  +     * Tests GET via authenticating proxy + host auth + connection close 
  +     */
  +    public void testGetProxyAuthHostAuthConnClose() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +
  +        this.proxy.requireAuthentication(creds, "test", true);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +    
  +    /**
  +     * Tests GET via authenticating proxy + invalid host auth 
  +     */
  +    public void testGetProxyAuthHostInvalidAuth() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY,
  +                new UsernamePasswordCredentials("testuser", "wrongstuff"));
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_UNAUTHORIZED, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +
  +    /**
  +     * Tests GET via authenticating proxy + interactive host and proxy auth + connection
keep-alive 
  +     */
  +    public void testGetInteractiveProxyAuthHostAuthConnKeepAlive() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER, 
  +                new GetItWrongThenGetItRight());
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +
  +        this.proxy.requireAuthentication(creds, "test", true);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +
  +    /**
  +     * Tests GET via authenticating proxy + interactive host and proxy auth + connection
close 
  +     */
  +    public void testGetInteractiveProxyAuthHostAuthConnClose() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER, 
  +                new GetItWrongThenGetItRight());
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +
  +    /**
        * Tests POST via non-authenticating proxy
        */
       public void testSimplePost() throws Exception {
  @@ -415,4 +577,175 @@
               post.releaseConnection();
           }
       }
  +
  +    /**
  +     * Tests POST via authenticating proxy
  +     */
  +    public void testPostAuthProxy() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        this.httpserver.setHttpService(new FeedbackService());
  +
  +        this.proxy.requireAuthentication(creds, "test", true);
  +
  +        PostMethod post = new PostMethod("/");
  +        post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
  +        try {
  +            this.httpclient.executeMethod(post);
  +            assertEquals(HttpStatus.SC_OK, post.getStatusCode());
  +            assertNotNull(post.getResponseBodyAsString());
  +        } finally {
  +            post.releaseConnection();
  +        }
  +    }
  +
  +    /**
  +     * Tests POST via authenticating proxy + host auth + connection keep-alive 
  +     */
  +    public void testPostProxyAuthHostAuthConnKeepAlive() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +
  +        PostMethod post = new PostMethod("/");
  +        post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
  +        try {
  +            this.httpclient.executeMethod(post);
  +            assertEquals(HttpStatus.SC_OK, post.getStatusCode());
  +            assertNotNull(post.getResponseBodyAsString());
  +        } finally {
  +            post.releaseConnection();
  +        }
  +    }
  +
  +    /**
  +     * Tests POST via authenticating proxy + host auth + connection close 
  +     */
  +    public void testPostProxyAuthHostAuthConnClose() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +
  +        PostMethod post = new PostMethod("/");
  +        post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
  +        try {
  +            this.httpclient.executeMethod(post);
  +            assertEquals(HttpStatus.SC_OK, post.getStatusCode());
  +            assertNotNull(post.getResponseBodyAsString());
  +        } finally {
  +            post.releaseConnection();
  +        }
  +    }
  +
  +    /**
  +     * Tests POST via non-authenticating proxy + invalid host auth 
  +     */
  +    public void testPostProxyAuthHostInvalidAuth() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY,
  +                new UsernamePasswordCredentials("testuser", "wrongstuff"));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +
  +        PostMethod post = new PostMethod("/");
  +        post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
  +        try {
  +            this.httpclient.executeMethod(post);
  +            assertEquals(HttpStatus.SC_UNAUTHORIZED, post.getStatusCode());
  +        } finally {
  +            post.releaseConnection();
  +        }
  +    }
  +
  +    /**
  +     * Tests POST via non-authenticating proxy + interactive host auth + connection keep-alive

  +     */
  +    public void testPostInteractiveProxyAuthHostAuthConnKeepAlive() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER, 
  +                new GetItWrongThenGetItRight());
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", true));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +
  +        PostMethod post = new PostMethod("/");
  +        post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
  +        try {
  +            this.httpclient.executeMethod(post);
  +            assertEquals(HttpStatus.SC_OK, post.getStatusCode());
  +            assertNotNull(post.getResponseBodyAsString());
  +        } finally {
  +            post.releaseConnection();
  +        }
  +    }
  +
  +    /**
  +     * Tests POST via non-authenticating proxy + interactive host auth + connection close

  +     */
  +    public void testPostInteractiveProxyAuthHostAuthConnClose() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getParams().setParameter(CredentialsProvider.PROVIDER, 
  +                new GetItWrongThenGetItRight());
  +                
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", false));
  +        handlerchain.appendHandler(new HttpServiceHandler(new FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        this.proxy.requireAuthentication(creds, "test", true);
  +
  +        PostMethod post = new PostMethod("/");
  +        post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
  +        try {
  +            this.httpclient.executeMethod(post);
  +            assertEquals(HttpStatus.SC_OK, post.getStatusCode());
  +            assertNotNull(post.getResponseBodyAsString());
  +        } finally {
  +            post.releaseConnection();
  +        }
  +    }
  +
   }
  
  
  
  1.6       +14 -3     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.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- HttpRequestHandlerChain.java	7 Nov 2004 12:31:42 -0000	1.5
  +++ HttpRequestHandlerChain.java	28 Nov 2004 15:44:39 -0000	1.6
  @@ -47,8 +47,17 @@
    * @author Christian Kohlschuetter
    */
   public class HttpRequestHandlerChain implements HttpRequestHandler {
  +    
       private List subhandlers = new ArrayList();
       
  +    public HttpRequestHandlerChain(final HttpRequestHandlerChain chain) {
  +        super();
  +        if (chain != null) {
  +            this.subhandlers.clear();
  +            this.subhandlers.addAll(chain.subhandlers);
  +        }
  +    }
  +    
       public HttpRequestHandlerChain() {
           super();
       }
  @@ -56,9 +65,11 @@
       public synchronized void clear() {
           subhandlers.clear();
       }
  +    
       public synchronized void prependHandler(HttpRequestHandler handler) {
           subhandlers.add(0,handler);
       }
  +    
       public synchronized void appendHandler(HttpRequestHandler handler) {
           subhandlers.add(handler);
       }
  
  
  
  1.12      +41 -12    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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- ProxyAuthRequestHandler.java	13 Nov 2004 12:21:28 -0000	1.11
  +++ ProxyAuthRequestHandler.java	28 Nov 2004 15:44:39 -0000	1.12
  @@ -46,7 +46,10 @@
    * @author Oleg Kalnichevski
    */
   public class ProxyAuthRequestHandler implements HttpRequestHandler {
  -    private Credentials credentials;
  +
  +    private Credentials credentials = null;
  +    private String realm = null;
  +    private boolean keepalive = true;
   
       /**
        * The proxy authenticate response header.
  @@ -59,10 +62,24 @@
        * 
        * @param creds
        */
  -    public ProxyAuthRequestHandler(Credentials creds) {
  +    public ProxyAuthRequestHandler(final Credentials creds, final String realm, boolean
keepalive) {
           if (creds == null)
  -            throw new IllegalArgumentException("Credentials can not be 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 ProxyAuthRequestHandler(final Credentials creds, final String realm) {
  +        this(creds, realm, true);
  +    }
  +    
  +    public ProxyAuthRequestHandler(final Credentials creds) {
  +        this(creds, null, true);
       }
   
       public boolean processRequest(
  @@ -70,10 +87,10 @@
           final SimpleRequest request) throws IOException
       {
           Header clientAuth = request.getFirstHeader(PROXY_AUTH_RESP);
  -        if (clientAuth != null) {
  -            return !checkAuthorization(clientAuth);
  +        if (clientAuth != null && checkAuthorization(clientAuth)) {
  +            return false;
           } else {
  -            SimpleResponse response = performBasicHandshake(request);
  +            SimpleResponse response = performBasicHandshake(conn, request);
               // Make sure the request body is fully consumed
               request.getBodyBytes();
               conn.writeResponse(response);
  @@ -82,13 +99,25 @@
       }
   
       //TODO add more auth schemes
  -    private SimpleResponse performBasicHandshake(final SimpleRequest request) { 
  +    private SimpleResponse performBasicHandshake(
  +            final SimpleHttpServerConnection conn, 
  +            final SimpleRequest request) { 
  +
           SimpleResponse response = new SimpleResponse();
           response.setStatusLine(
                   request.getRequestLine().getHttpVersion(),
                   HttpStatus.SC_PROXY_AUTHENTICATION_REQUIRED);
  -        response.addHeader(new Header("Proxy-Authenticate", "basic realm=test"));
  -        //response.addHeader(new Header("Proxy-Connection", "Close"));
  +        if (!request.getRequestLine().getMethod().equalsIgnoreCase("HEAD")) {
  +            response.setBodyString("unauthorized");
  +        }
  +        response.addHeader(new Header("Proxy-Authenticate", "basic realm=\"" + this.realm
+ "\""));
  +        if (this.keepalive) {
  +            response.addHeader(new Header("Proxy-Connection", "keep-alive"));
  +            conn.setKeepAlive(true);
  +        } else {
  +            response.addHeader(new Header("Proxy-Connection", "close"));
  +            conn.setKeepAlive(false);
  +        }
           return response;
       }
   
  
  
  
  1.7       +22 -16    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java
  
  Index: SimpleProxy.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/SimpleProxy.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SimpleProxy.java	13 Nov 2004 12:21:28 -0000	1.6
  +++ SimpleProxy.java	28 Nov 2004 15:44:39 -0000	1.7
  @@ -25,8 +25,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.server;
  @@ -44,18 +42,18 @@
   public class SimpleProxy extends SimpleHttpServer {
       
       private SimpleConnManager connmanager = null; 
  -    private HttpRequestHandlerChain chain = null;
  +    private HttpRequestHandlerChain stdchain = null;
   
       /**
        * @throws IOException
        */
  -    public SimpleProxy() throws IOException {
  -        super();
  +    public SimpleProxy(int port) throws IOException {
  +        super(port);
           this.connmanager = new SimpleConnManager(); 
  -        this.chain = new HttpRequestHandlerChain();
  -        this.chain.appendHandler(new TransparentProxyRequestHandler());
  -        this.chain.appendHandler(new ProxyRequestHandler(this.connmanager));
  -        setRequestHandler(this.chain);
  +        this.stdchain = new HttpRequestHandlerChain();
  +        this.stdchain.appendHandler(new TransparentProxyRequestHandler());
  +        this.stdchain.appendHandler(new ProxyRequestHandler(this.connmanager));
  +        setRequestHandler(this.stdchain);
       }
   
       /**
  @@ -63,12 +61,20 @@
        *                The local TCP port to listen on
        * @throws IOException
        */
  -    public SimpleProxy(int port) throws IOException {
  -        super(port);
  +    public SimpleProxy() throws IOException {
  +        this(0);
  +    }
  +
  +    public void requireAuthentication(final Credentials creds, final String realm, boolean
keepalive) {
  +        HttpRequestHandlerChain chain = new HttpRequestHandlerChain(this.stdchain); 
  +        chain.prependHandler(new ProxyAuthRequestHandler(creds ,realm, keepalive));
  +        setRequestHandler(chain);
       }
   
  -    public void requireCredentials(Credentials creds) {
  -        this.chain.prependHandler(new ProxyAuthRequestHandler(creds));
  +    public void requireAuthentication(final Credentials creds) {
  +        HttpRequestHandlerChain chain = new HttpRequestHandlerChain(this.stdchain); 
  +        chain.prependHandler(new ProxyAuthRequestHandler(creds));
  +        setRequestHandler(chain);
       }
   
       public void destroy() {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message