44import static org .junit .Assert .assertTrue ;
55
66import java .io .IOException ;
7- import java .net .URL ;
87
98import org .javaee7 .jaspic .common .ArquillianBase ;
109import org .jboss .arquillian .container .test .api .Deployment ;
1110import org .jboss .arquillian .junit .Arquillian ;
12- import org .jboss .arquillian .test .api .ArquillianResource ;
1311import org .jboss .shrinkwrap .api .spec .WebArchive ;
1412import org .junit .Test ;
1513import org .junit .runner .RunWith ;
1614import org .xml .sax .SAXException ;
1715
18- import com .meterware .httpunit .GetMethodWebRequest ;
19- import com .meterware .httpunit .WebConversation ;
20- import com .meterware .httpunit .WebResponse ;
21-
2216/**
2317 *
2418 * @author Arjan Tijms
2721@ RunWith (Arquillian .class )
2822public class BasicAuthenticationStatelessTest extends ArquillianBase {
2923
30- @ ArquillianResource
31- private URL base ;
32-
3324 @ Deployment (testable = false )
3425 public static WebArchive createDeployment () {
3526 return defaultArchive ();
3627 }
28+
3729
3830 /**
3931 * Tests that access to a protected page does not depend on the authenticated identity that was established in a previous
4032 * request.
4133 */
4234 @ Test
4335 public void testProtectedAccessIsStateless () throws IOException , SAXException {
44-
45- WebConversation conversion = new WebConversation ();
36+
4637
4738 // -------------------- Request 1 ---------------------------
4839
4940 // Accessing protected page without login
50- WebResponse response = conversion . getResponse ( new GetMethodWebRequest ( base + "protected/servlet" ) );
51-
41+ String response = getFromServerPath ( "protected/servlet" );
42+
5243 // Not logged-in thus should not be accessible.
53- assertFalse (response .getText ().contains ("This is a protected servlet" ));
44+ assertFalse (response .contains ("This is a protected servlet" ));
45+
5446
5547 // -------------------- Request 2 ---------------------------
5648
@@ -61,25 +53,26 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
6153 // we're not authenticated and it will deny further attempts to authenticate. This may happen when
6254 // the container does not correctly recognize the JASPIC protocol for "do nothing".
6355
64- response = conversion . getResponse ( new GetMethodWebRequest ( base + "protected/servlet?doLogin" ) );
56+ response = getFromServerPath ( "protected/servlet?doLogin" );
6557
6658 // Now has to be logged-in so page is accessible
6759 assertTrue ("Could not access protected page, but should be able to. "
6860 + "Did the container remember the previously set 'unauthenticated identity'?" ,
69- response .getText ().contains ("This is a protected servlet" ));
61+ response .contains ("This is a protected servlet" ));
62+
7063
7164 // -------------------- Request 3 ---------------------------
7265
7366 // JASPIC is stateless and login (re-authenticate) has to happen for every request
7467 //
7568 // In the following method we do a call without logging in after one where we did login.
7669 // The container should not remember this login and has to deny access.
77- response = conversion . getResponse ( new GetMethodWebRequest ( base + "protected/servlet" ) );
70+ response = getFromServerPath ( "protected/servlet" );
7871
7972 // Not logged-in thus should not be accessible.
8073 assertFalse ("Could access protected page, but should not be able to. "
81- + "Did the container remember the authenticated identity that was set in previous request?" , response . getText ()
82- .contains ("This is a protected servlet" ));
74+ + "Did the container remember the authenticated identity that was set in previous request?" ,
75+ response .contains ("This is a protected servlet" ));
8376 }
8477
8578 /**
@@ -89,12 +82,11 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
8982 @ Test
9083 public void testProtectedAccessIsStateless2 () throws IOException , SAXException {
9184
92- WebConversation conversion = new WebConversation ();
93-
9485 // -------------------- Request 1 ---------------------------
9586
9687 // Start with doing a login
97- WebResponse response = conversion .getResponse (new GetMethodWebRequest (base + "protected/servlet?doLogin" ));
88+ String response = getFromServerPath ("protected/servlet?doLogin" );
89+
9890
9991 // -------------------- Request 2 ---------------------------
10092
@@ -104,13 +96,12 @@ public void testProtectedAccessIsStateless2() throws IOException, SAXException {
10496 // The container should not remember this login and has to deny access.
10597
10698 // Accessing protected page without login
107-
108- response = conversion .getResponse (new GetMethodWebRequest (base + "protected/servlet" ));
99+ response = getFromServerPath ("protected/servlet" );
109100
110101 // Not logged-in thus should not be accessible.
111102 assertFalse ("Could access protected page, but should not be able to. "
112- + "Did the container remember the authenticated identity that was set in previous request?" , response . getText ()
113- .contains ("This is page A. " ));
103+ + "Did the container remember the authenticated identity that was set in previous request?" ,
104+ response .contains ("This is a protected servlet " ));
114105 }
115106
116107 /**
@@ -119,28 +110,28 @@ public void testProtectedAccessIsStateless2() throws IOException, SAXException {
119110 */
120111 @ Test
121112 public void testUserIdentityIsStateless () throws IOException , SAXException {
122-
123- WebConversation conversion = new WebConversation ();
113+
124114
125115 // -------------------- Request 1 ---------------------------
126116
127117 // Accessing protected page with login
128- WebResponse response = conversion .getResponse (new GetMethodWebRequest (base + "protected/servlet?doLogin" ));
118+ String response = getFromServerPath ("protected/servlet?doLogin" );
119+
129120
130121 // -------------------- Request 2 ---------------------------
131122
132123 // Accessing public page without login
133- response = conversion . getResponse ( new GetMethodWebRequest ( base + "public/servlet" ) );
124+ response = getFromServerPath ( "public/servlet" );
134125
135126 // No details should linger around
136127 assertFalse ("User principal was 'test', but it should be null here. "
137128 + "The container seemed to have remembered it from the previous request." ,
138- response .getText (). contains ("web username: test" ));
129+ response .contains ("web username: test" ));
139130 assertTrue ("User principal was not null, but it should be null here. " ,
140- response .getText (). contains ("web username: null" ));
131+ response .contains ("web username: null" ));
141132 assertTrue ("The unauthenticated user has the role 'architect', which should not be the case. "
142133 + "The container seemed to have remembered it from the previous request." ,
143- response .getText (). contains ("web user has role \" architect\" : false" ));
134+ response .contains ("web user has role \" architect\" : false" ));
144135 }
145136
146137}
0 commit comments