AnonymousOptionsTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OCA\DAV\tests\unit\DAV;
  7. use OCA\DAV\Connector\Sabre\AnonymousOptionsPlugin;
  8. use Sabre\DAV\Auth\Backend\BasicCallBack;
  9. use Sabre\DAV\Auth\Plugin;
  10. use Sabre\DAV\Server;
  11. use Sabre\HTTP\ResponseInterface;
  12. use Sabre\HTTP\Sapi;
  13. use Test\TestCase;
  14. class AnonymousOptionsTest extends TestCase {
  15. private function sendRequest($method, $path, $userAgent = '') {
  16. $server = new Server();
  17. $server->addPlugin(new AnonymousOptionsPlugin());
  18. $server->addPlugin(new Plugin(new BasicCallBack(function () {
  19. return false;
  20. })));
  21. $server->httpRequest->setMethod($method);
  22. $server->httpRequest->setUrl($path);
  23. $server->httpRequest->setHeader('User-Agent', $userAgent);
  24. $server->sapi = new SapiMock();
  25. $server->exec();
  26. return $server->httpResponse;
  27. }
  28. public function testAnonymousOptionsRoot(): void {
  29. $response = $this->sendRequest('OPTIONS', '');
  30. $this->assertEquals(401, $response->getStatus());
  31. }
  32. public function testAnonymousOptionsNonRoot(): void {
  33. $response = $this->sendRequest('OPTIONS', 'foo');
  34. $this->assertEquals(401, $response->getStatus());
  35. }
  36. public function testAnonymousOptionsNonRootSubDir(): void {
  37. $response = $this->sendRequest('OPTIONS', 'foo/bar');
  38. $this->assertEquals(401, $response->getStatus());
  39. }
  40. public function testAnonymousOptionsRootOffice(): void {
  41. $response = $this->sendRequest('OPTIONS', '', 'Microsoft Office does strange things');
  42. $this->assertEquals(200, $response->getStatus());
  43. }
  44. public function testAnonymousOptionsNonRootOffice(): void {
  45. $response = $this->sendRequest('OPTIONS', 'foo', 'Microsoft Office does strange things');
  46. $this->assertEquals(200, $response->getStatus());
  47. }
  48. public function testAnonymousOptionsNonRootSubDirOffice(): void {
  49. $response = $this->sendRequest('OPTIONS', 'foo/bar', 'Microsoft Office does strange things');
  50. $this->assertEquals(200, $response->getStatus());
  51. }
  52. public function testAnonymousHead(): void {
  53. $response = $this->sendRequest('HEAD', '', 'Microsoft Office does strange things');
  54. $this->assertEquals(200, $response->getStatus());
  55. }
  56. public function testAnonymousHeadNoOffice(): void {
  57. $response = $this->sendRequest('HEAD', '');
  58. $this->assertEquals(401, $response->getStatus(), 'curl');
  59. }
  60. }
  61. class SapiMock extends Sapi {
  62. /**
  63. * Overriding this so nothing is ever echo'd.
  64. *
  65. * @return void
  66. */
  67. public static function sendResponse(ResponseInterface $response): void {
  68. }
  69. }