@@ -41,6 +41,7 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
4141 // Not logged-in thus should not be accessible.
4242 assertFalse (response .contains ("This is a protected servlet" ));
4343
44+
4445 // -------------------- Request 2 ---------------------------
4546
4647 // JASPIC is stateless and login (re-authenticate) has to happen for every request
@@ -53,10 +54,13 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
5354 response = getFromServerPath ("protected/servlet?doLogin" );
5455
5556 // Now has to be logged-in so page is accessible
56- assertTrue ("Could not access protected page, but should be able to. "
57- + "Did the container remember the previously set 'unauthenticated identity'?" ,
58- response .contains ("This is a protected servlet" ));
57+ assertTrue (
58+ "Could not access protected page, but should be able to. " +
59+ "Did the container remember the previously set 'unauthenticated identity'?" ,
60+ response .contains ("This is a protected servlet" )
61+ );
5962
63+
6064 // -------------------- Request 3 ---------------------------
6165
6266 // JASPIC is stateless and login (re-authenticate) has to happen for every request
@@ -66,9 +70,11 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
6670 response = getFromServerPath ("protected/servlet" );
6771
6872 // Not logged-in thus should not be accessible.
69- assertFalse ("Could access protected page, but should not be able to. "
70- + "Did the container remember the authenticated identity that was set in previous request?" ,
71- response .contains ("This is a protected servlet" ));
73+ assertFalse (
74+ "Could access protected page, but should not be able to. " +
75+ "Did the container remember the authenticated identity that was set in previous request?" ,
76+ response .contains ("This is a protected servlet" )
77+ );
7278 }
7379
7480 /**
@@ -83,6 +89,7 @@ public void testProtectedAccessIsStateless2() throws IOException, SAXException {
8389 // Start with doing a login
8490 String response = getFromServerPath ("protected/servlet?doLogin" );
8591
92+
8693 // -------------------- Request 2 ---------------------------
8794
8895 // JASPIC is stateless and login (re-authenticate) has to happen for every request
@@ -94,37 +101,101 @@ public void testProtectedAccessIsStateless2() throws IOException, SAXException {
94101 response = getFromServerPath ("protected/servlet" );
95102
96103 // Not logged-in thus should not be accessible.
97- assertFalse ("Could access protected page, but should not be able to. "
98- + "Did the container remember the authenticated identity that was set in previous request?" ,
99- response .contains ("This is a protected servlet" ));
104+ assertFalse (
105+ "Could access protected page, but should not be able to. " +
106+ "Did the container remember the authenticated identity that was set in the previous request?" ,
107+ response .contains ("This is a protected servlet" )
108+ );
109+ }
110+
111+ /**
112+ * Tests that access to a public page does not depend on the authenticated identity that was established in a previous
113+ * request.
114+ */
115+ @ Test
116+ public void testPublicAccessIsStateless () throws IOException , SAXException {
117+
118+ // -------------------- Request 1 ---------------------------
119+
120+ String response = getFromServerPath ("public/servlet" );
121+
122+ // Establish that we're initially not logged-in
123+ assertTrue (
124+ "Not authenticated, but a username other than null was encountered. " +
125+ "This is not correct." ,
126+ response .contains ("web username: null" )
127+ );
128+ assertTrue (
129+ "Not authenticated, but the user seems to have the role \" architect\" . " +
130+ "This is not correct." ,
131+ response .contains ("web user has role \" architect\" : false" )
132+ );
133+
134+
135+ // -------------------- Request 2 ---------------------------
136+
137+ response = getFromServerPath ("public/servlet?doLogin" );
138+
139+ // Now has to be logged-in
140+ assertTrue (
141+ "User should have been authenticated and given name \" test\" , " +
142+ " but does not appear to have this name" ,
143+ response .contains ("web username: test" )
144+ );
145+ assertTrue (response .contains ("web user has role \" architect\" : true" ));
146+
147+
148+ // -------------------- Request 3 ---------------------------
149+
150+ // Accessing public page without login
151+ response = getFromServerPath ("public/servlet" );
152+
153+ // No details should linger around
154+ assertTrue (
155+ "Should not be authenticated, but a username other than null was encountered. " +
156+ "Did the container remember the authenticated identity that was set in the previous request?" ,
157+ response .contains ("web username: null" )
158+ );
159+ assertTrue (
160+ "The unauthenticated user has the role 'architect', which should not be the case. " +
161+ "The container seemed to have remembered it from the previous request." ,
162+ response .contains ("web user has role \" architect\" : false" )
163+ );
100164 }
101165
102166 /**
103167 * Tests independently from being able to access a protected resource if any details of a previously established
104168 * authenticated identity are remembered
105169 */
106170 @ Test
107- public void testUserIdentityIsStateless () throws IOException , SAXException {
171+ public void testProtectedThenPublicAccessIsStateless () throws IOException , SAXException {
108172
109173 // -------------------- Request 1 ---------------------------
110174
111175 // Accessing protected page with login
112176 String response = getFromServerPath ("protected/servlet?doLogin" );
113177
178+
114179 // -------------------- Request 2 ---------------------------
115180
116181 // Accessing public page without login
117182 response = getFromServerPath ("public/servlet" );
118183
119184 // No details should linger around
120- assertFalse ("User principal was 'test', but it should be null here. "
121- + "The container seemed to have remembered it from the previous request." ,
122- response .contains ("web username: test" ));
123- assertTrue ("User principal was not null, but it should be null here. " ,
124- response .contains ("web username: null" ));
125- assertTrue ("The unauthenticated user has the role 'architect', which should not be the case. "
126- + "The container seemed to have remembered it from the previous request." ,
127- response .contains ("web user has role \" architect\" : false" ));
185+ assertFalse (
186+ "User principal was 'test', but it should be null here. " +
187+ "The container seemed to have remembered it from the previous request." ,
188+ response .contains ("web username: test" )
189+ );
190+ assertTrue (
191+ "User principal was not null, but it should be null here. " ,
192+ response .contains ("web username: null" )
193+ );
194+ assertTrue (
195+ "The unauthenticated user has the role 'architect', which should not be the case. " +
196+ "The container seemed to have remembered it from the previous request." ,
197+ response .contains ("web user has role \" architect\" : false" )
198+ );
128199 }
129200
130201}
0 commit comments