1
0

DispatcherTest.php 14 KB

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