olegk       2004/11/20 11:35:30

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestProxy.java
  Log:
  Test coverage for proxied methods + connection persistance + interactive 
authentication
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.9       +272 -10   
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.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- TestProxy.java    20 Nov 2004 17:56:39 -0000      1.8
  +++ TestProxy.java    20 Nov 2004 19:35:30 -0000      1.9
  @@ -27,7 +27,10 @@
    */
   package org.apache.commons.httpclient;
   
  +import org.apache.commons.httpclient.auth.AuthScheme;
   import org.apache.commons.httpclient.auth.AuthScope;
  +import org.apache.commons.httpclient.auth.CredentialsNotAvailableException;
  +import org.apache.commons.httpclient.auth.CredentialsProvider;
   import org.apache.commons.httpclient.methods.GetMethod;
   import org.apache.commons.httpclient.methods.PostMethod;
   import org.apache.commons.httpclient.methods.StringRequestEntity;
  @@ -46,6 +49,7 @@
    * Tests for proxied connections.
    * 
    * @author Ortwin Glueck
  + * @author Oleg Kalnichevski
    */
   public class TestProxy extends TestCase {
   
  @@ -83,19 +87,108 @@
           this.httpserver = null;
           super.tearDown();
       }
  +
  +    class GetItWrongThenGetItRight implements CredentialsProvider {
  +        
  +        private int hostcount = 0;
  +        private int proxycount = 0;
  +        
  +        public GetItWrongThenGetItRight() {
  +            super();
  +        }
  +        
  +        public Credentials getCredentials(AuthScheme scheme, String host, 
int port, boolean proxy)
  +                throws CredentialsNotAvailableException {
  +            if (!proxy) {
  +                this.hostcount++;
  +                return provideCredentials(this.hostcount);
  +            } else {
  +                this.proxycount++;
  +                return provideCredentials(this.proxycount);
  +            }
  +        }
  +        
  +        private Credentials provideCredentials(int count) {
  +            switch (count) {
  +            case 1: 
  +                return new UsernamePasswordCredentials("testuser", 
"wrongstuff");
  +            case 2: 
  +                return new UsernamePasswordCredentials("testuser", 
"testpass");
  +            default:
  +                return null;
  +            }
  +        }
  +
  +    }
       
  +    /**
  +     * Tests GET via non-authenticating proxy
  +     */
       public void testSimpleGet() throws Exception {
           this.httpserver.setHttpService(new FeedbackService());
           GetMethod get = new GetMethod("/");
           try {
               this.httpclient.executeMethod(get);
  -            assertEquals(200, get.getStatusCode());
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +    
  +    /**
  +     * Tests GET via non-authenticating proxy + host auth + connection 
keep-alive 
  +     */
  +    public void testGetHostAuthConnKeepAlive() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", 
true));
  +        handlerchain.appendHandler(new HttpServiceHandler(new 
FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +    
  +    /**
  +     * Tests GET via non-authenticating proxy + host auth + connection close 
  +     */
  +    public void testGetHostAuthConnClose() throws Exception {
  +
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", 
false));
  +        handlerchain.appendHandler(new HttpServiceHandler(new 
FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
           } finally {
               get.releaseConnection();
           }
       }
   
  -    public void testAuthHostGet() throws Exception {
  +    /**
  +     * Tests GET via non-authenticating proxy + invalid host auth 
  +     */
  +    public void testGetHostInvalidAuth() throws Exception {
   
           UsernamePasswordCredentials creds = 
               new UsernamePasswordCredentials("testuser", "testpass");
  @@ -106,31 +199,145 @@
           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);
           
           GetMethod get = new GetMethod("/");
           try {
               this.httpclient.executeMethod(get);
  -            assertEquals(200, get.getStatusCode());
  +            assertEquals(HttpStatus.SC_UNAUTHORIZED, get.getStatusCode());
           } finally {
               get.releaseConnection();
           }
       }
  -    
  +
  +    /**
  +     * Tests GET via non-authenticating proxy + intercative host auth + 
connection keep-alive 
  +     */
  +    public void testGetInteractiveHostAuthConnKeepAlive() 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);
  +        
  +        GetMethod get = new GetMethod("/");
  +        try {
  +            this.httpclient.executeMethod(get);
  +            assertEquals(HttpStatus.SC_OK, get.getStatusCode());
  +        } finally {
  +            get.releaseConnection();
  +        }
  +    }
  +   
  +    /**
  +     * Tests GET via non-authenticating proxy + intercative host auth + 
connection close 
  +     */
  +    public void testGetInteractiveHostAuthConnClose() 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);
  +        
  +        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 {
           this.httpserver.setHttpService(new FeedbackService());
           PostMethod post = new PostMethod("/");
           post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
           try {
               this.httpclient.executeMethod(post);
  -            assertEquals(200, post.getStatusCode());
  +            assertEquals(HttpStatus.SC_OK, post.getStatusCode());
               assertNotNull(post.getResponseBodyAsString());
           } finally {
               post.releaseConnection();
           }
       }
   
  -    public void testAuthPost() throws Exception {
  +    /**
  +     * Tests POST via non-authenticating proxy + host auth + connection 
keep-alive 
  +     */
  +    public void testPostHostAuthConnKeepAlive() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", 
true));
  +        handlerchain.appendHandler(new HttpServiceHandler(new 
FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        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 + host auth + connection 
close 
  +     */
  +    public void testPostHostAuthConnClose() throws Exception {
  +        UsernamePasswordCredentials creds = 
  +            new UsernamePasswordCredentials("testuser", "testpass");
  +        
  +        this.httpclient.getState().setCredentials(AuthScope.ANY, creds);
  +        
  +        HttpRequestHandlerChain handlerchain = new HttpRequestHandlerChain();
  +        handlerchain.appendHandler(new AuthRequestHandler(creds, "test", 
false));
  +        handlerchain.appendHandler(new HttpServiceHandler(new 
FeedbackService()));
  +        
  +        this.httpserver.setRequestHandler(handlerchain);
  +        
  +        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 testPostHostInvalidAuth() throws Exception {
  +
           UsernamePasswordCredentials creds = 
               new UsernamePasswordCredentials("testuser", "testpass");
           
  @@ -140,17 +347,72 @@
           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);
  +        
  +        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 testPostInteractiveHostAuthConnKeepAlive() 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);
           
           PostMethod post = new PostMethod("/");
           post.setRequestEntity(new StringRequestEntity("Like tons of stuff"));
           try {
               this.httpclient.executeMethod(post);
  -            assertEquals(200, post.getStatusCode());
  +            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 testPostInteractiveHostAuthConnClose() 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);
  +        
  +        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();
  +        }
  +    }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to