You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cactus-dev@jakarta.apache.org by vm...@apache.org on 2002/01/13 22:21:17 UTC
cvs commit: jakarta-cactus/src/sample/share/org/apache/cactus/sample/unit TestServletTestCase_Authentication.java TestServletTestCase_OverrideRedirector.java
vmassol 02/01/13 13:21:17
Modified: conf/sample/build/servlet22 build-tests-tomcat-33.xml
conf/sample/build/servlet23 build-tests-tomcat-40.xml
conf/sample/conf/test/servlet22 cactus.properties web.xml
conf/sample/conf/test/servlet22/tomcat33 server.xml
conf/sample/conf/test/servlet23 cactus.properties web.xml
conf/sample/conf/test/servlet23/tomcat40 web.xml
docs/framework/xdocs changes.xml contributors.xml todo.xml
src/framework/servlet23/org/apache/cactus/client
FilterHttpClient.java
src/framework/share/org/apache/cactus WebRequest.java
src/framework/share/org/apache/cactus/client
AbstractHttpClient.java ClientConfiguration.java
HttpClientHelper.java JspHttpClient.java
ServletHttpClient.java
src/sample/servlet22/org/apache/cactus TestAll.java
src/sample/servlet23/org/apache/cactus TestAll.java
Added: conf/sample/conf/test/servlet22/tomcat33 tomcat-users.xml
src/framework/share/org/apache/cactus/client/authentication
AbstractAuthentication.java
BasicAuthentication.java
src/sample/share/org/apache/cactus/sample/unit
TestServletTestCase_Authentication.java
TestServletTestCase_OverrideRedirector.java
Log:
* Added support for per test case redirector (see changes.html)
* Added support for BASIC authentication (see changes.html) - Thanks to Jason Robertson and Peter Wong !
Revision Changes Path
1.5 +3 -5 jakarta-cactus/conf/sample/build/servlet22/build-tests-tomcat-33.xml
Index: build-tests-tomcat-33.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/build/servlet22/build-tests-tomcat-33.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- build-tests-tomcat-33.xml 3 Jan 2002 13:34:48 -0000 1.4
+++ build-tests-tomcat-33.xml 13 Jan 2002 21:21:16 -0000 1.5
@@ -95,11 +95,9 @@
<mkdir dir="${out.tomcat33.dir}/work"/>
<mkdir dir="${out.tomcat33.dir}/webapps"/>
- <copy file="${conf.tomcat33.dir}/server.xml"
- todir="${out.tomcat33.dir}/conf" filtering="on"/>
-
- <copy file="${conf.tomcat33.dir}/modules.xml"
- todir="${out.tomcat33.dir}/conf"/>
+ <copy todir="${out.tomcat33.dir}/conf" filtering="on">
+ <fileset dir="${conf.tomcat33.dir}"/>
+ </copy>
<!-- Copy the war file -->
<copy file="${out.test.dir}/test.war" tofile="${out.tomcat33.dir}/webapps/test.war"/>
1.5 +3 -6 jakarta-cactus/conf/sample/build/servlet23/build-tests-tomcat-40.xml
Index: build-tests-tomcat-40.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/build/servlet23/build-tests-tomcat-40.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- build-tests-tomcat-40.xml 2 Aug 2001 15:02:31 -0000 1.4
+++ build-tests-tomcat-40.xml 13 Jan 2002 21:21:16 -0000 1.5
@@ -116,12 +116,9 @@
time -->
<delete dir="${out.tomcat40.dir}/webapps/test"/>
- <!-- Copy the default tomcat web.xml to our test conf/ directory. -->
- <copy file="${conf.tomcat40.dir}/web.xml"
- todir="${out.tomcat40.dir}/conf"/>
-
- <copy file="${conf.tomcat40.dir}/server.xml"
- todir="${out.tomcat40.dir}/conf" filtering="on"/>
+ <copy todir="${out.tomcat40.dir}/conf" filtering="on">
+ <fileset dir="${conf.tomcat40.dir}"/>
+ </copy>
<!-- Copy the war file -->
<copy file="${out.test.dir}/test.war" tofile="${out.tomcat40.dir}/webapps/test.war"/>
1.3 +7 -4 jakarta-cactus/conf/sample/conf/test/servlet22/cactus.properties
Index: cactus.properties
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/conf/test/servlet22/cactus.properties,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- cactus.properties 19 Oct 2001 23:06:34 -0000 1.2
+++ cactus.properties 13 Jan 2002 21:21:16 -0000 1.3
@@ -8,8 +8,11 @@
# You need to specify in these URLs the webapp context that you use for your
# application. In the example below, the context is "test".
-# Servlet Redirector. Used by ServletTestCase test cases.
-cactus.servletRedirectorURL = http://localhost:@test.port@/test/ServletRedirector
+# Web app Context under which our application to test runs
+cactus.contextURL = http://localhost:@test.port@/test
-# JSP Redirector. Used by JspTestCase test cases.
-cactus.jspRedirectorURL = http://localhost:@test.port@/test/JspRedirector
+# Default Servlet Redirector Name. Used by ServletTestCase test cases.
+cactus.servletRedirectorName = ServletRedirector
+
+# Default JSP Redirector Name. Used by JspTestCase test cases.
+cactus.jspRedirectorName = JspRedirector
1.5 +54 -0 jakarta-cactus/conf/sample/conf/test/servlet22/web.xml
Index: web.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/conf/test/servlet22/web.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- web.xml 19 Oct 2001 23:06:34 -0000 1.4
+++ web.xml 13 Jan 2002 21:21:16 -0000 1.5
@@ -16,6 +16,20 @@
</servlet>
<servlet>
+ <servlet-name>ServletRedirectorSecure</servlet-name>
+ <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
+ </servlet>
+
+ <servlet>
+ <servlet-name>ServletRedirector_TestOverride</servlet-name>
+ <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
+ <init-param>
+ <param-name>param2</param-name>
+ <param-value>value2 used for testing</param-value>
+ </init-param>
+ </servlet>
+
+ <servlet>
<servlet-name>JspRedirector</servlet-name>
<jsp-file>/test/jspRedirector.jsp</jsp-file>
<init-param>
@@ -30,8 +44,48 @@
</servlet-mapping>
<servlet-mapping>
+ <servlet-name>ServletRedirector_TestOverride</servlet-name>
+ <url-pattern>/ServletRedirectorOverride</url-pattern>
+ </servlet-mapping>
+
+ <servlet-mapping>
+ <servlet-name>ServletRedirectorSecure</servlet-name>
+ <url-pattern>/ServletRedirectorSecure</url-pattern>
+ </servlet-mapping>
+
+ <servlet-mapping>
<servlet-name>JspRedirector</servlet-name>
<url-pattern>/JspRedirector</url-pattern>
</servlet-mapping>
+
+ <!-- Authentication -->
+
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>SecurityRestriction</web-resource-name>
+ <description>Protect the Cactus redirector servlet.</description>
+ <url-pattern>/ServletRedirectorSecure</url-pattern>
+ <http-method>GET</http-method>
+ <http-method>POST</http-method>
+ </web-resource-collection>
+ <auth-constraint>
+ <description>Authorized Users Group</description>
+ <role-name>test</role-name>
+ </auth-constraint>
+ <user-data-constraint>
+ <transport-guarantee>NONE</transport-guarantee>
+ </user-data-constraint>
+ </security-constraint>
+
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ </login-config>
+
+ <security-role>
+ <description>Test role</description>
+ <role-name>test</role-name>
+ </security-role>
+
+ <!-- End Authentication -->
</web-app>
1.5 +2 -1 jakarta-cactus/conf/sample/conf/test/servlet22/tomcat33/server.xml
Index: server.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/conf/test/servlet22/tomcat33/server.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- server.xml 13 Jan 2002 15:52:43 -0000 1.4
+++ server.xml 13 Jan 2002 21:21:16 -0000 1.5
@@ -63,10 +63,11 @@
<CredentialsInterceptor />
+ <SimpleRealm filename="conf/tomcat-users.xml" />
+
<LoadOnStartupInterceptor />
<Servlet22Interceptor />
-
<!-- Request processing -->
1.1 jakarta-cactus/conf/sample/conf/test/servlet22/tomcat33/tomcat-users.xml
Index: tomcat-users.xml
===================================================================
<tomcat-users>
<user name="testuser" password="testpwd" roles="test" />
</tomcat-users>
1.4 +7 -6 jakarta-cactus/conf/sample/conf/test/servlet23/cactus.properties
Index: cactus.properties
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/conf/test/servlet23/cactus.properties,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- cactus.properties 19 Oct 2001 23:06:34 -0000 1.3
+++ cactus.properties 13 Jan 2002 21:21:16 -0000 1.4
@@ -8,13 +8,14 @@
# You need to specify in these URLs the webapp context that you use for your
# application. In the example below, the context is "test".
-# Servlet Redirector. Used by ServletTestCase test cases.
-cactus.servletRedirectorURL = http://localhost:@test.port@/test/ServletRedirector
+# Web app Context under which our application to test runs
+cactus.contextURL = http://localhost:@test.port@/test
-# JSP Redirector. Used by JspTestCase test cases.
-cactus.jspRedirectorURL = http://localhost:@test.port@/test/JspRedirector
+# Default Servlet Redirector Name. Used by ServletTestCase test cases.
+cactus.servletRedirectorName = ServletRedirector
-# Filter Redirector. Used by FilterTestCase test cases.
+# Default JSP Redirector Name. Used by JspTestCase test cases.
+cactus.jspRedirectorName = JspRedirector
# NOTE: Some servlet 2.3 containers need to map the filter to a valid
# resource (Orion 1.5.2 for example). Others, like Tomcat, are happy
@@ -23,4 +24,4 @@
# For example, the following would work with Tomcat :
# cactus.filterRedirectorURL = http://localhost:@test.port@/test/FilterRedirector
-cactus.filterRedirectorURL = http://localhost:@test.port@/test/test/filterRedirector.jsp
+cactus.filterRedirectorName = test/filterRedirector.jsp
1.5 +54 -0 jakarta-cactus/conf/sample/conf/test/servlet23/web.xml
Index: web.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/conf/test/servlet23/web.xml,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- web.xml 19 Oct 2001 23:06:34 -0000 1.4
+++ web.xml 13 Jan 2002 21:21:16 -0000 1.5
@@ -33,6 +33,20 @@
</servlet>
<servlet>
+ <servlet-name>ServletRedirectorSecure</servlet-name>
+ <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
+ </servlet>
+
+ <servlet>
+ <servlet-name>ServletRedirector_TestOverride</servlet-name>
+ <servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
+ <init-param>
+ <param-name>param2</param-name>
+ <param-value>value2 used for testing</param-value>
+ </init-param>
+ </servlet>
+
+ <servlet>
<servlet-name>JspRedirector</servlet-name>
<jsp-file>/test/jspRedirector.jsp</jsp-file>
<init-param>
@@ -47,8 +61,48 @@
</servlet-mapping>
<servlet-mapping>
+ <servlet-name>ServletRedirectorSecure</servlet-name>
+ <url-pattern>/ServletRedirectorSecure</url-pattern>
+ </servlet-mapping>
+
+ <servlet-mapping>
+ <servlet-name>ServletRedirector_TestOverride</servlet-name>
+ <url-pattern>/ServletRedirectorOverride</url-pattern>
+ </servlet-mapping>
+
+ <servlet-mapping>
<servlet-name>JspRedirector</servlet-name>
<url-pattern>/JspRedirector</url-pattern>
</servlet-mapping>
+
+ <!-- Authentication -->
+
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>SecurityRestriction</web-resource-name>
+ <description>Protect the Cactus redirector servlet.</description>
+ <url-pattern>/ServletRedirectorSecure</url-pattern>
+ <http-method>GET</http-method>
+ <http-method>POST</http-method>
+ </web-resource-collection>
+ <auth-constraint>
+ <description>Authorized Users Group</description>
+ <role-name>test</role-name>
+ </auth-constraint>
+ <user-data-constraint>
+ <transport-guarantee>NONE</transport-guarantee>
+ </user-data-constraint>
+ </security-constraint>
+
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ </login-config>
+
+ <security-role>
+ <description>Test role</description>
+ <role-name>test</role-name>
+ </security-role>
+
+ <!-- End Authentication -->
</web-app>
1.2 +0 -26 jakarta-cactus/conf/sample/conf/test/servlet23/tomcat40/web.xml
Index: web.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/conf/sample/conf/test/servlet23/tomcat40/web.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- web.xml 30 Jun 2001 18:33:51 -0000 1.1
+++ web.xml 13 Jan 2002 21:21:17 -0000 1.2
@@ -676,31 +676,5 @@
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
- <!-- ================ Security Constraints for Testing =============== -->
-
-<!--
- <security-constraint>
- <web-resource-collection>
- <web-resource-name>The Entire Web Application</web-resource-name>
- <url-pattern>/*</url-pattern>
- </web-resource-collection>
- <auth-constraint>
- <role-name>tomcat</role-name>
- </auth-constraint>
- </security-constraint>
-
- <login-config>
- <auth-method>BASIC</auth-method>
- <realm-name>Tomcat Supported Realm</realm-name>
- </login-config>
-
- <security-role>
- <description>
- An example role defined in "conf/tomcat-users.xml"
- </description>
- <role-name>tomcat</role-name>
- </security-role>
--->
-
</web-app>
1.77 +24 -0 jakarta-cactus/docs/framework/xdocs/changes.xml
Index: changes.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/docs/framework/xdocs/changes.xml,v
retrieving revision 1.76
retrieving revision 1.77
diff -u -r1.76 -r1.77
--- changes.xml 13 Jan 2002 15:52:43 -0000 1.76
+++ changes.xml 13 Jan 2002 21:21:17 -0000 1.77
@@ -127,6 +127,30 @@
</devs>
<release version="1.3 in CVS">
+ <action dev="VMA" type="add">
+ Added support for BASCI authentication. Thanks to
+ <link href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</link>
+ and
+ <link href="mailto:pwong@sct.com">Peter Wong</link>. See class
+ <code>TestServletTestCase_Authentication</code> in the sample unit
+ tests for an example on how to use it.
+ </action>
+ <action dev="VMA" type="add">
+ Added <code>WebRequest.setRedirectorName(String redirectorName)</code>
+ to override default <code>cactus.servletRedirectorName</code> parameter
+ that you define in <code>cactus.properties</code>. This lets you
+ define a redirector per test case if you wish. It is useful when you
+ want to test some servlet methods that are secured and others that are
+ not, for example.
+ </action>
+ <action dev="VMA" type="update">
+ Modified <code>cactus.properties</code> configuration parameters. For
+ example, where you previously were writing
+ <code>cactus.servletRedirectorURL = http://localhost:8080/test/ServletRedirector</code>
+ you would now have 2 parameters :
+ <code>cactus.contextURL = http://localhost:8080/test</code> and
+ <code>cactus.servletRedirectorName = ServletRedirector</code>.
+ </action>
<action dev="VMA" type="fix" due-to="Balki Nakshatrala" due-to-email="Balki@Yodlee.com">
Cactus is now working again with JDK 1.2. However, be aware that most
versions of new Servlet containers need JDK 1.3 and above.
1.13 +6 -0 jakarta-cactus/docs/framework/xdocs/contributors.xml
Index: contributors.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/docs/framework/xdocs/contributors.xml,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- contributors.xml 9 Jan 2002 04:59:35 -0000 1.12
+++ contributors.xml 13 Jan 2002 21:21:17 -0000 1.13
@@ -55,6 +55,12 @@
<li>
<link href="mailto:ndlesiecki@apache.org">Nicholas Lesiecki</link>
</li>
+ <li>
+ <link href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</link>
+ </li>
+ <li>
+ <link href="mailto:pwong@sct.com">Peter Wong</link>
+ </li>
</ul>
</s1>
1.72 +4 -4 jakarta-cactus/docs/framework/xdocs/todo.xml
Index: todo.xml
===================================================================
RCS file: /home/cvs/jakarta-cactus/docs/framework/xdocs/todo.xml,v
retrieving revision 1.71
retrieving revision 1.72
diff -u -r1.71 -r1.72
--- todo.xml 17 Dec 2001 13:01:26 -0000 1.71
+++ todo.xml 13 Jan 2002 21:21:17 -0000 1.72
@@ -32,10 +32,10 @@
<version title="Version 1.3">
<category title="Design/Code">
- <action assigned-to="Peter Wong, Vincent Massol">
- Support unit testing of Servlet code that uses Security APIs, such as
- <code>isUserInRole()</code>, ... Review and Commit the code submitted by
- Peter Wong on the mailing-list.
+ <action assigned-to="Peter Wong, Jason Robertson, Vincent Massol">
+ Add support for Form-based authentication + configure all supported
+ containers with authentication so that they pass the Cactus unit
+ tests (done for Tomcat 3.3).
</action>
</category>
1.5 +19 -3 jakarta-cactus/src/framework/servlet23/org/apache/cactus/client/FilterHttpClient.java
Index: FilterHttpClient.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/framework/servlet23/org/apache/cactus/client/FilterHttpClient.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- FilterHttpClient.java 13 Jan 2002 15:01:57 -0000 1.4
+++ FilterHttpClient.java 13 Jan 2002 21:21:17 -0000 1.5
@@ -53,22 +53,38 @@
*/
package org.apache.cactus.client;
+import org.apache.cactus.WebRequest;
+
/**
* Manage the logic for calling the Servlet redirector for executing a test on
* the server side.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
- * @version $Id: FilterHttpClient.java,v 1.4 2002/01/13 15:01:57 vmassol Exp $
+ * @version $Id: FilterHttpClient.java,v 1.5 2002/01/13 21:21:17 vmassol Exp $
*/
public class FilterHttpClient extends AbstractHttpClient
{
/**
+ * Return the redirector URL to connect to.
+ *
+ * @param theRequest Request data from the user. We need it here as the user
+ * may have chosen to override the default redirector.
* @return the URL to call the redirector
*/
- protected String getRedirectorURL()
+ protected String getRedirectorURL(WebRequest theRequest)
{
- return ClientConfiguration.getFilterRedirectorURL();
+ String url;
+
+ // Check if user has overriden the servlet redirector
+ if (theRequest.getRedirectorName() != null) {
+ url = ClientConfiguration.getContextURL() + "/" +
+ theRequest.getRedirectorName();
+ } else {
+ url = ClientConfiguration.getFilterRedirectorURL();
+ }
+
+ return url;
}
}
1.11 +54 -0 jakarta-cactus/src/framework/share/org/apache/cactus/WebRequest.java
Index: WebRequest.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/WebRequest.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- WebRequest.java 13 Jan 2002 15:07:08 -0000 1.10
+++ WebRequest.java 13 Jan 2002 21:21:17 -0000 1.11
@@ -57,6 +57,7 @@
import java.io.*;
import org.apache.cactus.util.*;
+import org.apache.cactus.client.authentication.AbstractAuthentication;
/**
* Contains all HTTP request data for a test case. It is the data that
@@ -78,11 +79,14 @@
* session for you or not,</li>
* <li>Whether you want the HTTP connection to the server redirector to
* use a POST or GET method. Default is POST</li>
+ * <li>Authentication to use (optional)</li>
+ * <li>Content type (optional)</li>
* </ul>
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
+ * @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
*
- * @version $Id: WebRequest.java,v 1.10 2002/01/13 15:07:08 vmassol Exp $
+ * @version $Id: WebRequest.java,v 1.11 2002/01/13 21:21:17 vmassol Exp $
*/
public class WebRequest
{
@@ -135,6 +139,56 @@
* The content type to set in the http request
*/
private String contentType = "application/x-www-form-urlencoded";
+
+ /**
+ * The Authentication Object that will configure the http request
+ */
+ private AbstractAuthentication authentication;
+
+ /**
+ * Redirector Name. This is to let the user the possibility to override
+ * the default Redirector Name specified in <code>cactus.properties</code>.
+ */
+ private String redirectorName;
+
+ /**
+ * Override the redirector Name defined in <code>cactus.properties</code>.
+ * This is useful to define a per test case Name (for example, if some
+ * test case need to have authentication turned on and not other tests,
+ * etc).
+ *
+ * @param theRedirectorName the new redirector Name to use
+ */
+ public void setRedirectorName(String theRedirectorName)
+ {
+ this.redirectorName = theRedirectorName;
+ }
+
+ /**
+ * @return the overriden redirector Name or null if none has been defined
+ */
+ public String getRedirectorName()
+ {
+ return this.redirectorName;
+ }
+
+ /**
+ * Sets the authentication object that will configure the http request
+ *
+ * @param theAuthenticationObject the authentication object
+ */
+ public void setAuthentication(AbstractAuthentication theAuthenticationObject)
+ {
+ this.authentication = theAuthenticationObject;
+ }
+
+ /**
+ * @return the authentication object that will configure the http request
+ */
+ public AbstractAuthentication getAuthentication()
+ {
+ return this.authentication;
+ }
/**
* Sets the content type that will be set in the http request
1.13 +38 -18 jakarta-cactus/src/framework/share/org/apache/cactus/client/AbstractHttpClient.java
Index: AbstractHttpClient.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/client/AbstractHttpClient.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- AbstractHttpClient.java 13 Jan 2002 15:08:35 -0000 1.12
+++ AbstractHttpClient.java 13 Jan 2002 21:21:17 -0000 1.13
@@ -53,12 +53,18 @@
*/
package org.apache.cactus.client;
-import java.util.*;
-import java.net.*;
-import java.io.*;
-
-import org.apache.cactus.*;
-import org.apache.cactus.util.log.*;
+import java.util.PropertyResourceBundle;
+import java.util.ResourceBundle;
+import java.net.HttpURLConnection;
+import java.io.ObjectInputStream;
+
+import org.apache.cactus.util.log.LogService;
+import org.apache.cactus.util.log.Log;
+import org.apache.cactus.client.authentication.AbstractAuthentication;
+import org.apache.cactus.WebTestResult;
+import org.apache.cactus.WebRequest;
+import org.apache.cactus.ServiceEnumeration;
+import org.apache.cactus.ServiceDefinition;
/**
* Abstract class for performing the steps necessary to run a test. It involves
@@ -66,8 +72,9 @@
* stream and then opening a second HTTP connection to retrieve the test result.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
+ * @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
*
- * @version $Id: AbstractHttpClient.java,v 1.12 2002/01/13 15:08:35 vmassol Exp $
+ * @version $Id: AbstractHttpClient.java,v 1.13 2002/01/13 21:21:17 vmassol Exp $
*/
public abstract class AbstractHttpClient
{
@@ -89,9 +96,13 @@
PropertyResourceBundle.getBundle(CONFIG_NAME);
/**
+ * Return the redirector URL to connect to.
+ *
+ * @param theRequest Request data from the user. We need it here as the user
+ * may have chosen to override the default redirector.
* @return the URL to call the redirector
*/
- protected abstract String getRedirectorURL();
+ protected abstract String getRedirectorURL(WebRequest theRequest);
/**
* Calls the test method indirectly by calling the Redirector servlet and
@@ -114,7 +125,7 @@
HttpURLConnection connection = callRunTest(theRequest);
// Open the second connection to get the test results
- WebTestResult result = callGetResult();
+ WebTestResult result = callGetResult(theRequest.getAuthentication());
// Check if the returned result object returned contains an error or
// not. If yes, we need to raise an exception so that the JUnit
@@ -168,15 +179,16 @@
*/
private HttpURLConnection callRunTest(WebRequest theRequest) throws Throwable
{
- // Open the first connection to the redirector to execute the test on
- // the server side
- HttpClientHelper helper =
- new HttpClientHelper(getRedirectorURL());
-
// Specify the service to call on the redirector side
theRequest.addParameter(ServiceDefinition.SERVICE_NAME_PARAM,
ServiceEnumeration.CALL_TEST_SERVICE.toString(),
WebRequest.GET_METHOD);
+
+ // Open the first connection to the redirector to execute the test on
+ // the server side
+ HttpClientHelper helper =
+ new HttpClientHelper(getRedirectorURL(theRequest));
+
HttpURLConnection connection = helper.connect(theRequest);
// Wrap the connection to ensure that all servlet output is read
@@ -192,21 +204,29 @@
/**
* Get the test result from the redirector.
*
+ * @param theAuthentication Authentication object used to authenticate
+ * the user when securing Redirectors for testing security /
+ * authentication code. Can be null if security is not enabled for
+ * the redirector.
* @return the result that was returned by the redirector.
*
* @exception Throwable if an error occured in the test method or in the
* redirector servlet.
*/
- private WebTestResult callGetResult() throws Throwable
+ private WebTestResult callGetResult(
+ AbstractAuthentication theAuthentication) throws Throwable
{
- // Open the second connection to get the test results
- HttpClientHelper helper =
- new HttpClientHelper(getRedirectorURL());
-
+ // Add authentication details
WebRequest resultsRequest = new WebRequest();
resultsRequest.addParameter(ServiceDefinition.SERVICE_NAME_PARAM,
ServiceEnumeration.GET_RESULTS_SERVICE.toString(),
WebRequest.GET_METHOD);
+ resultsRequest.setAuthentication(theAuthentication);
+
+ // Open the second connection to get the test results
+ HttpClientHelper helper =
+ new HttpClientHelper(getRedirectorURL(resultsRequest));
+
HttpURLConnection resultConnection = helper.connect(resultsRequest);
// Read the results as a serialized object
1.4 +18 -5 jakarta-cactus/src/framework/share/org/apache/cactus/client/ClientConfiguration.java
Index: ClientConfiguration.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/client/ClientConfiguration.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ClientConfiguration.java 13 Jan 2002 15:08:35 -0000 1.3
+++ ClientConfiguration.java 13 Jan 2002 21:21:17 -0000 1.4
@@ -66,7 +66,7 @@
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
- * @version $Id: ClientConfiguration.java,v 1.3 2002/01/13 15:08:35 vmassol Exp $
+ * @version $Id: ClientConfiguration.java,v 1.4 2002/01/13 21:21:17 vmassol Exp $
*/
public class ClientConfiguration
{
@@ -122,7 +122,17 @@
}
return CONFIG;
}
-
+
+ /**
+ * @return the context URL under which our application to test runs.
+ * @throw MissingResourceException if the property defining the context
+ * is missing.
+ */
+ public static String getContextURL()
+ {
+ return getConfiguration().getString("cactus.contextURL");
+ }
+
/**
* @return the Servlet redirector
* @throw MissingResourceException if the property defining the servlet
@@ -130,7 +140,8 @@
*/
public static String getServletRedirectorURL()
{
- return getConfiguration().getString("cactus.servletRedirectorURL");
+ return getContextURL() + "/" +
+ getConfiguration().getString("cactus.servletRedirectorName");
}
/**
@@ -140,7 +151,8 @@
*/
public static String getJspRedirectorURL()
{
- return getConfiguration().getString("cactus.jspRedirectorURL");
+ return getContextURL() + "/" +
+ getConfiguration().getString("cactus.jspRedirectorName");
}
/**
@@ -150,7 +162,8 @@
*/
public static String getFilterRedirectorURL()
{
- return getConfiguration().getString("cactus.filterRedirectorURL");
+ return getContextURL() + "/" +
+ getConfiguration().getString("cactus.filterRedirectorName");
}
}
1.18 +25 -10 jakarta-cactus/src/framework/share/org/apache/cactus/client/HttpClientHelper.java
Index: HttpClientHelper.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/client/HttpClientHelper.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- HttpClientHelper.java 13 Jan 2002 15:08:35 -0000 1.17
+++ HttpClientHelper.java 13 Jan 2002 21:21:17 -0000 1.18
@@ -53,24 +53,33 @@
*/
package org.apache.cactus.client;
-import java.util.*;
-import java.net.*;
-import java.io.*;
-
-import junit.framework.*;
-import org.apache.commons.httpclient.*;
-
-import org.apache.cactus.*;
-import org.apache.cactus.util.log.*;
-import org.apache.cactus.util.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLEncoder;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.PrintWriter;
+import java.io.IOException;
+import java.util.Enumeration;
+import java.util.Vector;
+
+import org.apache.commons.httpclient.Header;
+
+import org.apache.cactus.util.log.Log;
+import org.apache.cactus.util.log.LogService;
+import org.apache.cactus.client.authentication.AbstractAuthentication;
+import org.apache.cactus.WebRequest;
+import org.apache.cactus.ServletURL;
/**
* Helper class to open an HTTP connection to the server redirector and pass
* to it HTTP parameters, Cookies and HTTP headers.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
+ * @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
*
- * @version $Id: HttpClientHelper.java,v 1.17 2002/01/13 15:08:35 vmassol Exp $
+ * @version $Id: HttpClientHelper.java,v 1.18 2002/01/13 21:21:17 vmassol Exp $
*/
public class HttpClientHelper
{
@@ -139,6 +148,12 @@
// Sets the content type
connection.setRequestProperty("Content-type",
theRequest.getContentType());
+
+ // Add Authentication headers, if necessary
+ AbstractAuthentication authentication = theRequest.getAuthentication();
+ if (authentication != null) {
+ authentication.configure(connection);
+ }
// Add the other header fields
addHeaders(theRequest, connection);
1.11 +19 -3 jakarta-cactus/src/framework/share/org/apache/cactus/client/JspHttpClient.java
Index: JspHttpClient.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/client/JspHttpClient.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- JspHttpClient.java 13 Jan 2002 15:08:35 -0000 1.10
+++ JspHttpClient.java 13 Jan 2002 21:21:17 -0000 1.11
@@ -53,22 +53,38 @@
*/
package org.apache.cactus.client;
+import org.apache.cactus.WebRequest;
+
/**
* Manage the logic for calling the JSP redirector for executing a test on
* the server side.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
- * @version $Id: JspHttpClient.java,v 1.10 2002/01/13 15:08:35 vmassol Exp $
+ * @version $Id: JspHttpClient.java,v 1.11 2002/01/13 21:21:17 vmassol Exp $
*/
public class JspHttpClient extends AbstractHttpClient
{
/**
+ * Return the redirector URL to connect to.
+ *
+ * @param theRequest Request data from the user. We need it here as the user
+ * may have chosen to override the default redirector.
* @return the URL to call the redirector
*/
- protected String getRedirectorURL()
+ protected String getRedirectorURL(WebRequest theRequest)
{
- return ClientConfiguration.getJspRedirectorURL();
+ String url;
+
+ // Check if user has overriden the servlet redirector
+ if (theRequest.getRedirectorName() != null) {
+ url = ClientConfiguration.getContextURL() + "/" +
+ theRequest.getRedirectorName();
+ } else {
+ url = ClientConfiguration.getJspRedirectorURL();
+ }
+
+ return url;
}
}
1.11 +19 -3 jakarta-cactus/src/framework/share/org/apache/cactus/client/ServletHttpClient.java
Index: ServletHttpClient.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/framework/share/org/apache/cactus/client/ServletHttpClient.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ServletHttpClient.java 13 Jan 2002 15:08:35 -0000 1.10
+++ ServletHttpClient.java 13 Jan 2002 21:21:17 -0000 1.11
@@ -53,22 +53,38 @@
*/
package org.apache.cactus.client;
+import org.apache.cactus.WebRequest;
+
/**
* Manage the logic for calling the Servlet redirector for executing a test on
* the server side.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
- * @version $Id: ServletHttpClient.java,v 1.10 2002/01/13 15:08:35 vmassol Exp $
+ * @version $Id: ServletHttpClient.java,v 1.11 2002/01/13 21:21:17 vmassol Exp $
*/
public class ServletHttpClient extends AbstractHttpClient
{
/**
+ * Return the redirector URL to connect to.
+ *
+ * @param theRequest Request data from the user. We need it here as the user
+ * may have chosen to override the default redirector.
* @return the URL to call the redirector
*/
- protected String getRedirectorURL()
+ protected String getRedirectorURL(WebRequest theRequest)
{
- return ClientConfiguration.getServletRedirectorURL();
+ String url;
+
+ // Check if user has overriden the servlet redirector
+ if (theRequest.getRedirectorName() != null) {
+ url = ClientConfiguration.getContextURL() + "/" +
+ theRequest.getRedirectorName();
+ } else {
+ url = ClientConfiguration.getServletRedirectorURL();
+ }
+
+ return url;
}
}
1.7 +3 -1 jakarta-cactus/src/sample/servlet22/org/apache/cactus/TestAll.java
Index: TestAll.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/sample/servlet22/org/apache/cactus/TestAll.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- TestAll.java 13 Jan 2002 15:29:38 -0000 1.6
+++ TestAll.java 13 Jan 2002 21:21:17 -0000 1.7
@@ -61,7 +61,7 @@
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
- * @version $Id: TestAll.java,v 1.6 2002/01/13 15:29:38 vmassol Exp $
+ * @version $Id: TestAll.java,v 1.7 2002/01/13 21:21:17 vmassol Exp $
*/
public class TestAll extends TestCase
{
@@ -114,6 +114,8 @@
suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase4.suite());
suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase5.suite());
suite.addTest(org.apache.cactus.sample.unit.TestServletTestCaseSpecific.suite());
+ suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase_OverrideRedirector.suite());
+ suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase_Authentication.suite());
// suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase_HttpUnit.suite());
// JspTestCase tests
1.6 +3 -1 jakarta-cactus/src/sample/servlet23/org/apache/cactus/TestAll.java
Index: TestAll.java
===================================================================
RCS file: /home/cvs/jakarta-cactus/src/sample/servlet23/org/apache/cactus/TestAll.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- TestAll.java 13 Jan 2002 15:30:20 -0000 1.5
+++ TestAll.java 13 Jan 2002 21:21:17 -0000 1.6
@@ -61,7 +61,7 @@
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
- * @version $Id: TestAll.java,v 1.5 2002/01/13 15:30:20 vmassol Exp $
+ * @version $Id: TestAll.java,v 1.6 2002/01/13 21:21:17 vmassol Exp $
*/
public class TestAll extends TestCase
{
@@ -114,6 +114,8 @@
suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase4.suite());
suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase5.suite());
suite.addTest(org.apache.cactus.sample.unit.TestServletTestCaseSpecific.suite());
+ suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase_OverrideRedirector.suite());
+ suite.addTest(org.apache.cactus.sample.unit.TestServletTestCase_Authentication.suite());
// JspTestCase tests
suite.addTest(org.apache.cactus.sample.unit.TestJspTestCase.suite());
1.1 jakarta-cactus/src/framework/share/org/apache/cactus/client/authentication/AbstractAuthentication.java
Index: AbstractAuthentication.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.cactus.client.authentication;
import java.net.HttpURLConnection;
/**
* This class was designed with the simple assumption that ALL authentication
* implementations will have a String <code>UserId</code> and a string
* <code>Password</code>. Two abstract functions <code>validateUserId</code> and
* <code>validatePassword</code> provide for concrete implementations to
* perform character validation. All the work is then done in the
* <code>configure</code> abstract function. In the
* <code>BasicAuthentication</code> class, for example, the configuring is done
* by adding the request property "Authorization" with a value
* "Basic <base64encode of 'userid:password'>".
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
* @author <a href="mailto:Jason.Robertson@acs-inc.com">Jason Robertson</a>
*
* @version $Id: AbstractAuthentication.java,v 1.1 2002/01/13 21:21:17 vmassol Exp $
*/
public abstract class AbstractAuthentication
{
/**
* User id part of the Credential
*/
protected String userid;
/**
* Password part of the Credential
*/
protected String password;
public AbstractAuthentication(String theUserid, String thePassword)
{
setUserId(theUserid);
setPassword(thePassword);
}
public void setUserId(String theUserid)
{
validateUserId(theUserid);
this.userid = theUserid;
}
public String getUserId()
{
return this.userid;
}
public void setPassword(String thePassword)
{
validatePassword(thePassword);
this.password = thePassword;
}
/**
* @exception IllegalArgumentException if invalid
*/
protected abstract void validateUserId(String theUserid);
/**
* @exception IllegalArgumentException if invalid
*/
protected abstract void validatePassword(String thePassword);
public abstract void configure(HttpURLConnection theConnection);
}
1.1 jakarta-cactus/src/framework/share/org/apache/cactus/client/authentication/BasicAuthentication.java
Index: BasicAuthentication.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.cactus.client.authentication;
import java.net.HttpURLConnection;
import java.text.CharacterIterator;
import java.text.StringCharacterIterator;
public class BasicAuthentication extends AbstractAuthentication
{
public BasicAuthentication(String userid, String password)
{
super(userid, password);
}
protected void validateUserId(String userid)
{
// According to HTTP 1.0 Spec:
// userid = [ token ]
// token = 1*<any CHAR except CTLs or tspecials>
// CTL = <any US-ASCII control character (octets 0-31) and DEL (127)
// tspecial = "(" | ")" | "<" | ">" | "@"
// "," | ";" | ":" | "\" | <">
// "/" | "[" | "]" | "?" | "="
// "{" | "}" | SP | HT
// SP = <US-ASCII SP, space (32)>
// HT = <US-ASCII HT, horizontal-tab (9)>
// Validate the given userid
// The userid is optional, it can be blank.
if (userid == null)
{
return;
}
// If it's non-blank, there is no maximum length
// and it can't contain any illegal characters
String illegalChars = "()<>@,;:\\\"/[]?={} \t";
StringCharacterIterator iter = new StringCharacterIterator(userid);
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
if ((illegalChars.indexOf(c) != -1) ||
((c >=0 ) && (c <= 31)) ||
(c == 127)) {
// Bad userid! Go to your room!
throw new IllegalArgumentException("Given userid contains illegal characters.");
}
}
}
protected void validatePassword(String password)
{
// According to HTTP 1.0 Spec:
// password = *TEXT
// TEXT = <any OCTET except CTLs, but including LWS>
// OCTET = <any 8-bit sequence of data>
// CTL = <any US-ASCII control character (octets 0-31) and DEL (127)
// LWS = [CRLF] 1*( SP | HT )
// CRLF = CR LF
// CR = <US-ASCII CR, carriage return (13)>
// LF = <US-ASCII LF, linefeed (10)>
// SP = <US-ASCII SP, space (32)>
// HT = <US-ASCII HT, horizontal-tab (9)>
// Validate the given password
// The password can have zero characters, i.e. be blank.
if (password == null)
{
return;
}
// If it's non-blank, there is no maximum length
// and it can't contain any illegal characters
String exceptionChars = "\r\n \t"; // CR LF SP HT
StringCharacterIterator iter = new StringCharacterIterator(password);
for (char c = iter.first(); c != CharacterIterator.DONE; c = iter.next()) {
if (((c >=0 ) && (c <= 31)) || (c == 127)) {
if (exceptionChars.indexOf(c) != -1 )
{
continue;
}
// Bad password! Go to your room!
throw new IllegalArgumentException("Given password contains illegal characters.");
}
}
}
public void configure(HttpURLConnection connection)
{
// According to HTTP 1.0 Spec:
// basic-credentials = "Basic" SP basic-cookie
// basic-cookie = <base64 encoding of userid-password,
// except not limited to 76 char/line>
// userid-password = [ token ] ":" *TEXT
//
// see setUserId and setPassword for details of token and TEXT
String basicCookie = userid + ":" + password;
String basicCredentials = "Basic " + new String(base64Encode(basicCookie.getBytes()));
connection.setRequestProperty("Authorization", basicCredentials);
}
// Base64 code - is there common code somewhere I should be using???
/**
* Provides encoding of raw bytes to base64-encoded characters, and
* decoding of base64 characters to raw bytes.
*
* @author Kevin Kelley (kelley@iguana.ruralnet.net)
* @version 1.0
* @date 06 August 1998
*/
static private char[] alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=".toCharArray();
//
// lookup table for converting base64 characters to value in range 0..63
//
static private byte[] codes = new byte[256];
static
{
for (int i=0; i<256; i++) codes[i] = -1;
for (int i = 'A'; i <= 'Z'; i++) codes[i] = (byte)( i - 'A');
for (int i = 'a'; i <= 'z'; i++) codes[i] = (byte)(26 + i - 'a');
for (int i = '0'; i <= '9'; i++) codes[i] = (byte)(52 + i - '0');
codes['+'] = 62;
codes['/'] = 63;
}
/**
* returns an array of base64-encoded characters to represent the
* passed data array.
*
* @param data the array of bytes to encode
* @return base64-coded character array.
*/
static private char[] base64Encode(byte[] data)
{
char[] out = new char[((data.length + 2) / 3) * 4];
//
// 3 bytes encode to 4 chars. Output is always an even
// multiple of 4 characters.
//
for (int i=0, index=0; i<data.length; i+=3, index+=4)
{
boolean quad = false;
boolean trip = false;
int val = (0xFF & (int) data[i]);
val <<= 8;
if ((i+1) < data.length)
{
val |= (0xFF & (int) data[i+1]);
trip = true;
}
val <<= 8;
if ((i+2) < data.length)
{
val |= (0xFF & (int) data[i+2]);
quad = true;
}
out[index+3] = alphabet[(quad? (val & 0x3F): 64)];
val >>= 6;
out[index+2] = alphabet[(trip? (val & 0x3F): 64)];
val >>= 6;
out[index+1] = alphabet[val & 0x3F];
val >>= 6;
out[index+0] = alphabet[val & 0x3F];
}
return out;
}
}
1.1 jakarta-cactus/src/sample/share/org/apache/cactus/sample/unit/TestServletTestCase_Authentication.java
Index: TestServletTestCase_Authentication.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.cactus.sample.unit;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.apache.cactus.ServletTestCase;
import org.apache.cactus.WebRequest;
import org.apache.cactus.client.authentication.BasicAuthentication;
/**
* Some Cactus unit tests for testing Authentication support.
*
* These tests should not really be part of the sample application functional
* tests as they are unit tests for Cactus. However, they are unit tests that
* need a servlet environment running for their execution, so they have been
* package here for convenience. They can also be read by end-users to
* understand how Cactus work.
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
* @version $Id: TestServletTestCase_Authentication.java,v 1.1 2002/01/13 21:21:17 vmassol Exp $
*/
public class TestServletTestCase_Authentication extends ServletTestCase
{
/**
* Defines the testcase name for JUnit.
*
* @param theName the testcase's name.
*/
public TestServletTestCase_Authentication(String theName)
{
super(theName);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs)
{
junit.ui.TestRunner.main(new String[] {
TestServletTestCase_Authentication.class.getName()});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestServletTestCase_Authentication.class);
}
//-------------------------------------------------------------------------
/**
* Verify basic authentication.
*/
public void beginBasicAuthentication(WebRequest theRequest)
{
theRequest.setRedirectorName("ServletRedirectorSecure");
theRequest.setAuthentication(
new BasicAuthentication("testuser", "testpwd"));
}
/**
* Verify basic authentication.
*/
public void testBasicAuthentication()
{
assertEquals("testuser", request.getUserPrincipal().getName());
assertEquals("testuser", request.getRemoteUser());
assert("User not in 'test' role", request.isUserInRole("test"));
}
}
1.1 jakarta-cactus/src/sample/share/org/apache/cactus/sample/unit/TestServletTestCase_OverrideRedirector.java
Index: TestServletTestCase_OverrideRedirector.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001-2002 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Cactus", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.cactus.sample.unit;
import java.util.*;
import java.text.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.io.*;
import junit.framework.*;
import org.apache.cactus.*;
import org.apache.cactus.client.ClientConfiguration;
/**
* Cactus unit tests for testing that it is possible to override a servlet
* redirector as defined in <code>cactus.properties</code> on a per test case
* basis.
*
* These tests should not really be part of the sample application functional
* tests as they are unit tests for Cactus. However, they are unit tests that
* need a servlet environment running for their execution, so they have been
* package here for convenience. They can also be read by end-users to
* understand how Cactus work.
* <br><br>
*
* @author <a href="mailto:vmassol@apache.org">Vincent Massol</a>
*
* @version $Id: TestServletTestCase_OverrideRedirector.java,v 1.1 2002/01/13 21:21:17 vmassol Exp $
*/
public class TestServletTestCase_OverrideRedirector extends ServletTestCase
{
/**
* Defines the testcase name for JUnit.
*
* @param theName the testcase's name.
*/
public TestServletTestCase_OverrideRedirector(String theName)
{
super(theName);
}
/**
* Start the tests.
*
* @param theArgs the arguments. Not used
*/
public static void main(String[] theArgs)
{
junit.ui.TestRunner.main(new String[] {
TestServletTestCase_OverrideRedirector.class.getName()});
}
/**
* @return a test suite (<code>TestSuite</code>) that includes all methods
* starting with "test"
*/
public static Test suite()
{
// All methods starting with "test" will be executed in the test suite.
return new TestSuite(TestServletTestCase_OverrideRedirector.class);
}
//-------------------------------------------------------------------------
public void beginRedirectorOverride1(WebRequest theRequest)
{
theRequest.setRedirectorName("ServletRedirectorOverride");
}
public void testRedirectorOverride1()
{
assertEquals("value2 used for testing", config.getInitParameter("param2"));
}
//-------------------------------------------------------------------------
public void beginRedirectorOverride2(WebRequest theRequest)
{
theRequest.setRedirectorName("ServletRedirector");
}
public void testRedirectorOverride2()
{
assertEquals("value1 used for testing", config.getInitParameter("param1"));
}
//-------------------------------------------------------------------------
public void testRedirectorOverride3()
{
assertEquals("value1 used for testing", config.getInitParameter("param1"));
}
}
--
To unsubscribe, e-mail: <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>