ControllerTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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\Controller;
  23. use OC\AppFramework\Http\Request;
  24. use OCP\AppFramework\Controller;
  25. use OCP\AppFramework\Http\TemplateResponse;
  26. use OCP\AppFramework\Http\JSONResponse;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\IConfig;
  29. class ChildController extends Controller {
  30. public function __construct($appName, $request) {
  31. parent::__construct($appName, $request);
  32. $this->registerResponder('tom', function ($respone) {
  33. return 'hi';
  34. });
  35. }
  36. public function custom($in) {
  37. $this->registerResponder('json', function ($response) {
  38. return new JSONResponse(array(strlen($response)));
  39. });
  40. return $in;
  41. }
  42. public function customDataResponse($in) {
  43. $response = new DataResponse($in, 300);
  44. $response->addHeader('test', 'something');
  45. return $response;
  46. }
  47. };
  48. class ControllerTest extends \Test\TestCase {
  49. /**
  50. * @var Controller
  51. */
  52. private $controller;
  53. private $app;
  54. protected function setUp(){
  55. parent::setUp();
  56. $request = new Request(
  57. [
  58. 'get' => ['name' => 'John Q. Public', 'nickname' => 'Joey'],
  59. 'post' => ['name' => 'Jane Doe', 'nickname' => 'Janey'],
  60. 'urlParams' => ['name' => 'Johnny Weissmüller'],
  61. 'files' => ['file' => 'filevalue'],
  62. 'env' => ['PATH' => 'daheim'],
  63. 'session' => ['sezession' => 'kein'],
  64. 'method' => 'hi',
  65. ],
  66. $this->getMockBuilder('\OCP\Security\ISecureRandom')
  67. ->disableOriginalConstructor()
  68. ->getMock(),
  69. $this->getMockBuilder(IConfig::class)
  70. ->disableOriginalConstructor()
  71. ->getMock()
  72. );
  73. $this->app = $this->getMockBuilder('OC\AppFramework\DependencyInjection\DIContainer')
  74. ->setMethods(['getAppName'])
  75. ->setConstructorArgs(['test'])
  76. ->getMock();
  77. $this->app->expects($this->any())
  78. ->method('getAppName')
  79. ->will($this->returnValue('apptemplate_advanced'));
  80. $this->controller = new ChildController($this->app, $request);
  81. }
  82. /**
  83. * @expectedException \DomainException
  84. */
  85. public function testFormatResonseInvalidFormat() {
  86. $this->controller->buildResponse(null, 'test');
  87. }
  88. public function testFormat() {
  89. $response = $this->controller->buildResponse(array('hi'), 'json');
  90. $this->assertEquals(array('hi'), $response->getData());
  91. }
  92. public function testFormatDataResponseJSON() {
  93. $expectedHeaders = [
  94. 'test' => 'something',
  95. 'Cache-Control' => 'no-cache, no-store, must-revalidate',
  96. 'Content-Type' => 'application/json; charset=utf-8',
  97. 'Content-Security-Policy' => "default-src 'none';base-uri 'none';manifest-src 'self';script-src 'self';style-src 'self' 'unsafe-inline';img-src 'self' data: blob:;font-src 'self' data:;connect-src 'self';media-src 'self';frame-ancestors 'self'",
  98. ];
  99. $response = $this->controller->customDataResponse(array('hi'));
  100. $response = $this->controller->buildResponse($response, 'json');
  101. $this->assertEquals(array('hi'), $response->getData());
  102. $this->assertEquals(300, $response->getStatus());
  103. $this->assertEquals($expectedHeaders, $response->getHeaders());
  104. }
  105. public function testCustomFormatter() {
  106. $response = $this->controller->custom('hi');
  107. $response = $this->controller->buildResponse($response, 'json');
  108. $this->assertEquals(array(2), $response->getData());
  109. }
  110. public function testDefaultResponderToJSON() {
  111. $responder = $this->controller->getResponderByHTTPHeader('*/*');
  112. $this->assertEquals('json', $responder);
  113. }
  114. public function testResponderAcceptHeaderParsed() {
  115. $responder = $this->controller->getResponderByHTTPHeader(
  116. '*/*, application/tom, application/json'
  117. );
  118. $this->assertEquals('tom', $responder);
  119. }
  120. public function testResponderAcceptHeaderParsedUpperCase() {
  121. $responder = $this->controller->getResponderByHTTPHeader(
  122. '*/*, apPlication/ToM, application/json'
  123. );
  124. $this->assertEquals('tom', $responder);
  125. }
  126. }