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