Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExceptionHandlerTest.php 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Debug\Tests;
  11. use PHPUnit\Framework\TestCase;
  12. use Symfony\Component\Debug\Exception\OutOfMemoryException;
  13. use Symfony\Component\Debug\ExceptionHandler;
  14. use Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException;
  15. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  16. require_once __DIR__.'/HeaderMock.php';
  17. class ExceptionHandlerTest extends TestCase
  18. {
  19. protected function setUp()
  20. {
  21. testHeader();
  22. }
  23. protected function tearDown()
  24. {
  25. testHeader();
  26. }
  27. public function testDebug()
  28. {
  29. $handler = new ExceptionHandler(false);
  30. ob_start();
  31. $handler->sendPhpResponse(new \RuntimeException('Foo'));
  32. $response = ob_get_clean();
  33. $this->assertContains('Whoops, looks like something went wrong.', $response);
  34. $this->assertNotContains('<div class="trace trace-as-html">', $response);
  35. $handler = new ExceptionHandler(true);
  36. ob_start();
  37. $handler->sendPhpResponse(new \RuntimeException('Foo'));
  38. $response = ob_get_clean();
  39. $this->assertContains('<h1 class="break-long-words exception-message">Foo</h1>', $response);
  40. $this->assertContains('<div class="trace trace-as-html">', $response);
  41. // taken from https://www.owasp.org/index.php/Cross-site_Scripting_(XSS)
  42. $htmlWithXss = '<body onload=alert(\'test1\')> <b onmouseover=alert(\'Wufff!\')>click me!</b> <img src="j&#X41vascript:alert(\'test2\')"> <meta http-equiv="refresh"
  43. content="0;url=data:text/html;base64,PHNjcmlwdD5hbGVydCgndGVzdDMnKTwvc2NyaXB0Pg">';
  44. ob_start();
  45. $handler->sendPhpResponse(new \RuntimeException($htmlWithXss));
  46. $response = ob_get_clean();
  47. $this->assertContains(sprintf('<h1 class="break-long-words exception-message">%s</h1>', htmlspecialchars($htmlWithXss, ENT_COMPAT | ENT_SUBSTITUTE, 'UTF-8')), $response);
  48. }
  49. public function testStatusCode()
  50. {
  51. $handler = new ExceptionHandler(false, 'iso8859-1');
  52. ob_start();
  53. $handler->sendPhpResponse(new NotFoundHttpException('Foo'));
  54. $response = ob_get_clean();
  55. $this->assertContains('Sorry, the page you are looking for could not be found.', $response);
  56. $expectedHeaders = [
  57. ['HTTP/1.0 404', true, null],
  58. ['Content-Type: text/html; charset=iso8859-1', true, null],
  59. ];
  60. $this->assertSame($expectedHeaders, testHeader());
  61. }
  62. public function testHeaders()
  63. {
  64. $handler = new ExceptionHandler(false, 'iso8859-1');
  65. ob_start();
  66. $handler->sendPhpResponse(new MethodNotAllowedHttpException(['POST']));
  67. ob_get_clean();
  68. $expectedHeaders = [
  69. ['HTTP/1.0 405', true, null],
  70. ['Allow: POST', false, null],
  71. ['Content-Type: text/html; charset=iso8859-1', true, null],
  72. ];
  73. $this->assertSame($expectedHeaders, testHeader());
  74. }
  75. public function testNestedExceptions()
  76. {
  77. $handler = new ExceptionHandler(true);
  78. ob_start();
  79. $handler->sendPhpResponse(new \RuntimeException('Foo', 0, new \RuntimeException('Bar')));
  80. $response = ob_get_clean();
  81. $this->assertStringMatchesFormat('%A<p class="break-long-words trace-message">Foo</p>%A<p class="break-long-words trace-message">Bar</p>%A', $response);
  82. }
  83. public function testHandle()
  84. {
  85. $handler = new ExceptionHandler(true);
  86. ob_start();
  87. $handler->handle(new \Exception('foo'));
  88. $this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'foo');
  89. }
  90. public function testHandleWithACustomHandlerThatOutputsSomething()
  91. {
  92. $handler = new ExceptionHandler(true);
  93. ob_start();
  94. $handler->setHandler(function () {
  95. echo 'ccc';
  96. });
  97. $handler->handle(new \Exception());
  98. ob_end_flush(); // Necessary because of this PHP bug : https://bugs.php.net/bug.php?id=76563
  99. $this->assertSame('ccc', ob_get_clean());
  100. }
  101. public function testHandleWithACustomHandlerThatOutputsNothing()
  102. {
  103. $handler = new ExceptionHandler(true);
  104. $handler->setHandler(function () {});
  105. $handler->handle(new \Exception('ccc'));
  106. $this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'ccc');
  107. }
  108. public function testHandleWithACustomHandlerThatFails()
  109. {
  110. $handler = new ExceptionHandler(true);
  111. $handler->setHandler(function () {
  112. throw new \RuntimeException();
  113. });
  114. $handler->handle(new \Exception('ccc'));
  115. $this->assertThatTheExceptionWasOutput(ob_get_clean(), \Exception::class, 'Exception', 'ccc');
  116. }
  117. public function testHandleOutOfMemoryException()
  118. {
  119. $handler = new ExceptionHandler(true);
  120. ob_start();
  121. $handler->setHandler(function () {
  122. $this->fail('OutOfMemoryException should bypass the handler');
  123. });
  124. $handler->handle(new OutOfMemoryException('foo', 0, E_ERROR, __FILE__, __LINE__));
  125. $this->assertThatTheExceptionWasOutput(ob_get_clean(), OutOfMemoryException::class, 'OutOfMemoryException', 'foo');
  126. }
  127. private function assertThatTheExceptionWasOutput($content, $expectedClass, $expectedTitle, $expectedMessage)
  128. {
  129. $this->assertContains(sprintf('<span class="exception_title"><abbr title="%s">%s</abbr></span>', $expectedClass, $expectedTitle), $content);
  130. $this->assertContains(sprintf('<p class="break-long-words trace-message">%s</p>', $expectedMessage), $content);
  131. }
  132. }