DispatcherTest.php 15 KB

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