ControllerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 OCP\AppFramework;
  23. use OC\AppFramework\Http\Request;
  24. use OCP\AppFramework\Http\TemplateResponse;
  25. use OCP\AppFramework\Http\JSONResponse;
  26. use OCP\AppFramework\Http\DataResponse;
  27. class ChildController extends Controller {
  28. public function __construct($appName, $request) {
  29. parent::__construct($appName, $request);
  30. $this->registerResponder('tom', function ($respone) {
  31. return 'hi';
  32. });
  33. }
  34. public function custom($in) {
  35. $this->registerResponder('json', function ($response) {
  36. return new JSONResponse(array(strlen($response)));
  37. });
  38. return $in;
  39. }
  40. public function customDataResponse($in) {
  41. $response = new DataResponse($in, 300);
  42. $response->addHeader('test', 'something');
  43. return $response;
  44. }
  45. };
  46. class ControllerTest extends \Test\TestCase {
  47. /**
  48. * @var Controller
  49. */
  50. private $controller;
  51. private $app;
  52. protected function setUp(){
  53. parent::setUp();
  54. $request = new Request(
  55. [
  56. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  57. 'post' => ['name' => 'Jane Doe', 'nickname' => 'Janey'],
  58. 'urlParams' => ['name' => 'Johnny Weissmüller'],
  59. 'files' => ['file' => 'filevalue'],
  60. 'env' => ['PATH' => 'daheim'],
  61. 'session' => ['sezession' => 'kein'],
  62. 'method' => 'hi',
  63. ],
  64. $this->getMock('\OCP\Security\ISecureRandom'),
  65. $this->getMock('\OCP\IConfig')
  66. );
  67. $this->app = $this->getMock('OC\AppFramework\DependencyInjection\DIContainer',
  68. array('getAppName'), array('test'));
  69. $this->app->expects($this->any())
  70. ->method('getAppName')
  71. ->will($this->returnValue('apptemplate_advanced'));
  72. $this->controller = new ChildController($this->app, $request);
  73. }
  74. public function testParamsGet(){
  75. $this->assertEquals('Johnny Weissmüller', $this->controller->params('name', 'Tarzan'));
  76. }
  77. public function testParamsGetDefault(){
  78. $this->assertEquals('Tarzan', $this->controller->params('Ape Man', 'Tarzan'));
  79. }
  80. public function testParamsFile(){
  81. $this->assertEquals('filevalue', $this->controller->params('file', 'filevalue'));
  82. }
  83. public function testGetUploadedFile(){
  84. $this->assertEquals('filevalue', $this->controller->getUploadedFile('file'));
  85. }
  86. public function testGetUploadedFileDefault(){
  87. $this->assertEquals('default', $this->controller->params('files', 'default'));
  88. }
  89. public function testGetParams(){
  90. $params = array(
  91. 'name' => 'Johnny Weissmüller',
  92. 'nickname' => 'Janey',
  93. );
  94. $this->assertEquals($params, $this->controller->getParams());
  95. }
  96. public function testRender(){
  97. $this->assertTrue($this->controller->render('') instanceof TemplateResponse);
  98. }
  99. public function testSetParams(){
  100. $params = array('john' => 'foo');
  101. $response = $this->controller->render('home', $params);
  102. $this->assertEquals($params, $response->getParams());
  103. }
  104. public function testRenderHeaders(){
  105. $headers = array('one', 'two');
  106. $response = $this->controller->render('', array(), '', $headers);
  107. $this->assertTrue(in_array($headers[0], $response->getHeaders()));
  108. $this->assertTrue(in_array($headers[1], $response->getHeaders()));
  109. }
  110. public function testGetRequestMethod(){
  111. $this->assertEquals('hi', $this->controller->method());
  112. }
  113. public function testGetEnvVariable(){
  114. $this->assertEquals('daheim', $this->controller->env('PATH'));
  115. }
  116. /**
  117. * @expectedException \DomainException
  118. */
  119. public function testFormatResonseInvalidFormat() {
  120. $this->controller->buildResponse(null, 'test');
  121. }
  122. public function testFormat() {
  123. $response = $this->controller->buildResponse(array('hi'), 'json');
  124. $this->assertEquals(array('hi'), $response->getData());
  125. }
  126. public function testFormatDataResponseJSON() {
  127. $expectedHeaders = [
  128. 'test' => 'something',
  129. 'Cache-Control' => 'no-cache, must-revalidate',
  130. 'Content-Type' => 'application/json; charset=utf-8',
  131. 'Content-Security-Policy' => "default-src 'none';script-src 'self' 'unsafe-eval';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self';connect-src 'self';media-src 'self'",
  132. ];
  133. $response = $this->controller->customDataResponse(array('hi'));
  134. $response = $this->controller->buildResponse($response, 'json');
  135. $this->assertEquals(array('hi'), $response->getData());
  136. $this->assertEquals(300, $response->getStatus());
  137. $this->assertEquals($expectedHeaders, $response->getHeaders());
  138. }
  139. public function testCustomFormatter() {
  140. $response = $this->controller->custom('hi');
  141. $response = $this->controller->buildResponse($response, 'json');
  142. $this->assertEquals(array(2), $response->getData());
  143. }
  144. public function testDefaultResponderToJSON() {
  145. $responder = $this->controller->getResponderByHTTPHeader('*/*');
  146. $this->assertEquals('json', $responder);
  147. }
  148. public function testResponderAcceptHeaderParsed() {
  149. $responder = $this->controller->getResponderByHTTPHeader(
  150. '*/*, application/tom, application/json'
  151. );
  152. $this->assertEquals('tom', $responder);
  153. }
  154. public function testResponderAcceptHeaderParsedUpperCase() {
  155. $responder = $this->controller->getResponderByHTTPHeader(
  156. '*/*, apPlication/ToM, application/json'
  157. );
  158. $this->assertEquals('tom', $responder);
  159. }
  160. }