|
18 | 18 |
|
19 | 19 | import java.io.File;
|
20 | 20 | import java.io.IOException;
|
| 21 | +import java.net.HttpURLConnection; |
21 | 22 | import java.util.HashMap;
|
22 | 23 | import java.util.List;
|
23 | 24 |
|
|
33 | 34 | import org.apache.catalina.core.StandardContext;
|
34 | 35 | import org.apache.catalina.startup.Tomcat;
|
35 | 36 | import org.apache.catalina.startup.TomcatBaseTest;
|
| 37 | +import org.apache.catalina.valves.RemoteAddrValve; |
36 | 38 | import org.apache.tomcat.util.buf.ByteChunk;
|
| 39 | +import org.apache.tomcat.util.descriptor.web.SecurityCollection; |
| 40 | +import org.apache.tomcat.util.descriptor.web.SecurityConstraint; |
37 | 41 | import org.apache.tomcat.websocket.server.WsContextListener;
|
38 | 42 |
|
39 | 43 | /**
|
@@ -225,6 +229,66 @@ public void testWelcomeFileStrict() throws Exception {
|
225 | 229 | Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rc);
|
226 | 230 | }
|
227 | 231 |
|
| 232 | + @Test |
| 233 | + public void testRedirect() throws Exception { |
| 234 | + // Disable the following of redirects for this test only |
| 235 | + boolean originalValue = HttpURLConnection.getFollowRedirects(); |
| 236 | + HttpURLConnection.setFollowRedirects(false); |
| 237 | + try { |
| 238 | + Tomcat tomcat = getTomcatInstance(); |
| 239 | + |
| 240 | + // Use standard test webapp as ROOT |
| 241 | + File rootDir = new File("test/webapp"); |
| 242 | + org.apache.catalina.Context root = |
| 243 | + tomcat.addWebapp(null, "", rootDir.getAbsolutePath()); |
| 244 | + |
| 245 | + // Add a security constraint |
| 246 | + SecurityConstraint constraint = new SecurityConstraint(); |
| 247 | + SecurityCollection collection = new SecurityCollection(); |
| 248 | + collection.addPattern("/welcome-files/*"); |
| 249 | + collection.addPattern("/welcome-files"); |
| 250 | + constraint.addCollection(collection); |
| 251 | + constraint.addAuthRole("foo"); |
| 252 | + root.addConstraint(constraint); |
| 253 | + |
| 254 | + // Also make examples available |
| 255 | + File examplesDir = new File(getBuildDirectory(), "webapps/examples"); |
| 256 | + org.apache.catalina.Context examples = tomcat.addWebapp( |
| 257 | + null, "/examples", examplesDir.getAbsolutePath()); |
| 258 | + // Then block access to the examples to test redirection |
| 259 | + RemoteAddrValve rav = new RemoteAddrValve(); |
| 260 | + rav.setDeny(".*"); |
| 261 | + rav.setDenyStatus(404); |
| 262 | + examples.getPipeline().addValve(rav); |
| 263 | + |
| 264 | + tomcat.start(); |
| 265 | + |
| 266 | + // Redirects within a web application |
| 267 | + doRedirectTest("/welcome-files", 401); |
| 268 | + doRedirectTest("/welcome-files/", 401); |
| 269 | + |
| 270 | + doRedirectTest("/jsp", 302); |
| 271 | + doRedirectTest("/jsp/", 404); |
| 272 | + |
| 273 | + doRedirectTest("/WEB-INF", 404); |
| 274 | + doRedirectTest("/WEB-INF/", 404); |
| 275 | + |
| 276 | + // Redirects between web applications |
| 277 | + doRedirectTest("/examples", 404); |
| 278 | + doRedirectTest("/examples/", 404); |
| 279 | + } finally { |
| 280 | + HttpURLConnection.setFollowRedirects(originalValue); |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + |
| 285 | + private void doRedirectTest(String path, int expected) throws IOException { |
| 286 | + ByteChunk bc = new ByteChunk(); |
| 287 | + int rc = getUrl("http://localhost:" + getPort() + path, bc, null); |
| 288 | + Assert.assertEquals(expected, rc); |
| 289 | + } |
| 290 | + |
| 291 | + |
228 | 292 | /**
|
229 | 293 | * Prepare a string to search in messages that contain a timestamp, when it
|
230 | 294 | * is known that the timestamp was printed between {@code timeA} and
|
|
0 commit comments