Revision: 593
          http://svn.sourceforge.net/jwebunit/?rev=593&view=rev
Author:   henryju
Date:     2006-10-28 03:00:41 -0700 (Sat, 28 Oct 2006)

Log Message:
-----------
Add support for Basic and Digest Authentication + unit tests.
Add support for NTLM and proxy Authentication (not tested).

Modified Paths:
--------------
    
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
    
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/JettySetup.java
    
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/WEB-INF/web.xml
    
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/TestContext.java
    
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
    branches/1.x/src/changes/changes.xml

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
   2006-10-28 09:58:45 UTC (rev 592)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/JWebUnitAPITestCase.java
   2006-10-28 10:00:41 UTC (rev 593)
@@ -45,6 +45,7 @@
        public void setUp() throws Exception {
                super.setUp();
                getTestContext().setBaseUrl(HOST_PATH);
+        getTestContext().setAuthorization("admin", "admin");
        }
 
     public void assertPassFail(String methodName, Object passArg, Object 
failArgs) throws Throwable {

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/JettySetup.java
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/JettySetup.java
       2006-10-28 09:58:45 UTC (rev 592)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/java/net/sourceforge/jwebunit/tests/util/JettySetup.java
       2006-10-28 10:00:41 UTC (rev 593)
@@ -11,6 +11,16 @@
 import org.mortbay.jetty.MimeTypes;
 import org.mortbay.jetty.Server;
 import org.mortbay.jetty.bio.SocketConnector;
+import org.mortbay.jetty.handler.DefaultHandler;
+import org.mortbay.jetty.handler.HandlerCollection;
+import org.mortbay.jetty.security.Authenticator;
+import org.mortbay.jetty.security.BasicAuthenticator;
+import org.mortbay.jetty.security.Constraint;
+import org.mortbay.jetty.security.ConstraintMapping;
+import org.mortbay.jetty.security.HashUserRealm;
+import org.mortbay.jetty.security.Password;
+import org.mortbay.jetty.security.SecurityHandler;
+import org.mortbay.jetty.security.UserRealm;
 import org.mortbay.jetty.webapp.WebAppContext;
 import org.mortbay.xml.XmlConfiguration;
 
@@ -27,64 +37,79 @@
  * @author Eelco Hillenius
  */
 public class JettySetup extends TestSetup {
-    /**
-     * The Jetty server we are going to use as test server.
-     */
-    private Server jettyServer = null;
-    
-    /**
-     * Constructor.
-     * 
-     * @param test
-     */
-    public JettySetup(Test test) {
-        super(test);
-    }
+       /**
+        * The Jetty server we are going to use as test server.
+        */
+       private Server jettyServer = null;
 
-    /**
-     * Starts the Jetty server using the <tt>jetty-test-config.xml</tt> file
-     * which is loaded using the classloader used to load Jetty.
-     * 
-     * @see junit.extensions.TestSetup#setUp()
-     */
-    public void setUp() {
-        try {
-            jettyServer = new Server();
-            SocketConnector connector = new SocketConnector();
-            connector.setPort(JWebUnitAPITestCase.JETTY_PORT);
-            jettyServer.setConnectors (new Connector[]{connector});
-            WebAppContext wah = new WebAppContext();
-            // Handle files encoded in UTF-8
-            MimeTypes mimeTypes = new MimeTypes();
-            mimeTypes.addMimeMapping("html_utf-8", "text/html; charset=UTF-8");
-            wah.setMimeTypes(mimeTypes);
-            
-            wah.setServer(jettyServer);
-            wah.setContextPath(JWebUnitAPITestCase.JETTY_URL);
-            URL url = this.getClass().getResource("/testcases/");
-            wah.setWar(url.toString());
-            jettyServer.addHandler(wah);
-            jettyServer.start();
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail("Could not start the Jetty server: " + e);
-        }
-    }
+       /**
+        * Constructor.
+        * 
+        * @param test
+        */
+       public JettySetup(Test test) {
+               super(test);
+       }
 
-    /**
-     * Stops the Jetty server.
-     * 
-     * @see junit.extensions.TestSetup#tearDown()
-     */
-    public void tearDown() {
-        try {
-            jettyServer.stop();
-        } catch (InterruptedException e) {
-            e.printStackTrace();
-            fail("Jetty server was interrupted: " + e);
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail("Could not stop the Jetty server: " + e);
-        }
-    }
+       /**
+        * Starts the Jetty server using the <tt>jetty-test-config.xml</tt> file
+        * which is loaded using the classloader used to load Jetty.
+        * 
+        * @see junit.extensions.TestSetup#setUp()
+        */
+       public void setUp() {
+               try {
+                       jettyServer = new Server();
+                       SocketConnector connector = new SocketConnector();
+                       connector.setPort(JWebUnitAPITestCase.JETTY_PORT);
+                       jettyServer.setConnectors(new Connector[] { connector 
});
+
+                       WebAppContext wah = new WebAppContext();
+                       
+                       // Handle files encoded in UTF-8
+                       MimeTypes mimeTypes = new MimeTypes();
+                       mimeTypes.addMimeMapping("html_utf-8", "text/html; 
charset=UTF-8");
+                       wah.setMimeTypes(mimeTypes);
+                       
+                       
+                       HandlerCollection handlers= new HandlerCollection();
+                       handlers.setHandlers(new Handler[]{wah, new 
DefaultHandler()});
+
+                       jettyServer.setHandler(wah);
+                       HashUserRealm myrealm = new HashUserRealm("MyRealm");
+                       myrealm.put("jetty", "jetty");
+                       myrealm.addUserToRole("jetty", "user");
+                       myrealm.put("admin", "admin");
+                       myrealm.addUserToRole("admin", "admin");
+                       jettyServer.setUserRealms(new UserRealm[]{myrealm});
+                       
+                       wah.setContextPath(JWebUnitAPITestCase.JETTY_URL);
+
+                       URL url = this.getClass().getResource("/testcases/");
+                       wah.setWar(url.toString());
+                       
+                       jettyServer.start();
+
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       fail("Could not start the Jetty server: " + e);
+               }
+       }
+
+       /**
+        * Stops the Jetty server.
+        * 
+        * @see junit.extensions.TestSetup#tearDown()
+        */
+       public void tearDown() {
+               try {
+                       jettyServer.stop();
+               } catch (InterruptedException e) {
+                       e.printStackTrace();
+                       fail("Jetty server was interrupted: " + e);
+               } catch (Exception e) {
+                       e.printStackTrace();
+                       fail("Could not stop the Jetty server: " + e);
+               }
+       }
 }
\ No newline at end of file

Modified: 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/WEB-INF/web.xml
===================================================================
--- 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/WEB-INF/web.xml
    2006-10-28 09:58:45 UTC (rev 592)
+++ 
branches/1.x/jwebunit-commons-tests/src/main/resources/testcases/WEB-INF/web.xml
    2006-10-28 10:00:41 UTC (rev 593)
@@ -13,4 +13,22 @@
     <url-pattern>/params.jsp</url-pattern>
 </servlet-mapping>
 
+<security-constraint>
+   <web-resource-collection>
+     <web-resource-name>A Protected Page</web-resource-name>
+     <url-pattern>/*</url-pattern>
+   </web-resource-collection>
+
+   <auth-constraint>
+     <role-name>admin</role-name>
+     <role-name>user</role-name>
+     <role-name>moderator</role-name>
+   </auth-constraint>
+</security-constraint>
+
+  <login-config>
+   <auth-method>DIGEST</auth-method>
+   <realm-name>MyRealm</realm-name>
+  </login-config>
+
 </web-app>
\ No newline at end of file

Modified: 
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/TestContext.java
===================================================================
--- 
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/TestContext.java
  2006-10-28 09:58:45 UTC (rev 592)
+++ 
branches/1.x/jwebunit-core/src/main/java/net/sourceforge/jwebunit/TestContext.java
  2006-10-28 10:00:41 UTC (rev 593)
@@ -6,7 +6,6 @@
 
 import javax.servlet.http.Cookie;
 
-import java.io.UnsupportedEncodingException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -19,18 +18,24 @@
  * 
  * @author Wilkes Joiner
  * @author Jim Weaver
+ * @author Julien Henry
  */
 public class TestContext {
        private String user;
        private String passwd;
+       private String domain;
        private List cookies;
-       private boolean hasAuth;
+       private boolean hasAuth = false;
+    private boolean hasNTLMAuth = false;
        private Locale locale = Locale.getDefault();
        private String resourceBundleName;
        private String baseUrl = "http://localhost:8080";;
        private String userAgent;
-       private String proxyName;
-       private int proxyPort = 80;
+       private String proxyUser;
+       private String proxyPasswd;
+       private String proxyHost;
+       private int proxyPort;
+       private boolean hasProxyAuth = false;
 
        /**
         * Construct a test client context.
@@ -40,9 +45,7 @@
        }
 
        /**
-        * Set authentication information for the test context. This 
information is
-        * used by [EMAIL PROTECTED] HttpUnitDialog}to set authorization on the
-        * WebConversation when the dialog is begun.
+        * Set basic authentication information for the test context.
         * 
         * @param user
         *            user name
@@ -55,6 +58,39 @@
                hasAuth = true;
        }
 
+    /**
+     * Set NTLM authentication information for the test context.
+     * 
+     * @param user
+     *            user name
+     * @param passwd
+     *            password
+     */
+    public void setNTLMAuthorization(String user, String passwd, String 
domain) {
+        this.user = user;
+        this.passwd = passwd;
+        this.domain = domain;
+        hasNTLMAuth = true;
+    }
+    
+    /**
+     * Set proxy authentication information for the test context.
+     * 
+     * @param user
+     *            user name
+     * @param passwd
+     *            password
+     * @param host proxy host name (null if applicable to any host).
+     * @param port proxy port (negative if applicable to any port).
+     */
+    public void setProxyAuthorization(String user, String passwd, String host, 
int port) {
+        this.proxyUser = user;
+        this.proxyPasswd = passwd;
+        this.proxyHost = host;
+        this.proxyPort = port;
+        hasProxyAuth = true;
+    }
+
        /**
         * Add a cookie to the test context. These cookies are set on the
         * WebConversation when an [EMAIL PROTECTED] HttpUnitDialog}is begun.
@@ -69,14 +105,30 @@
        }
 
        /**
-        * Return true if a user / password has been set on the context via
+        * Return true if a basic authentication has been set on the context via
         * [EMAIL PROTECTED] #setAuthorization}.
         */
        public boolean hasAuthorization() {
                return hasAuth;
        }
 
-       /**
+    /**
+     * Return true if a NTLM authentication has been set on the context via
+     * [EMAIL PROTECTED] #setNTLMAuthorization}.
+     */
+    public boolean hasNTLMAuthorization() {
+        return hasNTLMAuth;
+    }
+
+    /**
+     * Return true if a proxy authentication has been set on the context via
+     * [EMAIL PROTECTED] #setProxyAuthorization}.
+     */
+    public boolean hasProxyAuthorization() {
+        return hasProxyAuth;
+    }
+
+    /**
         * Return true if one or more cookies have been added to the test 
context.
         */
        public boolean hasCookies() {
@@ -96,6 +148,13 @@
        public String getPassword() {
                return passwd;
        }
+    
+       /**
+     * Return the user domain.
+     */
+    public String getDomain() {
+        return domain;
+    }
 
        /**
         * Return the cookies which have been added to the test context.
@@ -150,39 +209,31 @@
        }
 
        /**
-        * Return the proxy server name Contributed by Jack Chen
+        * Return the proxy server name
         */
-       public String getProxyName() {
-               return proxyName;
+       public String getProxyHost() {
+               return proxyHost;
        }
 
        /**
-        * Set the proxy server name for the test context. Contributed by Jack 
Chen
+        * Return the proxy server port
         */
-       public void setProxyName(String proxyName) {
-               this.proxyName = proxyName;
-       }
-
-       /**
-        * Return the proxy server port Contributed by Jack Chen
-        */
        public int getProxyPort() {
                return proxyPort;
        }
 
        /**
-        * Set the proxy server port for the test context. Contributed by Jack 
Chen
+        * Return the proxy user name
         */
-       public void setProxyPort(int proxyPort) {
-               this.proxyPort = proxyPort;
+       public String getProxyUser() {
+               return proxyUser;
        }
 
        /**
-        * Return true if a proxy name is set [EMAIL PROTECTED] #setProxyName}. 
Contributed
-        * by Jack Chen
+        * Return the proxy password
         */
-       public boolean hasProxy() {
-               return proxyName != null && proxyName.trim().length() > 0;
+       public String getProxyPasswd() {
+               return proxyPasswd;
        }
 
        /**

Modified: 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
===================================================================
--- 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
   2006-10-28 09:58:45 UTC (rev 592)
+++ 
branches/1.x/jwebunit-htmlunit-plugin/src/main/java/net/sourceforge/jwebunit/htmlunit/HtmlUnitDialog.java
   2006-10-28 10:00:41 UTC (rev 593)
@@ -11,7 +11,9 @@
 
 import java.io.IOException;
 import java.net.ConnectException;
+import java.net.InetAddress;
 import java.net.URL;
+import java.net.UnknownHostException;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Iterator;
@@ -33,6 +35,7 @@
 
 import com.gargoylesoftware.htmlunit.AlertHandler;
 import com.gargoylesoftware.htmlunit.BrowserVersion;
+import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
 import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
 import com.gargoylesoftware.htmlunit.Page;
 import com.gargoylesoftware.htmlunit.UnexpectedPage;
@@ -494,6 +497,30 @@
                                "4.0", testContext.getUserAgent(), "1.2", 6));
                wc.setJavaScriptEnabled(jsEnabled);
                wc.setThrowExceptionOnScriptError(true);
+               DefaultCredentialsProvider creds = new 
DefaultCredentialsProvider();
+               if (getTestContext().hasAuthorization()) {
+                       creds.addCredentials(getTestContext().getUser(), 
getTestContext()
+                                       .getPassword());
+               }
+               if (getTestContext().hasNTLMAuthorization()) {
+                       InetAddress netAddress;
+                       String address;
+                       try {
+                               netAddress = InetAddress.getLocalHost();
+                               address = netAddress.getHostName();
+                       } catch (UnknownHostException e) {
+                               address = "";
+                       }
+                       creds.addNTLMCredentials(getTestContext().getUser(),
+                                       getTestContext().getPassword(), "", -1, 
address,
+                                       getTestContext().getDomain());
+               }
+               if (getTestContext().hasProxyAuthorization()) {
+                       
creds.addProxyCredentials(getTestContext().getProxyUser(),
+                                       getTestContext().getPassword(), 
getTestContext()
+                                                       .getProxyHost(), 
getTestContext().getProxyPort());
+               }
+               wc.setCredentialsProvider(creds);
                wc.addWebWindowListener(new WebWindowListener() {
                        public void webWindowClosed(WebWindowEvent event) {
                                if 
(event.getOldPage().equals(win.getEnclosedPage())) {

Modified: branches/1.x/src/changes/changes.xml
===================================================================
--- branches/1.x/src/changes/changes.xml        2006-10-28 09:58:45 UTC (rev 
592)
+++ branches/1.x/src/changes/changes.xml        2006-10-28 10:00:41 UTC (rev 
593)
@@ -8,7 +8,11 @@
     </properties>
     <body>
         <release version="1.4" date="UNKNOW">
-             <action type="fix" dev="Julien Henry" due-to="Joe Athman" 
issue="1554148">
+            <action type="add" dev="Julien Henry">
+                Add support for Basic and Digest Authentication using 
Context.setAuthorization().
+                Context.setNTMLAuthorization() and 
Context.setProxyAuthorization may also work (not tested).
+            </action>
+            <action type="fix" dev="Julien Henry" due-to="Joe Athman" 
issue="1554148">
                 beginAt and gotoPage now accept absolute URL beginning with 
"http://";, "https://"; or "www."
             </action>
             <action type="add" dev="Julien Henry">


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Jwebunit-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jwebunit-development

Reply via email to