RequestTestCase.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Georg Ehrke <oc.list@georgehrke.com>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. * @author Lukas Reschke <lukas@statuscode.ch>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;
  30. use OC\Files\View;
  31. use OCA\DAV\Connector\Sabre\Server;
  32. use OCA\DAV\Connector\Sabre\ServerFactory;
  33. use OCP\EventDispatcher\IEventDispatcher;
  34. use OCP\IConfig;
  35. use OCP\IRequest;
  36. use OCP\IRequestId;
  37. use Psr\Log\LoggerInterface;
  38. use Sabre\HTTP\Request;
  39. use Test\TestCase;
  40. use Test\Traits\MountProviderTrait;
  41. use Test\Traits\UserTrait;
  42. abstract class RequestTestCase extends TestCase {
  43. use UserTrait;
  44. use MountProviderTrait;
  45. /**
  46. * @var \OCA\DAV\Connector\Sabre\ServerFactory
  47. */
  48. protected $serverFactory;
  49. protected function getStream($string) {
  50. $stream = fopen('php://temp', 'r+');
  51. fwrite($stream, $string);
  52. fseek($stream, 0);
  53. return $stream;
  54. }
  55. protected function setUp(): void {
  56. parent::setUp();
  57. $this->serverFactory = new ServerFactory(
  58. \OC::$server->getConfig(),
  59. \OC::$server->get(LoggerInterface::class),
  60. \OC::$server->getDatabaseConnection(),
  61. \OC::$server->getUserSession(),
  62. \OC::$server->getMountManager(),
  63. \OC::$server->getTagManager(),
  64. $this->getMockBuilder(IRequest::class)
  65. ->disableOriginalConstructor()
  66. ->getMock(),
  67. \OC::$server->getPreviewManager(),
  68. \OC::$server->get(IEventDispatcher::class),
  69. \OC::$server->getL10N('dav')
  70. );
  71. }
  72. protected function setupUser($name, $password) {
  73. $this->createUser($name, $password);
  74. $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
  75. $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
  76. $this->loginAsUser($name);
  77. return new View('/' . $name . '/files');
  78. }
  79. /**
  80. * @param \OC\Files\View $view the view to run the webdav server against
  81. * @param string $user
  82. * @param string $password
  83. * @param string $method
  84. * @param string $url
  85. * @param resource|string|null $body
  86. * @param array|null $headers
  87. * @return \Sabre\HTTP\Response
  88. * @throws \Exception
  89. */
  90. protected function request($view, $user, $password, $method, $url, $body = null, $headers = []) {
  91. if (is_string($body)) {
  92. $body = $this->getStream($body);
  93. }
  94. $this->logout();
  95. $exceptionPlugin = new ExceptionPlugin('webdav', \OC::$server->get(LoggerInterface::class));
  96. $server = $this->getSabreServer($view, $user, $password, $exceptionPlugin);
  97. $request = new Request($method, $url, $headers, $body);
  98. // since sabre catches all exceptions we need to save them and throw them from outside the sabre server
  99. $serverParams = [];
  100. if (is_array($headers)) {
  101. foreach ($headers as $header => $value) {
  102. $serverParams['HTTP_' . strtoupper(str_replace('-', '_', $header))] = $value;
  103. }
  104. }
  105. $ncRequest = new \OC\AppFramework\Http\Request([
  106. 'server' => $serverParams
  107. ], $this->createMock(IRequestId::class), $this->createMock(IConfig::class), null);
  108. $this->overwriteService(IRequest::class, $ncRequest);
  109. $result = $this->makeRequest($server, $request);
  110. $this->restoreService(IRequest::class);
  111. foreach ($exceptionPlugin->getExceptions() as $exception) {
  112. throw $exception;
  113. }
  114. return $result;
  115. }
  116. /**
  117. * @param Server $server
  118. * @param Request $request
  119. * @return \Sabre\HTTP\Response
  120. */
  121. protected function makeRequest(Server $server, Request $request) {
  122. $sapi = new Sapi($request);
  123. $server->sapi = $sapi;
  124. $server->httpRequest = $request;
  125. $server->exec();
  126. return $sapi->getResponse();
  127. }
  128. /**
  129. * @param View $view
  130. * @param string $user
  131. * @param string $password
  132. * @param ExceptionPlugin $exceptionPlugin
  133. * @return Server
  134. */
  135. protected function getSabreServer(View $view, $user, $password, ExceptionPlugin $exceptionPlugin) {
  136. $authBackend = new Auth($user, $password);
  137. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  138. $server = $this->serverFactory->createServer('/', 'dummy', $authPlugin, function () use ($view) {
  139. return $view;
  140. });
  141. $server->addPlugin($exceptionPlugin);
  142. return $server;
  143. }
  144. }