RequestTestCase.php 4.5 KB

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