[Httpunit-commit] CVS: httpunit/doc/tutorial build.xml,1.1,1.2 index.html,1.2,1.3 task1.html,1.2,1.3
Brought to you by:
russgold
|
From: Russell G. <rus...@us...> - 2001-11-27 16:40:50
|
Update of /cvsroot/httpunit/httpunit/doc/tutorial
In directory usw-pr-cvs1:/tmp/cvs-serv25123/doc/tutorial
Modified Files:
build.xml index.html task1.html task1.zip
task1editor-entry.html task1editor-form.html
task1editor-initial.html task1editor-validation.html
task2.html web.xml
Log Message:
Added security to servlet unit tutorial
Index: build.xml
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/build.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- build.xml 2001/11/12 20:43:51 1.1
+++ build.xml 2001/11/27 16:40:47 1.2
@@ -1,6 +1,6 @@
<?xml version="1.0" ?>
<!-- ======================================================================= -->
-<!-- httpunit tutorial build file -->
+<!-- httpunit tutorial build file -->
<!-- ======================================================================= -->
<project name="tutorial" default="test" basedir=".">
<property name="src.dir" value="src" />
Index: index.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/index.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- index.html 2001/11/26 14:19:26 1.2
+++ index.html 2001/11/27 16:40:47 1.3
@@ -57,7 +57,8 @@
<p>Each section of the tutorial will address a specific use of the system, and show how HttpUnit and ServletUnit can be used
to write tests which verify that functionality. You should begin each section by copying the initial directory, which includes
an ant build script and some classes that you will need to complete it. Running the ant script will compile the code and
-run the tests. After you implement each test, it should fail until you then add the implementation.</p>
+run the tests. If you are not using ant, compile and run tutorial.TutorialTestSuite. After you implement each test,
+it should fail until you then add the implementation.</p>
<p>Tasks</p>
Index: task1.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- task1.html 2001/11/26 14:19:26 1.2
+++ task1.html 2001/11/27 16:40:47 1.3
@@ -9,12 +9,21 @@
<p class="location"><a href="index.html">Tutorial</a>
<img src="arrow_yellow.gif" width=13 height=9 align=bottom ALT="->">Task 1</p>
<h1>Creating the Pool Editor</h1>
-<p>Our initial task will be the creation of the simple pool editor, following use case 1.1. We will ignore
-security restrictions that permit only the administrator to access this page.
+<p>Our initial task will be the creation of the simple pool editor, following use case 1.1. To restrict access to the
+administrator, we will take advantage of declarative security defined by the Servlet standard. As part of this task, we will
+use the Basic Authentication approach, which causes the browser to pop up a challenge dialog. The more common form-based
+authentication is handled the same way that all forms should be.</p>
+<p>
+To begin the tutorial, create a working directory and expand <a href="task1.zip">this archive</a> into it. To run the
+tutorial, you will need httpunit.jar, junit.jar, Tidy.jar, servlet.jar, and xerces.jar on your classpath. If you use
+<code>ant</code>, you can either copy
+them into the <code>jars</code> directory just created, or invoke ant with <code>-Dclasspath="..."</code>, specifying the locations
+of those jars between the quotation marks. If you do not use <code>ant</code>, note that the main class is named
+<code>tutorial.PoolEditorTest</code><p>
-To begin the tutorial, create a working directory and expand <a href="task1.zip">this archive</a> into it.
-Either create a jars directory and copy httpunit.jar, Tidy.jar, and xerces.jar into it, or make sure that they are on
-your classpath and invoke ant passing the classpath as a property.
-You will write your code in the src/tutorial sub-directory.
+<p>You will write your code in the src/tutorial sub-directory.</p>
-Once you are ready, proceed to <a href="task1editor-initial.html">step one</a> to begin the tutorial.
\ No newline at end of file
+<p>Once you are ready, proceed to <a href="task1editor-initial.html">step one</a> to begin the tutorial.</p>
+
+</body>
+</html>
\ No newline at end of file
Index: task1.zip
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1.zip,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
Binary files /tmp/cvsrQSXJs and /tmp/cvsalEVkL differ
Index: task1editor-entry.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1editor-entry.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- task1editor-entry.html 2001/11/26 14:19:26 1.2
+++ task1editor-entry.html 2001/11/27 16:40:47 1.3
@@ -39,6 +39,7 @@
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
@@ -83,6 +84,7 @@
<b>public void</b> testPoolEntry() <b>throws</b> Exception {
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
@@ -95,7 +97,7 @@
response = client.getResponse( request ); // (3) submit the form
form = response.getFormWithID( "pool" );
- assertEquals( "Away team 0", "", form.getParameterValue( "home0" ) ); // (4) verify the response
+ assertEquals( "Away team 0", "", form.getParameterValue( "away0" ) ); // (4) verify the response
assertEquals( "Home team 0", "", form.getParameterValue( "home0" ) );
assertEquals( "Away team 1", "Detroit Lions", form.getParameterValue( "away1" ) );
assertEquals( "Home team 1", "Denver Broncos", form.getParameterValue( "home1" ) );
@@ -126,7 +128,7 @@
pw.println( "</body></html>" );
}
-<b>private void</b> updateBettingPool( HttpServletRequest request ) {
+<b>void</b> updateBettingPool( HttpServletRequest request ) {
BettingPoolGame[] games = BettingPool.getGames();
<b>for</b> (int i = 0; i < games.length; i++) {
games[i].setAwayTeam( request.getParameter( "away" + i ) );
Index: task1editor-form.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1editor-form.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- task1editor-form.html 2001/11/26 14:19:26 1.2
+++ task1editor-form.html 2001/11/27 16:40:47 1.3
@@ -27,6 +27,7 @@
<b>public void</b> testFormAction() <b>throws</b> Exception {
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" ); // (1) obtain the desired form
@@ -59,6 +60,7 @@
<b>public void</b> testFormContents() <b>throws</b> Exception {
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
@@ -100,6 +102,7 @@
<b>public void</b> testSubmitButtons() <b>throws</b> Exception {
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
Index: task1editor-initial.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1editor-initial.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- task1editor-initial.html 2001/11/26 14:19:26 1.2
+++ task1editor-initial.html 2001/11/27 16:40:47 1.3
@@ -11,7 +11,10 @@
<img src="arrow_yellow.gif" width=13 height=9 align=bottom ALT="->"> Step 1: Invoking the pool editor</p>
<h1>Invoking the Pool Editor</h1>
-<p class="goals">In this step, you will learn how to:<br />• Initialize ServletUnit<br />• Invoke a servlet</p>
+<p class="goals">In this step, you will learn how to:<br />
+• Initialize ServletUnit<br />
+• Invoke a servlet<br />
+• Specify a username and password for basic authentication</p>
<p>The first step will simply be to verify that we can register
and access the servlet, which we will name <code>PoolEditorServlet</code>. A GET method to this page should return the editor form
itself, while updates will be handled by a POST method to the same address. Since we are working with servlets, we can
@@ -23,6 +26,7 @@
<b>import</b> com.meterware.httpunit.*;
<b>import</b> com.meterware.servletunit.*;
+<b>import</b> java.util.*;
<b>import</b> junit.framework.*;
<b>import</b> tutorial.persistence.*;
@@ -41,10 +45,17 @@
}
<b>public void</b> testGetForm() <b>throws</b> Exception {
- ServletRunner sr = <b>new</b> ServletRunner( "web.xml" ); // (1) use the web.xml file to define mappings
+ ServletRunner sr = <b>new</b> ServletRunner( "web.xml" ); // (1) use the web.xml file to define mappings
+ ServletUnitClient client = sr.newClient(); // (2) create a client to invoke the application
- ServletUnitClient client = sr.newClient(); // (2) create a client to invoke the application
- client.getResponse( "http://localhost/PoolEditor" ); // (3) invoke the servlet
+ try {
+ client.getResponse( "http://localhost/PoolEditor" ); // (3) invoke the servlet w/o authorization
+ fail( "PoolEditor is not protected" );
+ } catch (AuthorizationRequiredException e) { // (4) verify that access is denied
+ }
+
+ client.setAuthorization( "aUser", "pool-admin" ); // (5) specify authorization and
+ client.getResponse( "http://localhost/PoolEditor" ); // invoke the servlet again
}
}
@@ -56,7 +67,10 @@
The application is defined by an XML file which maps URL information to servlet classes.</li>
<li>Creating a client which can access the application and maintain state across multiple invocations.</li>
<li>Invoking the servlet via its URL. Note that ServletUnit ignores any host and port information. All URL patterns
-are treated as being relative to the root ("/").</li></ol></p>
+are treated as being relative to the root ("/").</li>
+<li>Catching an exception which indicates that authentication is required.</li>
+<li>Specifying the authorization information. ServletUnit does not maintain a database of users, no any username is
+accepted, and the password is interpreted as a comma-separated list of role names associated with the user.</li></ol></p>
<p>To run this code, you will also need the <a href="web.xml">web.xml</a> file in your current directory. This file
maps the request URL to the Pool Editor servlet.</p>
@@ -68,6 +82,7 @@
<b>package</b> tutorial;
<b>import</b> java.io.*;
+<b>import</b> java.util.*;
<b>import</b> javax.servlet.http.*;
<b>import</b> javax.servlet.ServletException;
Index: task1editor-validation.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task1editor-validation.html,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- task1editor-validation.html 2001/11/26 14:19:26 1.2
+++ task1editor-validation.html 2001/11/27 16:40:47 1.3
@@ -29,6 +29,7 @@
<b>public void</b> testPoolValidation() <b>throws</b> Exception {
ServletRunner sr = new ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
WebRequest request = form.getRequest( "save", "Open Pool" );
@@ -86,6 +87,7 @@
<b>public void</b> testBadPoolOpen() <b>throws</b> Exception {
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
WebRequest request = form.getRequest( "save", "Open Pool" ); // (1) select a submit button
@@ -96,7 +98,7 @@
request.setParameter( "tiebreaker", "3" );
response = client.getResponse( request ); // (3) submit the form
- WebTable errorTable = response.getTableStartingWith( "Errors Detected:" ); // (4) Look for error table
+ WebTable errorTable = response.getTableStartingWithPrefix( "Cannot ope" ); // (4) Look for error table
assertNotNull( "No errors reported", errorTable );
String[][] cells = errorTable.asText(); // (5) Convert non-empty cells to text
assertEquals( "Number of error messages provided", 2, cells.length - 1 );
@@ -108,7 +110,7 @@
<li>We select the "Open Pool" button to be included with the form submission.</li>
<li>We then enter known bad values.</li>
<li>We want the response when we submit the form changes.</li>
-<li>We expect to find them in a table which we can recognize because its upper-leftmost non-empty cell which contain
+<li>We expect to find them in a table which we can recognize because its upper-leftmost non-empty cell which starts with
a known string.</li>
<li>Since we want to examine the textual content of any non-empty cells in the table, we ask that the table be converted
to a two-dimensional string array. In this case, there should only be one non-blank cell in each row.</li></ol></p>
@@ -149,6 +151,7 @@
<b>public void</b> testGoodPoolOpen() <b>throws</b> Exception {
ServletRunner sr = <b>new</b> ServletRunner( "web.xml" );
ServletUnitClient client = sr.newClient();
+ client.setAuthorization( "aUser", "pool-admin" );
WebResponse response = client.getResponse( "http://localhost/PoolEditor" );
WebForm form = response.getFormWithID( "pool" );
WebRequest request = form.getRequest( "save", "Open Pool" );
Index: task2.html
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/task2.html,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- task2.html 2001/11/26 14:19:26 1.1
+++ task2.html 2001/11/27 16:40:47 1.2
@@ -9,15 +9,17 @@
<p class="location"><a href="index.html">Tutorial</a>
<img src="arrow_yellow.gif" width=13 height=9 align=bottom ALT="->">Task 2</p>
<h1>Controlling access to the application</h1>
-<p>Only the administrator is supposed to be able to edit the pool, but there is nothing in the code written so far
-which enforces it. The servlet API provides ways to restrict application to servlets. As part of this task, we will
-use the Basic Authentication approach, which causes the browser to pop up a challenge dialog. The more common form-based
-authentication is handled the same way that all forms should be. In addition, we will create a front-end page with links
-to those pages a user is permitted to access.
+<p>At this point, we have a working pool editor, accessible only to a user with the appropriate assigned role. Any other
+user will be refused when they try to access the editor. We would like to go further, and provide a common front page
+to the application for all users, which will provide links to the permitted pages only.</p>
+<b>This is not written</b>
+<!--
To begin the tutorial, create a working directory and expand <a href="task2.zip">this archive</a> into it.
Either create a jars directory and copy httpunit.jar, Tidy.jar, and xerces.jar into it, or make sure that they are on
your classpath and invoke ant passing the classpath as a property.
You will write your code in the src/tutorial sub-directory.
-Once you are ready, proceed to <a href="task2access-authentication.html">step one</a> to begin the tutorial.
\ No newline at end of file
+Once you are ready, proceed to <a href="task2access-authentication.html">step one</a> to begin the tutorial. -->
+
+</body></html>
\ No newline at end of file
Index: web.xml
===================================================================
RCS file: /cvsroot/httpunit/httpunit/doc/tutorial/web.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- web.xml 2001/11/12 20:43:51 1.1
+++ web.xml 2001/11/27 16:40:47 1.2
@@ -8,4 +8,17 @@
<servlet-name>Editor</servlet-name>
<url-pattern>/PoolEditor</url-pattern>
</servlet-mapping>
+ <security-constraint>
+ <web-resource-collection>
+ <web-resource-name>Administration</web-resource-name>
+ <url-pattern>/PoolEditor</url-pattern>
+ </web-resource-collection>
+ <auth-constraint>
+ <role-name>pool-admin</role-name>
+ </auth-constraint>
+ </security-constraint>
+ <login-config>
+ <auth-method>BASIC</auth-method>
+ <realm-name>Betting Pool</realm-name>
+ </login-config>
</web-app>
|