MiddlewareDispatcherTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 OC\AppFramework;
  23. use OC\AppFramework\Http\Request;
  24. use OC\AppFramework\Middleware\MiddlewareDispatcher;
  25. use OCP\AppFramework\Middleware;
  26. use OCP\AppFramework\Http\Response;
  27. // needed to test ordering
  28. class TestMiddleware extends Middleware {
  29. public static $beforeControllerCalled = 0;
  30. public static $afterControllerCalled = 0;
  31. public static $afterExceptionCalled = 0;
  32. public static $beforeOutputCalled = 0;
  33. public $beforeControllerOrder = 0;
  34. public $afterControllerOrder = 0;
  35. public $afterExceptionOrder = 0;
  36. public $beforeOutputOrder = 0;
  37. public $controller;
  38. public $methodName;
  39. public $exception;
  40. public $response;
  41. public $output;
  42. private $beforeControllerThrowsEx;
  43. /**
  44. * @param boolean $beforeControllerThrowsEx
  45. */
  46. public function __construct($beforeControllerThrowsEx) {
  47. self::$beforeControllerCalled = 0;
  48. self::$afterControllerCalled = 0;
  49. self::$afterExceptionCalled = 0;
  50. self::$beforeOutputCalled = 0;
  51. $this->beforeControllerThrowsEx = $beforeControllerThrowsEx;
  52. }
  53. public function beforeController($controller, $methodName){
  54. self::$beforeControllerCalled++;
  55. $this->beforeControllerOrder = self::$beforeControllerCalled;
  56. $this->controller = $controller;
  57. $this->methodName = $methodName;
  58. if($this->beforeControllerThrowsEx){
  59. throw new \Exception();
  60. }
  61. }
  62. public function afterException($controller, $methodName, \Exception $exception){
  63. self::$afterExceptionCalled++;
  64. $this->afterExceptionOrder = self::$afterExceptionCalled;
  65. $this->controller = $controller;
  66. $this->methodName = $methodName;
  67. $this->exception = $exception;
  68. parent::afterException($controller, $methodName, $exception);
  69. }
  70. public function afterController($controller, $methodName, Response $response){
  71. self::$afterControllerCalled++;
  72. $this->afterControllerOrder = self::$afterControllerCalled;
  73. $this->controller = $controller;
  74. $this->methodName = $methodName;
  75. $this->response = $response;
  76. return parent::afterController($controller, $methodName, $response);
  77. }
  78. public function beforeOutput($controller, $methodName, $output){
  79. self::$beforeOutputCalled++;
  80. $this->beforeOutputOrder = self::$beforeOutputCalled;
  81. $this->controller = $controller;
  82. $this->methodName = $methodName;
  83. $this->output = $output;
  84. return parent::beforeOutput($controller, $methodName, $output);
  85. }
  86. }
  87. class MiddlewareDispatcherTest extends \Test\TestCase {
  88. public $exception;
  89. public $response;
  90. private $out;
  91. private $method;
  92. private $controller;
  93. /**
  94. * @var MiddlewareDispatcher
  95. */
  96. private $dispatcher;
  97. protected function setUp() {
  98. parent::setUp();
  99. $this->dispatcher = new MiddlewareDispatcher();
  100. $this->controller = $this->getControllerMock();
  101. $this->method = 'method';
  102. $this->response = new Response();
  103. $this->out = 'hi';
  104. $this->exception = new \Exception();
  105. }
  106. private function getControllerMock(){
  107. return $this->getMock(
  108. 'OCP\AppFramework\Controller',
  109. ['method'],
  110. ['app',
  111. new Request(
  112. ['method' => 'GET'],
  113. $this->getMock('\OCP\Security\ISecureRandom'),
  114. $this->getMock('\OCP\IConfig')
  115. )
  116. ]
  117. );
  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->getMock('\OCP\AppFramework\Middleware',
  127. array('afterException', 'beforeController'));
  128. $m1->expects($this->never())
  129. ->method('afterException');
  130. $m2 = $this->getMock('OCP\AppFramework\Middleware',
  131. array('afterException', 'beforeController'));
  132. $m2->expects($this->once())
  133. ->method('afterException')
  134. ->will($this->returnValue($response));
  135. $this->dispatcher->registerMiddleware($m1);
  136. $this->dispatcher->registerMiddleware($m2);
  137. $this->dispatcher->beforeController($this->controller, $this->method);
  138. $this->assertEquals($response, $this->dispatcher->afterException($this->controller, $this->method, $this->exception));
  139. }
  140. public function testAfterExceptionShouldThrowAgainWhenNotHandled(){
  141. $m1 = new TestMiddleware(false);
  142. $m2 = new TestMiddleware(true);
  143. $this->dispatcher->registerMiddleware($m1);
  144. $this->dispatcher->registerMiddleware($m2);
  145. $this->setExpectedException('\Exception');
  146. $this->dispatcher->beforeController($this->controller, $this->method);
  147. $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
  148. }
  149. public function testBeforeControllerCorrectArguments(){
  150. $m1 = $this->getMiddleware();
  151. $this->dispatcher->beforeController($this->controller, $this->method);
  152. $this->assertEquals($this->controller, $m1->controller);
  153. $this->assertEquals($this->method, $m1->methodName);
  154. }
  155. public function testAfterControllerCorrectArguments(){
  156. $m1 = $this->getMiddleware();
  157. $this->dispatcher->afterController($this->controller, $this->method, $this->response);
  158. $this->assertEquals($this->controller, $m1->controller);
  159. $this->assertEquals($this->method, $m1->methodName);
  160. $this->assertEquals($this->response, $m1->response);
  161. }
  162. public function testAfterExceptionCorrectArguments(){
  163. $m1 = $this->getMiddleware();
  164. $this->setExpectedException('\Exception');
  165. $this->dispatcher->beforeController($this->controller, $this->method);
  166. $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
  167. $this->assertEquals($this->controller, $m1->controller);
  168. $this->assertEquals($this->method, $m1->methodName);
  169. $this->assertEquals($this->exception, $m1->exception);
  170. }
  171. public function testBeforeOutputCorrectArguments(){
  172. $m1 = $this->getMiddleware();
  173. $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
  174. $this->assertEquals($this->controller, $m1->controller);
  175. $this->assertEquals($this->method, $m1->methodName);
  176. $this->assertEquals($this->out, $m1->output);
  177. }
  178. public function testBeforeControllerOrder(){
  179. $m1 = $this->getMiddleware();
  180. $m2 = $this->getMiddleware();
  181. $this->dispatcher->beforeController($this->controller, $this->method);
  182. $this->assertEquals(1, $m1->beforeControllerOrder);
  183. $this->assertEquals(2, $m2->beforeControllerOrder);
  184. }
  185. public function testAfterControllerOrder(){
  186. $m1 = $this->getMiddleware();
  187. $m2 = $this->getMiddleware();
  188. $this->dispatcher->afterController($this->controller, $this->method, $this->response);
  189. $this->assertEquals(2, $m1->afterControllerOrder);
  190. $this->assertEquals(1, $m2->afterControllerOrder);
  191. }
  192. public function testAfterExceptionOrder(){
  193. $m1 = $this->getMiddleware();
  194. $m2 = $this->getMiddleware();
  195. $this->setExpectedException('\Exception');
  196. $this->dispatcher->beforeController($this->controller, $this->method);
  197. $this->dispatcher->afterException($this->controller, $this->method, $this->exception);
  198. $this->assertEquals(1, $m1->afterExceptionOrder);
  199. $this->assertEquals(1, $m2->afterExceptionOrder);
  200. }
  201. public function testBeforeOutputOrder(){
  202. $m1 = $this->getMiddleware();
  203. $m2 = $this->getMiddleware();
  204. $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
  205. $this->assertEquals(2, $m1->beforeOutputOrder);
  206. $this->assertEquals(1, $m2->beforeOutputOrder);
  207. }
  208. public function testExceptionShouldRunAfterExceptionOfOnlyPreviouslyExecutedMiddlewares(){
  209. $m1 = $this->getMiddleware();
  210. $m2 = $this->getMiddleware(true);
  211. $m3 = $this->getMock('\OCP\AppFramework\Middleware');
  212. $m3->expects($this->never())
  213. ->method('afterException');
  214. $m3->expects($this->never())
  215. ->method('beforeController');
  216. $m3->expects($this->never())
  217. ->method('afterController');
  218. $this->dispatcher->registerMiddleware($m3);
  219. $this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
  220. $this->assertEquals(2, $m1->beforeOutputOrder);
  221. $this->assertEquals(1, $m2->beforeOutputOrder);
  222. }
  223. }