RequestTestCase.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\Tests\unit\Connector\Sabre\RequestTest;
  8. use OC\Files\View;
  9. use OCA\DAV\Connector\Sabre\Server;
  10. use OCA\DAV\Connector\Sabre\ServerFactory;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\IConfig;
  13. use OCP\IRequest;
  14. use OCP\IRequestId;
  15. use Psr\Log\LoggerInterface;
  16. use Sabre\HTTP\Request;
  17. use Test\TestCase;
  18. use Test\Traits\MountProviderTrait;
  19. use Test\Traits\UserTrait;
  20. abstract class RequestTestCase extends TestCase {
  21. use UserTrait;
  22. use MountProviderTrait;
  23. /**
  24. * @var ServerFactory
  25. */
  26. protected $serverFactory;
  27. protected function getStream($string) {
  28. $stream = fopen('php://temp', 'r+');
  29. fwrite($stream, $string);
  30. fseek($stream, 0);
  31. return $stream;
  32. }
  33. protected function setUp(): void {
  34. parent::setUp();
  35. $this->serverFactory = new ServerFactory(
  36. \OC::$server->getConfig(),
  37. \OC::$server->get(LoggerInterface::class),
  38. \OC::$server->getDatabaseConnection(),
  39. \OC::$server->getUserSession(),
  40. \OC::$server->getMountManager(),
  41. \OC::$server->getTagManager(),
  42. $this->getMockBuilder(IRequest::class)
  43. ->disableOriginalConstructor()
  44. ->getMock(),
  45. \OC::$server->getPreviewManager(),
  46. \OC::$server->get(IEventDispatcher::class),
  47. \OC::$server->getL10N('dav')
  48. );
  49. }
  50. protected function setupUser($name, $password) {
  51. $this->createUser($name, $password);
  52. $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
  53. $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
  54. $this->loginAsUser($name);
  55. return new View('/' . $name . '/files');
  56. }
  57. /**
  58. * @param View $view the view to run the webdav server against
  59. * @param string $user
  60. * @param string $password
  61. * @param string $method
  62. * @param string $url
  63. * @param resource|string|null $body
  64. * @param array|null $headers
  65. * @return \Sabre\HTTP\Response
  66. * @throws \Exception
  67. */
  68. protected function request($view, $user, $password, $method, $url, $body = null, $headers = []) {
  69. if (is_string($body)) {
  70. $body = $this->getStream($body);
  71. }
  72. $this->logout();
  73. $exceptionPlugin = new ExceptionPlugin('webdav', \OC::$server->get(LoggerInterface::class));
  74. $server = $this->getSabreServer($view, $user, $password, $exceptionPlugin);
  75. $request = new Request($method, $url, $headers, $body);
  76. // since sabre catches all exceptions we need to save them and throw them from outside the sabre server
  77. $serverParams = [];
  78. if (is_array($headers)) {
  79. foreach ($headers as $header => $value) {
  80. $serverParams['HTTP_' . strtoupper(str_replace('-', '_', $header))] = $value;
  81. }
  82. }
  83. $ncRequest = new \OC\AppFramework\Http\Request([
  84. 'server' => $serverParams
  85. ], $this->createMock(IRequestId::class), $this->createMock(IConfig::class), null);
  86. $this->overwriteService(IRequest::class, $ncRequest);
  87. $result = $this->makeRequest($server, $request);
  88. $this->restoreService(IRequest::class);
  89. foreach ($exceptionPlugin->getExceptions() as $exception) {
  90. throw $exception;
  91. }
  92. return $result;
  93. }
  94. /**
  95. * @param Server $server
  96. * @param Request $request
  97. * @return \Sabre\HTTP\Response
  98. */
  99. protected function makeRequest(Server $server, Request $request) {
  100. $sapi = new Sapi($request);
  101. $server->sapi = $sapi;
  102. $server->httpRequest = $request;
  103. $server->exec();
  104. return $sapi->getResponse();
  105. }
  106. /**
  107. * @param View $view
  108. * @param string $user
  109. * @param string $password
  110. * @param ExceptionPlugin $exceptionPlugin
  111. * @return Server
  112. */
  113. protected function getSabreServer(View $view, $user, $password, ExceptionPlugin $exceptionPlugin) {
  114. $authBackend = new Auth($user, $password);
  115. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  116. $server = $this->serverFactory->createServer('/', 'dummy', $authPlugin, function () use ($view) {
  117. return $view;
  118. });
  119. $server->addPlugin($exceptionPlugin);
  120. return $server;
  121. }
  122. }