DispatcherTest.php 15 KB

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