You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RequestTestCase.php 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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\IRequest;
  34. use Psr\Log\LoggerInterface;
  35. use Sabre\HTTP\Request;
  36. use Test\TestCase;
  37. use Test\Traits\MountProviderTrait;
  38. use Test\Traits\UserTrait;
  39. abstract class RequestTestCase extends TestCase {
  40. use UserTrait;
  41. use MountProviderTrait;
  42. /**
  43. * @var \OCA\DAV\Connector\Sabre\ServerFactory
  44. */
  45. protected $serverFactory;
  46. protected function getStream($string) {
  47. $stream = fopen('php://temp', 'r+');
  48. fwrite($stream, $string);
  49. fseek($stream, 0);
  50. return $stream;
  51. }
  52. protected function setUp(): void {
  53. parent::setUp();
  54. unset($_SERVER['HTTP_OC_CHUNKED']);
  55. $this->serverFactory = new ServerFactory(
  56. \OC::$server->getConfig(),
  57. \OC::$server->get(LoggerInterface::class),
  58. \OC::$server->getDatabaseConnection(),
  59. \OC::$server->getUserSession(),
  60. \OC::$server->getMountManager(),
  61. \OC::$server->getTagManager(),
  62. $this->getMockBuilder(IRequest::class)
  63. ->disableOriginalConstructor()
  64. ->getMock(),
  65. \OC::$server->getPreviewManager(),
  66. \OC::$server->getEventDispatcher(),
  67. \OC::$server->getL10N('dav')
  68. );
  69. }
  70. protected function setupUser($name, $password) {
  71. $this->createUser($name, $password);
  72. $tmpFolder = \OC::$server->getTempManager()->getTemporaryFolder();
  73. $this->registerMount($name, '\OC\Files\Storage\Local', '/' . $name, ['datadir' => $tmpFolder]);
  74. $this->loginAsUser($name);
  75. return new View('/' . $name . '/files');
  76. }
  77. /**
  78. * @param \OC\Files\View $view the view to run the webdav server against
  79. * @param string $user
  80. * @param string $password
  81. * @param string $method
  82. * @param string $url
  83. * @param resource|string|null $body
  84. * @param array|null $headers
  85. * @return \Sabre\HTTP\Response
  86. * @throws \Exception
  87. */
  88. protected function request($view, $user, $password, $method, $url, $body = null, $headers = []) {
  89. if (is_string($body)) {
  90. $body = $this->getStream($body);
  91. }
  92. $this->logout();
  93. $exceptionPlugin = new ExceptionPlugin('webdav', \OC::$server->get(LoggerInterface::class));
  94. $server = $this->getSabreServer($view, $user, $password, $exceptionPlugin);
  95. $request = new Request($method, $url, $headers, $body);
  96. // since sabre catches all exceptions we need to save them and throw them from outside the sabre server
  97. $originalServer = $_SERVER;
  98. if (is_array($headers)) {
  99. foreach ($headers as $header => $value) {
  100. $_SERVER['HTTP_' . strtoupper(str_replace('-', '_', $header))] = $value;
  101. }
  102. }
  103. $result = $this->makeRequest($server, $request);
  104. foreach ($exceptionPlugin->getExceptions() as $exception) {
  105. throw $exception;
  106. }
  107. $_SERVER = $originalServer;
  108. return $result;
  109. }
  110. /**
  111. * @param Server $server
  112. * @param Request $request
  113. * @return \Sabre\HTTP\Response
  114. */
  115. protected function makeRequest(Server $server, Request $request) {
  116. $sapi = new Sapi($request);
  117. $server->sapi = $sapi;
  118. $server->httpRequest = $request;
  119. $server->exec();
  120. return $sapi->getResponse();
  121. }
  122. /**
  123. * @param View $view
  124. * @param string $user
  125. * @param string $password
  126. * @param ExceptionPlugin $exceptionPlugin
  127. * @return Server
  128. */
  129. protected function getSabreServer(View $view, $user, $password, ExceptionPlugin $exceptionPlugin) {
  130. $authBackend = new Auth($user, $password);
  131. $authPlugin = new \Sabre\DAV\Auth\Plugin($authBackend);
  132. $server = $this->serverFactory->createServer('/', 'dummy', $authPlugin, function () use ($view) {
  133. return $view;
  134. });
  135. $server->addPlugin($exceptionPlugin);
  136. return $server;
  137. }
  138. }