DispatcherTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  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\Http;
  23. use OC\AppFramework\Http\Dispatcher;
  24. use OC\AppFramework\Http\Request;
  25. use OC\AppFramework\Middleware\MiddlewareDispatcher;
  26. use OC\AppFramework\Utility\ControllerMethodReflector;
  27. use OCP\AppFramework\Controller;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Http\JSONResponse;
  31. use OCP\AppFramework\Http\Response;
  32. use OCP\Diagnostics\IEventLogger;
  33. use OCP\IConfig;
  34. use OCP\IRequest;
  35. use OCP\IRequestId;
  36. use PHPUnit\Framework\MockObject\MockObject;
  37. use Psr\Container\ContainerInterface;
  38. use Psr\Log\LoggerInterface;
  39. class TestController extends Controller {
  40. /**
  41. * @param string $appName
  42. * @param \OCP\IRequest $request
  43. */
  44. public function __construct($appName, $request) {
  45. parent::__construct($appName, $request);
  46. }
  47. /**
  48. * @param int $int
  49. * @param bool $bool
  50. * @param double $foo
  51. * @param int $test
  52. * @param integer $test2
  53. * @return array
  54. */
  55. public function exec($int, $bool, $foo, $test = 4, $test2 = 1) {
  56. $this->registerResponder('text', function ($in) {
  57. return new JSONResponse(['text' => $in]);
  58. });
  59. return [$int, $bool, $test, $test2];
  60. }
  61. /**
  62. * @param int $int
  63. * @param bool $bool
  64. * @param int $test
  65. * @param int $test2
  66. * @return DataResponse
  67. */
  68. public function execDataResponse($int, $bool, $test = 4, $test2 = 1) {
  69. return new DataResponse([
  70. 'text' => [$int, $bool, $test, $test2]
  71. ]);
  72. }
  73. }
  74. /**
  75. * Class DispatcherTest
  76. *
  77. * @package Test\AppFramework\Http
  78. * @group DB
  79. */
  80. class DispatcherTest extends \Test\TestCase {
  81. /** @var MiddlewareDispatcher */
  82. private $middlewareDispatcher;
  83. /** @var Dispatcher */
  84. private $dispatcher;
  85. private $controllerMethod;
  86. /** @var Controller|MockObject */
  87. private $controller;
  88. private $response;
  89. /** @var IRequest|MockObject */
  90. private $request;
  91. private $lastModified;
  92. private $etag;
  93. /** @var Http|MockObject */
  94. private $http;
  95. private $reflector;
  96. /** @var IConfig|MockObject */
  97. private $config;
  98. /** @var LoggerInterface|MockObject */
  99. private $logger;
  100. /** @var IEventLogger|MockObject */
  101. private $eventLogger;
  102. /** @var ContainerInterface|MockObject */
  103. private $container;
  104. protected function setUp(): void {
  105. parent::setUp();
  106. $this->controllerMethod = 'test';
  107. $this->config = $this->createMock(IConfig::class);
  108. $this->logger = $this->createMock(LoggerInterface::class);
  109. $this->eventLogger = $this->createMock(IEventLogger::class);
  110. $this->container = $this->createMock(ContainerInterface::class);
  111. $app = $this->getMockBuilder(
  112. 'OC\AppFramework\DependencyInjection\DIContainer')
  113. ->disableOriginalConstructor()
  114. ->getMock();
  115. $request = $this->getMockBuilder(
  116. '\OC\AppFramework\Http\Request')
  117. ->disableOriginalConstructor()
  118. ->getMock();
  119. $this->http = $this->getMockBuilder(
  120. \OC\AppFramework\Http::class)
  121. ->disableOriginalConstructor()
  122. ->getMock();
  123. $this->middlewareDispatcher = $this->getMockBuilder(
  124. '\OC\AppFramework\Middleware\MiddlewareDispatcher')
  125. ->disableOriginalConstructor()
  126. ->getMock();
  127. $this->controller = $this->getMockBuilder(
  128. '\OCP\AppFramework\Controller')
  129. ->setMethods([$this->controllerMethod])
  130. ->setConstructorArgs([$app, $request])
  131. ->getMock();
  132. $this->request = $this->getMockBuilder(
  133. '\OC\AppFramework\Http\Request')
  134. ->disableOriginalConstructor()
  135. ->getMock();
  136. $this->reflector = new ControllerMethodReflector();
  137. $this->dispatcher = new Dispatcher(
  138. $this->http,
  139. $this->middlewareDispatcher,
  140. $this->reflector,
  141. $this->request,
  142. $this->config,
  143. \OC::$server->getDatabaseConnection(),
  144. $this->logger,
  145. $this->eventLogger,
  146. $this->container,
  147. );
  148. $this->response = $this->createMock(Response::class);
  149. $this->lastModified = new \DateTime('now', new \DateTimeZone('GMT'));
  150. $this->etag = 'hi';
  151. }
  152. /**
  153. * @param string $out
  154. * @param string $httpHeaders
  155. */
  156. private function setMiddlewareExpectations($out = null,
  157. $httpHeaders = null, $responseHeaders = [],
  158. $ex = false, $catchEx = true) {
  159. if ($ex) {
  160. $exception = new \Exception();
  161. $this->middlewareDispatcher->expects($this->once())
  162. ->method('beforeController')
  163. ->with($this->equalTo($this->controller),
  164. $this->equalTo($this->controllerMethod))
  165. ->will($this->throwException($exception));
  166. if ($catchEx) {
  167. $this->middlewareDispatcher->expects($this->once())
  168. ->method('afterException')
  169. ->with($this->equalTo($this->controller),
  170. $this->equalTo($this->controllerMethod),
  171. $this->equalTo($exception))
  172. ->willReturn($this->response);
  173. } else {
  174. $this->middlewareDispatcher->expects($this->once())
  175. ->method('afterException')
  176. ->with($this->equalTo($this->controller),
  177. $this->equalTo($this->controllerMethod),
  178. $this->equalTo($exception))
  179. ->willThrowException($exception);
  180. return;
  181. }
  182. } else {
  183. $this->middlewareDispatcher->expects($this->once())
  184. ->method('beforeController')
  185. ->with($this->equalTo($this->controller),
  186. $this->equalTo($this->controllerMethod));
  187. $this->controller->expects($this->once())
  188. ->method($this->controllerMethod)
  189. ->willReturn($this->response);
  190. }
  191. $this->response->expects($this->once())
  192. ->method('render')
  193. ->willReturn($out);
  194. $this->response->expects($this->once())
  195. ->method('getStatus')
  196. ->willReturn(Http::STATUS_OK);
  197. $this->response->expects($this->once())
  198. ->method('getHeaders')
  199. ->willReturn($responseHeaders);
  200. $this->http->expects($this->once())
  201. ->method('getStatusHeader')
  202. ->with($this->equalTo(Http::STATUS_OK))
  203. ->willReturn($httpHeaders);
  204. $this->middlewareDispatcher->expects($this->once())
  205. ->method('afterController')
  206. ->with($this->equalTo($this->controller),
  207. $this->equalTo($this->controllerMethod),
  208. $this->equalTo($this->response))
  209. ->willReturn($this->response);
  210. $this->middlewareDispatcher->expects($this->once())
  211. ->method('afterController')
  212. ->with($this->equalTo($this->controller),
  213. $this->equalTo($this->controllerMethod),
  214. $this->equalTo($this->response))
  215. ->willReturn($this->response);
  216. $this->middlewareDispatcher->expects($this->once())
  217. ->method('beforeOutput')
  218. ->with($this->equalTo($this->controller),
  219. $this->equalTo($this->controllerMethod),
  220. $this->equalTo($out))
  221. ->willReturn($out);
  222. }
  223. public function testDispatcherReturnsArrayWith2Entries() {
  224. $this->setMiddlewareExpectations('');
  225. $response = $this->dispatcher->dispatch($this->controller, $this->controllerMethod);
  226. $this->assertNull($response[0]);
  227. $this->assertEquals([], $response[1]);
  228. $this->assertNull($response[2]);
  229. }
  230. public function testHeadersAndOutputAreReturned() {
  231. $out = 'yo';
  232. $httpHeaders = 'Http';
  233. $responseHeaders = ['hell' => 'yeah'];
  234. $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders);
  235. $response = $this->dispatcher->dispatch($this->controller,
  236. $this->controllerMethod);
  237. $this->assertEquals($httpHeaders, $response[0]);
  238. $this->assertEquals($responseHeaders, $response[1]);
  239. $this->assertEquals($out, $response[3]);
  240. }
  241. public function testExceptionCallsAfterException() {
  242. $out = 'yo';
  243. $httpHeaders = 'Http';
  244. $responseHeaders = ['hell' => 'yeah'];
  245. $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders, true);
  246. $response = $this->dispatcher->dispatch($this->controller,
  247. $this->controllerMethod);
  248. $this->assertEquals($httpHeaders, $response[0]);
  249. $this->assertEquals($responseHeaders, $response[1]);
  250. $this->assertEquals($out, $response[3]);
  251. }
  252. public function testExceptionThrowsIfCanNotBeHandledByAfterException() {
  253. $out = 'yo';
  254. $httpHeaders = 'Http';
  255. $responseHeaders = ['hell' => 'yeah'];
  256. $this->setMiddlewareExpectations($out, $httpHeaders, $responseHeaders, true, false);
  257. $this->expectException(\Exception::class);
  258. $this->dispatcher->dispatch(
  259. $this->controller,
  260. $this->controllerMethod
  261. );
  262. }
  263. private function dispatcherPassthrough() {
  264. $this->middlewareDispatcher->expects($this->once())
  265. ->method('beforeController');
  266. $this->middlewareDispatcher->expects($this->once())
  267. ->method('afterController')
  268. ->willReturnCallback(function ($a, $b, $in) {
  269. return $in;
  270. });
  271. $this->middlewareDispatcher->expects($this->once())
  272. ->method('beforeOutput')
  273. ->willReturnCallback(function ($a, $b, $in) {
  274. return $in;
  275. });
  276. }
  277. public function testControllerParametersInjected() {
  278. $this->request = new Request(
  279. [
  280. 'post' => [
  281. 'int' => '3',
  282. 'bool' => 'false',
  283. 'double' => 1.2,
  284. ],
  285. 'method' => 'POST'
  286. ],
  287. $this->createMock(IRequestId::class),
  288. $this->createMock(IConfig::class)
  289. );
  290. $this->dispatcher = new Dispatcher(
  291. $this->http, $this->middlewareDispatcher, $this->reflector,
  292. $this->request,
  293. $this->config,
  294. \OC::$server->getDatabaseConnection(),
  295. $this->logger,
  296. $this->eventLogger,
  297. $this->container
  298. );
  299. $controller = new TestController('app', $this->request);
  300. // reflector is supposed to be called once
  301. $this->dispatcherPassthrough();
  302. $response = $this->dispatcher->dispatch($controller, 'exec');
  303. $this->assertEquals('[3,true,4,1]', $response[3]);
  304. }
  305. public function testControllerParametersInjectedDefaultOverwritten() {
  306. $this->request = new Request(
  307. [
  308. 'post' => [
  309. 'int' => '3',
  310. 'bool' => 'false',
  311. 'double' => 1.2,
  312. 'test2' => 7
  313. ],
  314. 'method' => 'POST',
  315. ],
  316. $this->createMock(IRequestId::class),
  317. $this->createMock(IConfig::class)
  318. );
  319. $this->dispatcher = new Dispatcher(
  320. $this->http, $this->middlewareDispatcher, $this->reflector,
  321. $this->request,
  322. $this->config,
  323. \OC::$server->getDatabaseConnection(),
  324. $this->logger,
  325. $this->eventLogger,
  326. $this->container
  327. );
  328. $controller = new TestController('app', $this->request);
  329. // reflector is supposed to be called once
  330. $this->dispatcherPassthrough();
  331. $response = $this->dispatcher->dispatch($controller, 'exec');
  332. $this->assertEquals('[3,true,4,7]', $response[3]);
  333. }
  334. public function testResponseTransformedByUrlFormat() {
  335. $this->request = new Request(
  336. [
  337. 'post' => [
  338. 'int' => '3',
  339. 'bool' => 'false',
  340. 'double' => 1.2,
  341. ],
  342. 'urlParams' => [
  343. 'format' => 'text'
  344. ],
  345. 'method' => 'GET'
  346. ],
  347. $this->createMock(IRequestId::class),
  348. $this->createMock(IConfig::class)
  349. );
  350. $this->dispatcher = new Dispatcher(
  351. $this->http, $this->middlewareDispatcher, $this->reflector,
  352. $this->request,
  353. $this->config,
  354. \OC::$server->getDatabaseConnection(),
  355. $this->logger,
  356. $this->eventLogger,
  357. $this->container
  358. );
  359. $controller = new TestController('app', $this->request);
  360. // reflector is supposed to be called once
  361. $this->dispatcherPassthrough();
  362. $response = $this->dispatcher->dispatch($controller, 'exec');
  363. $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
  364. }
  365. public function testResponseTransformsDataResponse() {
  366. $this->request = new Request(
  367. [
  368. 'post' => [
  369. 'int' => '3',
  370. 'bool' => 'false',
  371. 'double' => 1.2,
  372. ],
  373. 'urlParams' => [
  374. 'format' => 'json'
  375. ],
  376. 'method' => 'GET'
  377. ],
  378. $this->createMock(IRequestId::class),
  379. $this->createMock(IConfig::class)
  380. );
  381. $this->dispatcher = new Dispatcher(
  382. $this->http, $this->middlewareDispatcher, $this->reflector,
  383. $this->request,
  384. $this->config,
  385. \OC::$server->getDatabaseConnection(),
  386. $this->logger,
  387. $this->eventLogger,
  388. $this->container
  389. );
  390. $controller = new TestController('app', $this->request);
  391. // reflector is supposed to be called once
  392. $this->dispatcherPassthrough();
  393. $response = $this->dispatcher->dispatch($controller, 'execDataResponse');
  394. $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
  395. }
  396. public function testResponseTransformedByAcceptHeader() {
  397. $this->request = new Request(
  398. [
  399. 'post' => [
  400. 'int' => '3',
  401. 'bool' => 'false',
  402. 'double' => 1.2,
  403. ],
  404. 'server' => [
  405. 'HTTP_ACCEPT' => 'application/text, test',
  406. 'HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded'
  407. ],
  408. 'method' => 'PUT'
  409. ],
  410. $this->createMock(IRequestId::class),
  411. $this->createMock(IConfig::class)
  412. );
  413. $this->dispatcher = new Dispatcher(
  414. $this->http, $this->middlewareDispatcher, $this->reflector,
  415. $this->request,
  416. $this->config,
  417. \OC::$server->getDatabaseConnection(),
  418. $this->logger,
  419. $this->eventLogger,
  420. $this->container
  421. );
  422. $controller = new TestController('app', $this->request);
  423. // reflector is supposed to be called once
  424. $this->dispatcherPassthrough();
  425. $response = $this->dispatcher->dispatch($controller, 'exec');
  426. $this->assertEquals('{"text":[3,false,4,1]}', $response[3]);
  427. }
  428. public function testResponsePrimarilyTransformedByParameterFormat() {
  429. $this->request = new Request(
  430. [
  431. 'post' => [
  432. 'int' => '3',
  433. 'bool' => 'false',
  434. 'double' => 1.2,
  435. ],
  436. 'get' => [
  437. 'format' => 'text'
  438. ],
  439. 'server' => [
  440. 'HTTP_ACCEPT' => 'application/json, test'
  441. ],
  442. 'method' => 'POST'
  443. ],
  444. $this->createMock(IRequestId::class),
  445. $this->createMock(IConfig::class)
  446. );
  447. $this->dispatcher = new Dispatcher(
  448. $this->http, $this->middlewareDispatcher, $this->reflector,
  449. $this->request,
  450. $this->config,
  451. \OC::$server->getDatabaseConnection(),
  452. $this->logger,
  453. $this->eventLogger,
  454. $this->container
  455. );
  456. $controller = new TestController('app', $this->request);
  457. // reflector is supposed to be called once
  458. $this->dispatcherPassthrough();
  459. $response = $this->dispatcher->dispatch($controller, 'exec');
  460. $this->assertEquals('{"text":[3,true,4,1]}', $response[3]);
  461. }
  462. }