mbecke      2004/11/08 20:25:05

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestWebapp.java
               httpclient/src/test/org/apache/commons/httpclient/server
                        HttpServiceHandler.java
               httpclient/src/test/org/apache/commons/httpclient/auth
                        TestBasicAuth.java
  Removed:     httpclient/src/test/org/apache/commons/httpclient
                        TestWebappBasicAuth.java
  Log:
  Ported more test to the SimpleHttpServer.
  
  Revision  Changes    Path
  1.13      +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.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- TestWebapp.java   6 Oct 2004 03:39:58 -0000       1.12
  +++ TestWebapp.java   9 Nov 2004 04:25:05 -0000       1.13
  @@ -64,7 +64,6 @@
           suite.addTest(TestWebappMethods.suite());
           suite.addTest(TestWebappParameters.suite());
           suite.addTest(TestWebappHeaders.suite());
  -        suite.addTest(TestWebappBasicAuth.suite());
           suite.addTest(TestWebappPostMethod.suite());
           suite.addTest(TestWebappNoncompliant.suite());
           suite.addTest(TestProxy.suite());
  
  
  
  1.7       +8 -4      
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpServiceHandler.java
  
  Index: HttpServiceHandler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/server/HttpServiceHandler.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- HttpServiceHandler.java   7 Nov 2004 19:57:08 -0000       1.6
  +++ HttpServiceHandler.java   9 Nov 2004 04:25:05 -0000       1.7
  @@ -118,7 +118,11 @@
                   }
               }
           }
  -        conn.writeResponse(response);
  +        if ("HEAD".equalsIgnoreCase(request.getRequestLine().getMethod())) {
  +            // this is a head request, we don't want to send the actualy 
content
  +            response.setBodyEntity(null);
  +        }
  +         conn.writeResponse(response);
           return true;
       }
       
  
  
  
  1.8       +131 -7    
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.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- TestBasicAuth.java        7 Nov 2004 12:31:42 -0000       1.7
  +++ TestBasicAuth.java        9 Nov 2004 04:25:05 -0000       1.8
  @@ -44,6 +44,10 @@
   import org.apache.commons.httpclient.ProxyTestDecorator;
   import org.apache.commons.httpclient.UsernamePasswordCredentials;
   import org.apache.commons.httpclient.methods.GetMethod;
  +import org.apache.commons.httpclient.methods.HeadMethod;
  +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;
  @@ -119,11 +123,17 @@
                   return true;
               }
               response.setStatusLine(ver, HttpStatus.SC_OK);
  -            response.setBodyString("Authorization successful");
  +            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() {
  @@ -232,6 +242,32 @@
           }
       }
   
  +    public void testBasicAuthenticationWithNoCredsRetry() throws IOException 
{
  +        this.server.setHttpService(new BasicAuthService());
  +        GetMethod httpget = new GetMethod("/test/");
  +        try {
  +            this.client.executeMethod(httpget);
  +            assertNotNull(httpget.getStatusLine());
  +            assertEquals(HttpStatus.SC_UNAUTHORIZED, 
httpget.getStatusLine().getStatusCode());
  +            AuthState authstate = httpget.getHostAuthState();
  +            assertNotNull(authstate.getAuthScheme());
  +            assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
  +            assertEquals("test", authstate.getRealm());
  +        } finally {
  +            httpget.releaseConnection();
  +        }
  +        // now try with credentials
  +        httpget = new GetMethod("/test/");
  +        try {
  +            this.client.getState().setCredentials(AuthScope.ANY, new 
UsernamePasswordCredentials("test", "test"));
  +            this.client.executeMethod(httpget);
  +            assertNotNull(httpget.getStatusLine());
  +            assertEquals(HttpStatus.SC_OK, 
httpget.getStatusLine().getStatusCode());
  +        } finally {
  +            httpget.releaseConnection();
  +        }
  +    }
  +    
       public void testBasicAuthenticationWithNoRealm() {
           String challenge = "Basic";
           try {
  @@ -470,7 +506,6 @@
           assertEquals(expected, auth.getValue());
       }
   
  -
       public void testCustomAuthorizationHeader() throws Exception {
           String authResponse = "Basic " + EncodingUtil.getAsciiString(
               Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test")));
  @@ -485,4 +520,93 @@
           assertNotNull(httpget.getStatusLine());
           assertEquals(HttpStatus.SC_OK, 
httpget.getStatusLine().getStatusCode());
       }
  +    
  +    public void testHeadBasicAuthentication() 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 BasicAuthService());
  +        HeadMethod head = new HeadMethod("/test/");
  +        try {
  +            this.client.executeMethod(head);
  +        } finally {
  +            head.releaseConnection();
  +        }
  +        assertNotNull(head.getStatusLine());
  +        assertEquals(HttpStatus.SC_OK, head.getStatusLine().getStatusCode());
  +        Header auth = head.getRequestHeader("Authorization");
  +        assertNotNull(auth);
  +        String expected = "Basic " + EncodingUtil.getAsciiString(
  +            Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test")));
  +        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 {
  +        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 BasicAuthService());
  +        PostMethod post = new PostMethod("/test/");
  +        post.setRequestEntity(new StringRequestEntity("Test body"));
  +        try {
  +            this.client.executeMethod(post);
  +            assertEquals("Test body", post.getResponseBodyAsString());
  +        } finally {
  +            post.releaseConnection();
  +        }
  +        assertNotNull(post.getStatusLine());
  +        assertEquals(HttpStatus.SC_OK, post.getStatusLine().getStatusCode());
  +        Header auth = post.getRequestHeader("Authorization");
  +        assertNotNull(auth);
  +        String expected = "Basic " + EncodingUtil.getAsciiString(
  +            Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test")));
  +        assertEquals(expected, auth.getValue());
  +        AuthState authstate = post.getHostAuthState();
  +        assertNotNull(authstate.getAuthScheme());
  +        assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
  +        assertEquals("test", authstate.getRealm());
  +    }
  +    
  +    public void testPutBasicAuthentication() 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 BasicAuthService());
  +        PutMethod put = new PutMethod("/test/");
  +        put.setRequestEntity(new StringRequestEntity("Test body"));
  +        try {
  +            this.client.executeMethod(put);
  +            assertEquals("Test body", put.getResponseBodyAsString());
  +        } finally {
  +            put.releaseConnection();
  +        }
  +        assertNotNull(put.getStatusLine());
  +        assertEquals(HttpStatus.SC_OK, put.getStatusLine().getStatusCode());
  +        Header auth = put.getRequestHeader("Authorization");
  +        assertNotNull(auth);
  +        String expected = "Basic " + EncodingUtil.getAsciiString(
  +            Base64.encodeBase64(EncodingUtil.getAsciiBytes("test:test")));
  +        assertEquals(expected, auth.getValue());
  +        AuthState authstate = put.getHostAuthState();
  +        assertNotNull(authstate.getAuthScheme());
  +        assertTrue(authstate.getAuthScheme() instanceof BasicScheme);
  +        assertEquals("test", authstate.getRealm());
  +    }
  +    
   }
  
  
  

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

Reply via email to