DispatcherTest.php 13 KB

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