1
0

MiddlewareDispatcherTest.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. /**
  3. * ownCloud - App Framework
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\AppFramework\Middleware;
  23. use OC\AppFramework\Http\Request;
  24. use OC\AppFramework\Middleware\MiddlewareDispatcher;
  25. use OCP\AppFramework\Http\Response;
  26. use OCP\AppFramework\Middleware;
  27. use OCP\IConfig;
  28. use OCP\IRequestId;
  29. // needed to test ordering
  30. class TestMiddleware extends Middleware {
  31. public static $beforeControllerCalled = 0;
  32. public static $afterControllerCalled = 0;
  33. public static $afterExceptionCalled = 0;
  34. public static $beforeOutputCalled = 0;
  35. public $beforeControllerOrder = 0;
  36. public $afterControllerOrder = 0;
  37. public $afterExceptionOrder = 0;
  38. public $beforeOutputOrder = 0;
  39. public $controller;
  40. public $methodName;
  41. public $exception;
  42. public $response;
  43. public $output;
  44. private $beforeControllerThrowsEx;
  45. /**
  46. * @param boolean $beforeControllerThrowsEx
  47. */
  48. public function __construct($beforeControllerThrowsEx) {
  49. self::$beforeControllerCalled = 0;
  50. self::$afterControllerCalled = 0;
  51. self::$afterExceptionCalled = 0;
  52. self::$beforeOutputCalled = 0;
  53. $this->beforeControllerThrowsEx = $beforeControllerThrowsEx;
  54. }
  55. public function beforeController($controller, $methodName) {
  56. self::$beforeControllerCalled++;
  57. $this->beforeControllerOrder = self::$beforeControllerCalled;
  58. $this->controller = $controller;
  59. $this->methodName = $methodName;
  60. if ($this->beforeControllerThrowsEx) {
  61. throw new \Exception();
  62. }
  63. }
  64. public function afterException($controller, $methodName, \Exception $exception) {
  65. self::$afterExceptionCalled++;
  66. $this->afterExceptionOrder = self::$afterExceptionCalled;
  67. $this->controller = $controller;
  68. $this->methodName = $methodName;
  69. $this->exception = $exception;
  70. parent::afterException($controller, $methodName, $exception);
  71. }
  72. public function afterController($controller, $methodName, Response $response) {
  73. self::$afterControllerCalled++;
  74. $this->afterControllerOrder = self::$afterControllerCalled;
  75. $this->controller = $controller;
  76. $this->methodName = $methodName;
  77. $this->response = $response;
  78. return parent::afterController($controller, $methodName, $response);
  79. }
  80. public function beforeOutput($controller, $methodName, $output) {
  81. self::$beforeOutputCalled++;
  82. $this->beforeOutputOrder = self::$beforeOutputCalled;
  83. $this->controller = $controller;
  84. $this->methodName = $methodName;
  85. $this->output = $output;
  86. return parent::beforeOutput($controller, $methodName, $output);
  87. }
  88. }
  89. class MiddlewareDispatcherTest extends \Test\TestCase {
  90. public $exception;
  91. public $response;
  92. private $out;
  93. private $method;
  94. private $controller;
  95. /**
  96. * @var MiddlewareDispatcher
  97. */
  98. private $dispatcher;
  99. protected function setUp(): void {
  100. parent::setUp();
  101. $this->dispatcher = new MiddlewareDispatcher();
  102. $this->controller = $this->getControllerMock();
  103. $this->method = 'method';
  104. $this->response = new Response();
  105. $this->out = 'hi';
  106. $this->exception = new \Exception();
  107. }
  108. private function getControllerMock() {
  109. return $this->getMockBuilder('OCP\AppFramework\Controller')
  110. ->setMethods(['method'])
  111. ->setConstructorArgs(['app',
  112. new Request(
  113. ['method' => 'GET'],
  114. $this->createMock(IRequestId::class),
  115. $this->createMock(IConfig::class)
  116. )
  117. ])->getMock();
  118. }
  119. private function getMiddleware($beforeControllerThrowsEx = false) {
  120. $m1 = new TestMiddleware($beforeControllerThrowsEx);
  121. $this->dispatcher->registerMiddleware($m1);
  122. return $m1;
  123. }
  124. public function testAfterExceptionShouldReturnResponseOfMiddleware() {
  125. $response = new Response();
  126. $m1 = $this->getMockBuilder('\OCP\AppFramework\Middleware')
  127. ->setMethods(['afterException', 'beforeController'])
  128. ->getMock();
  129. $m1->expects($this->never())
  130. ->method('afterException');
  131. $m2 = $this->getMockBuilder('OCP\AppFramework\Middleware')
  132. ->setMethods(['afterException', 'beforeController'])
  133. ->getMock();
  134. $m2->expects($this->once())
  135. ->method('afterException')
  136. ->willReturn($response);
  137. $this->dispatcher->registerMiddleware($m1);
  138. $this->dispatcher->registerMiddleware($m2);
  139. $this->dispatcher->beforeController($this->controller, $this->method);
  140. $this->assertEquals($response, $this->dispatcher->afterException($this->controller, $this->method, $this->exception));
  141. }
  142. public function testAfterExceptionShouldThrowAgainWhenNotHandled() {
  143. $m1 = new TestMiddleware(false);
  144. $m2 = new TestMiddleware(true);
  145. $this->dispatcher->registerMiddleware($m1);
  146. $this->dispatcher->registerMiddleware($m2);
  147. $this->expectException(\Exception::class);
  148. $this->dispatcher->beforeController($this->controller, $this->method);
  149. $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
  150. }
  151. public function testBeforeControllerCorrectArguments() {
  152. $m1 = $this->getMiddleware();
  153. $this->dispatcher->beforeController($this->controller, $this->method);
  154. $this->assertEquals($this->controller, $m1->controller);
  155. $this->assertEquals($this->method, $m1->methodName);
  156. }
  157. public function testAfterControllerCorrectArguments() {
  158. $m1 = $this->getMiddleware();
  159. $this->dispatcher->afterController($this->controller, $this->method, $this->response);
  160. $this->assertEquals($this->controller, $m1->controller);
  161. $this->assertEquals($this->method, $m1->methodName);
  162. $this->assertEquals($this->response, $m1->response);
  163. }
  164. public function testAfterExceptionCorrectArguments() {
  165. $m1 = $this->getMiddleware();
  166. $this->expectException(\Exception::class);
  167. $this->dispatcher->beforeController($this->controller, $this->method);
  168. $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
  169. $this->assertEquals($this->controller, $m1->controller);
  170. $this->assertEquals($this->method, $m1->methodName);
  171. $this->assertEquals($this->exception, $m1->exception);
  172. }
  173. public function testBeforeOutputCorrectArguments() {
  174. $m1 = $this->getMiddleware();
  175. $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
  176. $this->assertEquals($this->controller, $m1->controller);
  177. $this->assertEquals($this->method, $m1->methodName);
  178. $this->assertEquals($this->out, $m1->output);
  179. }
  180. public function testBeforeControllerOrder() {
  181. $m1 = $this->getMiddleware();
  182. $m2 = $this->getMiddleware();
  183. $this->dispatcher->beforeController($this->controller, $this->method);
  184. $this->assertEquals(1, $m1->beforeControllerOrder);
  185. $this->assertEquals(2, $m2->beforeControllerOrder);
  186. }
  187. public function testAfterControllerOrder() {
  188. $m1 = $this->getMiddleware();
  189. $m2 = $this->getMiddleware();
  190. $this->dispatcher->afterController($this->controller, $this->method, $this->response);
  191. $this->assertEquals(2, $m1->afterControllerOrder);
  192. $this->assertEquals(1, $m2->afterControllerOrder);
  193. }
  194. public function testAfterExceptionOrder() {
  195. $m1 = $this->getMiddleware();
  196. $m2 = $this->getMiddleware();
  197. $this->expectException(\Exception::class);
  198. $this->dispatcher->beforeController($this->controller, $this->method);
  199. $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
  200. $this->assertEquals(1, $m1->afterExceptionOrder);
  201. $this->assertEquals(1, $m2->afterExceptionOrder);
  202. }
  203. public function testBeforeOutputOrder() {
  204. $m1 = $this->getMiddleware();
  205. $m2 = $this->getMiddleware();
  206. $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
  207. $this->assertEquals(2, $m1->beforeOutputOrder);
  208. $this->assertEquals(1, $m2->beforeOutputOrder);
  209. }
  210. public function testExceptionShouldRunAfterExceptionOfOnlyPreviouslyExecutedMiddlewares() {
  211. $m1 = $this->getMiddleware();
  212. $m2 = $this->getMiddleware(true);
  213. $m3 = $this->createMock(Middleware::class);
  214. $m3->expects($this->never())
  215. ->method('afterException');
  216. $m3->expects($this->never())
  217. ->method('beforeController');
  218. $m3->expects($this->never())
  219. ->method('afterController');
  220. $m3->method('beforeOutput')
  221. ->willReturnArgument(2);
  222. $this->dispatcher->registerMiddleware($m3);
  223. $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
  224. $this->assertEquals(2, $m1->beforeOutputOrder);
  225. $this->assertEquals(1, $m2->beforeOutputOrder);
  226. }
  227. }