[WAGON-347] Support preemtive authentication : add unit for that
git-svn-id: https://svn.apache.org/repos/asf/maven/wagon/branches/wagon-1.x@1172862 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/pom.xml b/pom.xml
index 842b15e..8831310 100644
--- a/pom.xml
+++ b/pom.xml
@@ -215,6 +215,10 @@
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.8</version>
</plugin>
+ <plugin>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <version>2.9</version>
+ </plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<configuration>
diff --git a/wagon-provider-test/src/main/java/org/apache/maven/wagon/http/HttpWagonTestCase.java b/wagon-provider-test/src/main/java/org/apache/maven/wagon/http/HttpWagonTestCase.java
index 76b4979..93f1ac9 100644
--- a/wagon-provider-test/src/main/java/org/apache/maven/wagon/http/HttpWagonTestCase.java
+++ b/wagon-provider-test/src/main/java/org/apache/maven/wagon/http/HttpWagonTestCase.java
@@ -19,22 +19,6 @@
* under the License.
*/
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.net.URLDecoder;
-import java.util.Enumeration;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-import java.util.zip.GZIPOutputStream;
-
-import javax.servlet.ServletException;
-import javax.servlet.ServletInputStream;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
import org.apache.maven.wagon.ConnectionException;
import org.apache.maven.wagon.FileTestUtils;
import org.apache.maven.wagon.ResourceDoesNotExistException;
@@ -54,6 +38,7 @@
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.HttpConnection;
import org.mortbay.jetty.Request;
+import org.mortbay.jetty.Response;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.AbstractHandler;
import org.mortbay.jetty.handler.HandlerCollection;
@@ -65,6 +50,23 @@
import org.mortbay.jetty.servlet.DefaultServlet;
import org.mortbay.jetty.servlet.ServletHolder;
+import javax.servlet.ServletException;
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.net.URLDecoder;
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.zip.GZIPOutputStream;
+
/**
* @version $Id: LightweightHttpWagonTest.java 680764 2008-07-29 16:45:51Z brett $
*/
@@ -143,7 +145,8 @@
addConnectors( server );
server.start();
- wagon.connect( new Repository( "id", getProtocol() + "://localhost:" + server.getConnectors()[0].getLocalPort() ) );
+ wagon.connect(
+ new Repository( "id", getProtocol() + "://localhost:" + server.getConnectors()[0].getLocalPort() ) );
wagon.getToStream( "resource", new StringOutputStream() );
@@ -602,25 +605,6 @@
return server;
}
- protected SecurityHandler createSecurityHandler()
- {
- Constraint constraint = new Constraint();
- constraint.setName( Constraint.__BASIC_AUTH );
- constraint.setRoles( new String[] { "admin" } );
- constraint.setAuthenticate( true );
-
- ConstraintMapping cm = new ConstraintMapping();
- cm.setConstraint( constraint );
- cm.setPathSpec( "/*" );
-
- SecurityHandler sh = new SecurityHandler();
- HashUserRealm hashUserRealm = new HashUserRealm( "MyRealm" );
- hashUserRealm.put( "user", "secret" );
- hashUserRealm.addUserToRole( "user", "admin" );
- sh.setUserRealm( hashUserRealm );
- sh.setConstraintMappings( new ConstraintMapping[] { cm } );
- return sh;
- }
private String writeTestFileGzip( File parent, String child )
throws IOException
@@ -766,12 +750,12 @@
String localRepositoryPath = FileTestUtils.getTestOutputDir().toString();
Server server = new Server( 0 );
- SecurityHandler sh = createSecurityHandler();
+ TestSecurityHandler sh = createSecurityHandler();
PutHandler handler = new PutHandler( new File( localRepositoryPath ) );
HandlerCollection handlers = new HandlerCollection();
- handlers.setHandlers( new Handler[] { sh, handler } );
+ handlers.setHandlers( new Handler[]{ sh, handler } );
server.setHandler( handlers );
addConnectors( server );
@@ -805,6 +789,7 @@
assertEquals( "put top secret", FileUtils.fileRead( sourceFile.getAbsolutePath() ) );
assertEquals( 1, handler.fileUploaded );
+ testPreemptiveAuthentication( sh );
}
finally
{
@@ -812,6 +797,27 @@
}
}
+ protected abstract boolean supportPreemptiveAuthentication();
+
+ protected void testPreemptiveAuthentication( TestSecurityHandler sh )
+ {
+
+ if ( supportPreemptiveAuthentication() )
+ {
+ assertEquals( "not 1 security handler use " + sh.securityHandlerRequestReponses, 1,
+ sh.securityHandlerRequestReponses.size() );
+ assertEquals( 200, sh.securityHandlerRequestReponses.get( 0 ).responseCode );
+ }
+ else
+ {
+ assertEquals( "not 2 security handler use " + sh.securityHandlerRequestReponses, 2,
+ sh.securityHandlerRequestReponses.size() );
+ assertEquals( 401, sh.securityHandlerRequestReponses.get( 0 ).responseCode );
+ assertEquals( 200, sh.securityHandlerRequestReponses.get( 1 ).responseCode );
+
+ }
+ }
+
static class StatusHandler
extends AbstractHandler
{
@@ -922,4 +928,69 @@
}
}
+
+ protected TestSecurityHandler createSecurityHandler()
+ {
+ Constraint constraint = new Constraint();
+ constraint.setName( Constraint.__BASIC_AUTH );
+ constraint.setRoles( new String[]{ "admin" } );
+ constraint.setAuthenticate( true );
+
+ ConstraintMapping cm = new ConstraintMapping();
+ cm.setConstraint( constraint );
+ cm.setPathSpec( "/*" );
+
+ TestSecurityHandler sh = new TestSecurityHandler();
+ HashUserRealm hashUserRealm = new HashUserRealm( "MyRealm" );
+ hashUserRealm.put( "user", "secret" );
+ hashUserRealm.addUserToRole( "user", "admin" );
+ sh.setUserRealm( hashUserRealm );
+ sh.setConstraintMappings( new ConstraintMapping[]{ cm } );
+ return sh;
+ }
+
+ public static class TestSecurityHandler
+ extends SecurityHandler
+ {
+
+ public List<SecurityHandlerRequestReponse> securityHandlerRequestReponses =
+ new ArrayList<SecurityHandlerRequestReponse>();
+
+ @Override
+ public void handle( String target, HttpServletRequest request, HttpServletResponse response, int dispatch )
+ throws IOException, ServletException
+ {
+ String method = request.getMethod();
+ super.handle( target, request, response, dispatch );
+ System.out.println( "method in SecurityHandler: " + method );
+
+ securityHandlerRequestReponses.add(
+ new SecurityHandlerRequestReponse( method, ( (Response) response ).getStatus() ) );
+ }
+
+ }
+
+ public static class SecurityHandlerRequestReponse
+ {
+ public String method;
+
+ public int responseCode;
+
+ private SecurityHandlerRequestReponse( String method, int responseCode )
+ {
+ this.method = method;
+ this.responseCode = responseCode;
+ }
+
+ @Override
+ public String toString()
+ {
+ final StringBuilder sb = new StringBuilder();
+ sb.append( "SecurityHandlerRequestReponse" );
+ sb.append( "{method='" ).append( method ).append( '\'' );
+ sb.append( ", responseCode=" ).append( responseCode );
+ sb.append( '}' );
+ return sb.toString();
+ }
+ }
}
diff --git a/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java b/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java
index 7b0813c..1c2ccda 100644
--- a/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java
+++ b/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonTest.java
@@ -135,4 +135,10 @@
System.getProperties().remove( "http.nonProxyHosts" );
}
}
+
+ @Override
+ protected boolean supportPreemptiveAuthentication()
+ {
+ return false;
+ }
}
diff --git a/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java b/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java
index 6fa4729..5c21e70 100644
--- a/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java
+++ b/wagon-providers/wagon-http-lightweight/src/test/java/org/apache/maven/wagon/providers/http/LightweightHttpWagonWithPreemptiveAuthenticationTest.java
@@ -34,4 +34,10 @@
wagon.setPreemptiveAuthentication( true );
return wagon;
}
+
+ @Override
+ protected boolean supportPreemptiveAuthentication()
+ {
+ return true;
+ }
}
diff --git a/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpWagonTest.java b/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpWagonTest.java
index e520bcf..412a2bb 100644
--- a/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpWagonTest.java
+++ b/wagon-providers/wagon-http/src/test/java/org/apache/maven/wagon/providers/http/HttpWagonTest.java
@@ -45,4 +45,10 @@
{
( (HttpWagon) wagon ).setHttpHeaders( properties );
}
+
+ @Override
+ protected boolean supportPreemptiveAuthentication()
+ {
+ return false;
+ }
}
diff --git a/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java b/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
index 03c8343..25cc22f 100644
--- a/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
+++ b/wagon-providers/wagon-webdav-jackrabbit/src/test/java/org/apache/maven/wagon/providers/webdav/WebDavWagonTest.java
@@ -296,4 +296,31 @@
tearDownWagonTestingFixtures();
}
+
+ @Override
+ protected boolean supportPreemptiveAuthentication()
+ {
+ return false;
+ }
+
+ protected void testPreemptiveAuthentication( TestSecurityHandler sh )
+ {
+
+ if ( supportPreemptiveAuthentication() )
+ {
+ assertEquals( "not 2 security handler use " + sh.securityHandlerRequestReponses, 2,
+ sh.securityHandlerRequestReponses.size() );
+ assertEquals( 200, sh.securityHandlerRequestReponses.get( 0 ).responseCode );
+ }
+ else
+ {
+ assertEquals( "not 4 security handler use " + sh.securityHandlerRequestReponses, 4,
+ sh.securityHandlerRequestReponses.size() );
+ assertEquals( 401, sh.securityHandlerRequestReponses.get( 0 ).responseCode );
+ assertEquals( 200, sh.securityHandlerRequestReponses.get( 1 ).responseCode );
+ assertEquals( 401, sh.securityHandlerRequestReponses.get( 2 ).responseCode );
+ assertEquals( 200, sh.securityHandlerRequestReponses.get( 3 ).responseCode );
+
+ }
+ }
}