@@ -41,6 +41,7 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
41
41
// Not logged-in thus should not be accessible.
42
42
assertFalse (response .contains ("This is a protected servlet" ));
43
43
44
+
44
45
// -------------------- Request 2 ---------------------------
45
46
46
47
// JASPIC is stateless and login (re-authenticate) has to happen for every request
@@ -53,10 +54,13 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
53
54
response = getFromServerPath ("protected/servlet?doLogin" );
54
55
55
56
// 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
+ );
59
62
63
+
60
64
// -------------------- Request 3 ---------------------------
61
65
62
66
// JASPIC is stateless and login (re-authenticate) has to happen for every request
@@ -66,9 +70,11 @@ public void testProtectedAccessIsStateless() throws IOException, SAXException {
66
70
response = getFromServerPath ("protected/servlet" );
67
71
68
72
// 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
+ );
72
78
}
73
79
74
80
/**
@@ -83,6 +89,7 @@ public void testProtectedAccessIsStateless2() throws IOException, SAXException {
83
89
// Start with doing a login
84
90
String response = getFromServerPath ("protected/servlet?doLogin" );
85
91
92
+
86
93
// -------------------- Request 2 ---------------------------
87
94
88
95
// JASPIC is stateless and login (re-authenticate) has to happen for every request
@@ -94,37 +101,101 @@ public void testProtectedAccessIsStateless2() throws IOException, SAXException {
94
101
response = getFromServerPath ("protected/servlet" );
95
102
96
103
// 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
+ );
100
164
}
101
165
102
166
/**
103
167
* Tests independently from being able to access a protected resource if any details of a previously established
104
168
* authenticated identity are remembered
105
169
*/
106
170
@ Test
107
- public void testUserIdentityIsStateless () throws IOException , SAXException {
171
+ public void testProtectedThenPublicAccessIsStateless () throws IOException , SAXException {
108
172
109
173
// -------------------- Request 1 ---------------------------
110
174
111
175
// Accessing protected page with login
112
176
String response = getFromServerPath ("protected/servlet?doLogin" );
113
177
178
+
114
179
// -------------------- Request 2 ---------------------------
115
180
116
181
// Accessing public page without login
117
182
response = getFromServerPath ("public/servlet" );
118
183
119
184
// 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
+ );
128
199
}
129
200
130
201
}
0 commit comments