DispatcherTest.php 13 KB

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