|
32 | 32 | import org.junit.Test; |
33 | 33 |
|
34 | 34 | import org.apache.catalina.Context; |
| 35 | +import org.apache.catalina.Valve; |
35 | 36 | import org.apache.catalina.Wrapper; |
36 | 37 | import org.apache.catalina.startup.Tomcat; |
37 | 38 | import org.apache.catalina.startup.TomcatBaseTest; |
@@ -263,4 +264,146 @@ public void testErrorPageServlet() throws Exception { |
263 | 264 | } |
264 | 265 |
|
265 | 266 |
|
| 267 | + @Test |
| 268 | + public void testShowReportFalse() throws Exception { |
| 269 | + Tomcat tomcat = getTomcatInstance(); |
| 270 | + |
| 271 | + Context ctx = getProgrammaticRootContext(); |
| 272 | + |
| 273 | + // Must use sendError() — setStatus() alone commits the response before |
| 274 | + // ErrorReportValve can write a body, so showReport would have nothing to suppress. |
| 275 | + Tomcat.addServlet(ctx, "error404", new HttpServlet() { |
| 276 | + private static final long serialVersionUID = 1L; |
| 277 | + @Override |
| 278 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 279 | + throws ServletException, IOException { |
| 280 | + resp.sendError(HttpServletResponse.SC_NOT_FOUND); |
| 281 | + } |
| 282 | + }); |
| 283 | + ctx.addServletMappingDecoded("/", "error404"); |
| 284 | + |
| 285 | + tomcat.start(); |
| 286 | + |
| 287 | + ErrorReportValve erv = findErrorReportValve(tomcat); |
| 288 | + Assert.assertNotNull("ErrorReportValve should exist", erv); |
| 289 | + erv.setShowReport(false); |
| 290 | + |
| 291 | + ByteChunk res = new ByteChunk(); |
| 292 | + res.setCharset(StandardCharsets.UTF_8); |
| 293 | + int rc = getUrl("http://localhost:" + getPort(), res, null); |
| 294 | + |
| 295 | + Assert.assertEquals(HttpServletResponse.SC_NOT_FOUND, rc); |
| 296 | + String body = res.toString(); |
| 297 | + Assert.assertNotNull(body); |
| 298 | + Assert.assertFalse("Report section should be suppressed when showReport=false", |
| 299 | + body.contains(sm.getString("errorReportValve.description"))); |
| 300 | + Assert.assertFalse(erv.isShowReport()); |
| 301 | + } |
| 302 | + |
| 303 | + |
| 304 | + @Test |
| 305 | + public void testShowServerInfoFalse() throws Exception { |
| 306 | + Tomcat tomcat = getTomcatInstance(); |
| 307 | + |
| 308 | + Context ctx = getProgrammaticRootContext(); |
| 309 | + |
| 310 | + Tomcat.addServlet(ctx, "error500", new ErrorServlet()); |
| 311 | + ctx.addServletMappingDecoded("/", "error500"); |
| 312 | + |
| 313 | + tomcat.start(); |
| 314 | + |
| 315 | + ErrorReportValve erv = findErrorReportValve(tomcat); |
| 316 | + Assert.assertNotNull("ErrorReportValve should exist", erv); |
| 317 | + erv.setShowServerInfo(false); |
| 318 | + |
| 319 | + ByteChunk res = new ByteChunk(); |
| 320 | + res.setCharset(StandardCharsets.UTF_8); |
| 321 | + getUrl("http://localhost:" + getPort(), res, null); |
| 322 | + |
| 323 | + String body = res.toString(); |
| 324 | + Assert.assertNotNull(body); |
| 325 | + Assert.assertFalse("Server info should be hidden", body.contains("Apache Tomcat")); |
| 326 | + Assert.assertFalse(erv.isShowServerInfo()); |
| 327 | + } |
| 328 | + |
| 329 | + |
| 330 | + @Test |
| 331 | + public void testSetPropertyErrorCode() { |
| 332 | + ErrorReportValve valve = new ErrorReportValve(); |
| 333 | + |
| 334 | + Assert.assertTrue(valve.setProperty("errorCode.404", "/error404.html")); |
| 335 | + Assert.assertEquals("/error404.html", valve.getProperty("errorCode.404")); |
| 336 | + } |
| 337 | + |
| 338 | + |
| 339 | + @Test |
| 340 | + public void testSetPropertyExceptionType() { |
| 341 | + ErrorReportValve valve = new ErrorReportValve(); |
| 342 | + |
| 343 | + Assert.assertTrue(valve.setProperty( |
| 344 | + "exceptionType.java.lang.NullPointerException", "/npe.html")); |
| 345 | + Assert.assertEquals("/npe.html", valve.getProperty( |
| 346 | + "exceptionType.java.lang.NullPointerException")); |
| 347 | + } |
| 348 | + |
| 349 | + |
| 350 | + @Test |
| 351 | + public void testGetPropertyNotFound() { |
| 352 | + ErrorReportValve valve = new ErrorReportValve(); |
| 353 | + |
| 354 | + Assert.assertNull(valve.getProperty("errorCode.999")); |
| 355 | + Assert.assertNull(valve.getProperty("exceptionType.com.example.Nope")); |
| 356 | + } |
| 357 | + |
| 358 | + |
| 359 | + @Test |
| 360 | + public void testGetPropertyUnknownPrefix() { |
| 361 | + ErrorReportValve valve = new ErrorReportValve(); |
| 362 | + |
| 363 | + Assert.assertFalse(valve.setProperty("unknownPrefix.something", "/x.html")); |
| 364 | + Assert.assertNull(valve.getProperty("unknownPrefix.something")); |
| 365 | + } |
| 366 | + |
| 367 | + |
| 368 | + @Test |
| 369 | + public void testExceptionWithRootCause() throws Exception { |
| 370 | + Tomcat tomcat = getTomcatInstance(); |
| 371 | + |
| 372 | + Context ctx = getProgrammaticRootContext(); |
| 373 | + |
| 374 | + Tomcat.addServlet(ctx, "nestedError", new HttpServlet() { |
| 375 | + private static final long serialVersionUID = 1L; |
| 376 | + @Override |
| 377 | + protected void doGet(HttpServletRequest req, HttpServletResponse resp) |
| 378 | + throws ServletException, IOException { |
| 379 | + Throwable root = new IllegalStateException("root cause"); |
| 380 | + Throwable wrapper = new RuntimeException("wrapper", root); |
| 381 | + req.setAttribute(RequestDispatcher.ERROR_EXCEPTION, wrapper); |
| 382 | + resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); |
| 383 | + } |
| 384 | + }); |
| 385 | + ctx.addServletMappingDecoded("/", "nestedError"); |
| 386 | + |
| 387 | + tomcat.start(); |
| 388 | + |
| 389 | + ByteChunk res = new ByteChunk(); |
| 390 | + res.setCharset(StandardCharsets.UTF_8); |
| 391 | + int rc = getUrl("http://localhost:" + getPort(), res, null); |
| 392 | + |
| 393 | + Assert.assertEquals(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, rc); |
| 394 | + String body = res.toString(); |
| 395 | + Assert.assertNotNull(body); |
| 396 | + // Should contain root cause section |
| 397 | + Assert.assertTrue(body.contains(sm.getString("errorReportValve.rootCause"))); |
| 398 | + } |
| 399 | + |
| 400 | + |
| 401 | + private static ErrorReportValve findErrorReportValve(Tomcat tomcat) { |
| 402 | + for (Valve v : tomcat.getHost().getPipeline().getValves()) { |
| 403 | + if (v instanceof ErrorReportValve) { |
| 404 | + return (ErrorReportValve) v; |
| 405 | + } |
| 406 | + } |
| 407 | + return null; |
| 408 | + } |
266 | 409 | } |
0 commit comments