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.

RequestTest.php 4.2KB

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