2013-08-17 13:16:48 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ownCloud - App Framework
|
|
|
|
*
|
|
|
|
* @author Bernhard Posselt
|
2014-05-07 00:25:05 +04:00
|
|
|
* @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
|
2013-08-17 13:16:48 +04:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 3 of the License, or any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public
|
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-18 19:40:34 +03:00
|
|
|
namespace Test\AppFramework\Middleware;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
use OC\AppFramework\Http\Request;
|
|
|
|
use OC\AppFramework\Middleware\MiddlewareDispatcher;
|
2017-07-26 10:03:04 +03:00
|
|
|
use OCP\AppFramework\Controller;
|
2013-08-21 02:41:20 +04:00
|
|
|
use OCP\AppFramework\Http\Response;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\AppFramework\Middleware;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IConfig;
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
// needed to test ordering
|
|
|
|
class TestMiddleware extends Middleware {
|
|
|
|
public static $beforeControllerCalled = 0;
|
|
|
|
public static $afterControllerCalled = 0;
|
|
|
|
public static $afterExceptionCalled = 0;
|
|
|
|
public static $beforeOutputCalled = 0;
|
|
|
|
|
|
|
|
public $beforeControllerOrder = 0;
|
|
|
|
public $afterControllerOrder = 0;
|
|
|
|
public $afterExceptionOrder = 0;
|
|
|
|
public $beforeOutputOrder = 0;
|
|
|
|
|
|
|
|
public $controller;
|
|
|
|
public $methodName;
|
|
|
|
public $exception;
|
|
|
|
public $response;
|
|
|
|
public $output;
|
|
|
|
|
|
|
|
private $beforeControllerThrowsEx;
|
|
|
|
|
2014-02-19 12:31:54 +04:00
|
|
|
/**
|
|
|
|
* @param boolean $beforeControllerThrowsEx
|
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
public function __construct($beforeControllerThrowsEx) {
|
|
|
|
self::$beforeControllerCalled = 0;
|
|
|
|
self::$afterControllerCalled = 0;
|
|
|
|
self::$afterExceptionCalled = 0;
|
|
|
|
self::$beforeOutputCalled = 0;
|
|
|
|
$this->beforeControllerThrowsEx = $beforeControllerThrowsEx;
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:32:03 +03:00
|
|
|
public function beforeController($controller, $methodName){
|
2013-08-17 13:16:48 +04:00
|
|
|
self::$beforeControllerCalled++;
|
|
|
|
$this->beforeControllerOrder = self::$beforeControllerCalled;
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->methodName = $methodName;
|
|
|
|
if($this->beforeControllerThrowsEx){
|
|
|
|
throw new \Exception();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:32:03 +03:00
|
|
|
public function afterException($controller, $methodName, \Exception $exception){
|
2013-08-17 13:16:48 +04:00
|
|
|
self::$afterExceptionCalled++;
|
|
|
|
$this->afterExceptionOrder = self::$afterExceptionCalled;
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->methodName = $methodName;
|
|
|
|
$this->exception = $exception;
|
|
|
|
parent::afterException($controller, $methodName, $exception);
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:32:03 +03:00
|
|
|
public function afterController($controller, $methodName, Response $response){
|
2013-08-17 13:16:48 +04:00
|
|
|
self::$afterControllerCalled++;
|
|
|
|
$this->afterControllerOrder = self::$afterControllerCalled;
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->methodName = $methodName;
|
|
|
|
$this->response = $response;
|
|
|
|
return parent::afterController($controller, $methodName, $response);
|
|
|
|
}
|
|
|
|
|
2017-08-01 18:32:03 +03:00
|
|
|
public function beforeOutput($controller, $methodName, $output){
|
2013-08-17 13:16:48 +04:00
|
|
|
self::$beforeOutputCalled++;
|
|
|
|
$this->beforeOutputOrder = self::$beforeOutputCalled;
|
|
|
|
$this->controller = $controller;
|
|
|
|
$this->methodName = $methodName;
|
|
|
|
$this->output = $output;
|
|
|
|
return parent::beforeOutput($controller, $methodName, $output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
class MiddlewareDispatcherTest extends \Test\TestCase {
|
2013-08-17 13:16:48 +04:00
|
|
|
|
2013-08-21 02:44:39 +04:00
|
|
|
public $exception;
|
|
|
|
public $response;
|
|
|
|
private $out;
|
|
|
|
private $method;
|
|
|
|
private $controller;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var MiddlewareDispatcher
|
|
|
|
*/
|
2013-08-17 13:16:48 +04:00
|
|
|
private $dispatcher;
|
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$this->dispatcher = new MiddlewareDispatcher();
|
|
|
|
$this->controller = $this->getControllerMock();
|
|
|
|
$this->method = 'method';
|
|
|
|
$this->response = new Response();
|
2013-08-21 02:44:39 +04:00
|
|
|
$this->out = 'hi';
|
2013-08-17 13:16:48 +04:00
|
|
|
$this->exception = new \Exception();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function getControllerMock(){
|
2016-07-10 15:17:26 +03:00
|
|
|
return $this->getMockBuilder('OCP\AppFramework\Controller')
|
|
|
|
->setMethods(['method'])
|
|
|
|
->setConstructorArgs(['app',
|
2015-02-09 13:41:48 +03:00
|
|
|
new Request(
|
|
|
|
['method' => 'GET'],
|
2016-07-10 15:17:26 +03:00
|
|
|
$this->getMockBuilder('\OCP\Security\ISecureRandom')->getMock(),
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->getMockBuilder(IConfig::class)->getMock()
|
2015-02-09 13:41:48 +03:00
|
|
|
)
|
2016-07-10 15:17:26 +03:00
|
|
|
])->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function getMiddleware($beforeControllerThrowsEx=false){
|
|
|
|
$m1 = new TestMiddleware($beforeControllerThrowsEx);
|
|
|
|
$this->dispatcher->registerMiddleware($m1);
|
|
|
|
return $m1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testAfterExceptionShouldReturnResponseOfMiddleware(){
|
|
|
|
$response = new Response();
|
2016-07-10 15:17:26 +03:00
|
|
|
$m1 = $this->getMockBuilder('\OCP\AppFramework\Middleware')
|
|
|
|
->setMethods(['afterException', 'beforeController'])
|
|
|
|
->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
$m1->expects($this->never())
|
|
|
|
->method('afterException');
|
|
|
|
|
2016-07-10 15:17:26 +03:00
|
|
|
$m2 = $this->getMockBuilder('OCP\AppFramework\Middleware')
|
|
|
|
->setMethods(['afterException', 'beforeController'])
|
|
|
|
->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
$m2->expects($this->once())
|
|
|
|
->method('afterException')
|
|
|
|
->will($this->returnValue($response));
|
|
|
|
|
|
|
|
$this->dispatcher->registerMiddleware($m1);
|
|
|
|
$this->dispatcher->registerMiddleware($m2);
|
|
|
|
|
|
|
|
$this->dispatcher->beforeController($this->controller, $this->method);
|
|
|
|
$this->assertEquals($response, $this->dispatcher->afterException($this->controller, $this->method, $this->exception));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testAfterExceptionShouldThrowAgainWhenNotHandled(){
|
|
|
|
$m1 = new TestMiddleware(false);
|
|
|
|
$m2 = new TestMiddleware(true);
|
|
|
|
|
|
|
|
$this->dispatcher->registerMiddleware($m1);
|
|
|
|
$this->dispatcher->registerMiddleware($m2);
|
|
|
|
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(\Exception::class);
|
2013-08-17 13:16:48 +04:00
|
|
|
$this->dispatcher->beforeController($this->controller, $this->method);
|
|
|
|
$this->dispatcher->afterException($this->controller, $this->method, $this->exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testBeforeControllerCorrectArguments(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
$this->dispatcher->beforeController($this->controller, $this->method);
|
|
|
|
|
|
|
|
$this->assertEquals($this->controller, $m1->controller);
|
|
|
|
$this->assertEquals($this->method, $m1->methodName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testAfterControllerCorrectArguments(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
|
|
|
|
$this->dispatcher->afterController($this->controller, $this->method, $this->response);
|
|
|
|
|
|
|
|
$this->assertEquals($this->controller, $m1->controller);
|
|
|
|
$this->assertEquals($this->method, $m1->methodName);
|
|
|
|
$this->assertEquals($this->response, $m1->response);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testAfterExceptionCorrectArguments(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(\Exception::class);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$this->dispatcher->beforeController($this->controller, $this->method);
|
|
|
|
$this->dispatcher->afterException($this->controller, $this->method, $this->exception);
|
|
|
|
|
|
|
|
$this->assertEquals($this->controller, $m1->controller);
|
|
|
|
$this->assertEquals($this->method, $m1->methodName);
|
|
|
|
$this->assertEquals($this->exception, $m1->exception);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testBeforeOutputCorrectArguments(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
|
2013-08-21 02:44:39 +04:00
|
|
|
$this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$this->assertEquals($this->controller, $m1->controller);
|
|
|
|
$this->assertEquals($this->method, $m1->methodName);
|
2013-08-21 02:44:39 +04:00
|
|
|
$this->assertEquals($this->out, $m1->output);
|
2013-08-17 13:16:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testBeforeControllerOrder(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
$m2 = $this->getMiddleware();
|
|
|
|
|
|
|
|
$this->dispatcher->beforeController($this->controller, $this->method);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $m1->beforeControllerOrder);
|
|
|
|
$this->assertEquals(2, $m2->beforeControllerOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAfterControllerOrder(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
$m2 = $this->getMiddleware();
|
|
|
|
|
|
|
|
$this->dispatcher->afterController($this->controller, $this->method, $this->response);
|
|
|
|
|
|
|
|
$this->assertEquals(2, $m1->afterControllerOrder);
|
|
|
|
$this->assertEquals(1, $m2->afterControllerOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testAfterExceptionOrder(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
$m2 = $this->getMiddleware();
|
|
|
|
|
2018-01-24 20:10:16 +03:00
|
|
|
$this->expectException(\Exception::class);
|
2013-08-17 13:16:48 +04:00
|
|
|
$this->dispatcher->beforeController($this->controller, $this->method);
|
|
|
|
$this->dispatcher->afterException($this->controller, $this->method, $this->exception);
|
|
|
|
|
|
|
|
$this->assertEquals(1, $m1->afterExceptionOrder);
|
|
|
|
$this->assertEquals(1, $m2->afterExceptionOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testBeforeOutputOrder(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
$m2 = $this->getMiddleware();
|
|
|
|
|
2013-08-21 02:44:39 +04:00
|
|
|
$this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$this->assertEquals(2, $m1->beforeOutputOrder);
|
|
|
|
$this->assertEquals(1, $m2->beforeOutputOrder);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testExceptionShouldRunAfterExceptionOfOnlyPreviouslyExecutedMiddlewares(){
|
|
|
|
$m1 = $this->getMiddleware();
|
|
|
|
$m2 = $this->getMiddleware(true);
|
2016-07-10 15:17:26 +03:00
|
|
|
$m3 = $this->getMockBuilder('\OCP\AppFramework\Middleware')->getMock();
|
2013-08-17 13:16:48 +04:00
|
|
|
$m3->expects($this->never())
|
|
|
|
->method('afterException');
|
|
|
|
$m3->expects($this->never())
|
|
|
|
->method('beforeController');
|
|
|
|
$m3->expects($this->never())
|
|
|
|
->method('afterController');
|
2018-02-21 12:54:17 +03:00
|
|
|
$m3->method('beforeOutput')
|
|
|
|
->will($this->returnArgument(2));
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$this->dispatcher->registerMiddleware($m3);
|
|
|
|
|
2013-08-21 02:44:39 +04:00
|
|
|
$this->dispatcher->beforeOutput($this->controller, $this->method, $this->out);
|
2013-08-17 13:16:48 +04:00
|
|
|
|
|
|
|
$this->assertEquals(2, $m1->beforeOutputOrder);
|
|
|
|
$this->assertEquals(1, $m2->beforeOutputOrder);
|
|
|
|
}
|
|
|
|
}
|