DiscoveryServiceTest.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\OCS;
  7. use OC\OCS\DiscoveryService;
  8. use OCP\Http\Client\IClientService;
  9. use OCP\ICacheFactory;
  10. use OCP\OCS\IDiscoveryService;
  11. use Test\TestCase;
  12. class DiscoveryServiceTest extends TestCase {
  13. /** @var \PHPUnit\Framework\MockObject\MockObject | ICacheFactory */
  14. private $cacheFactory;
  15. /** @var \PHPUnit\Framework\MockObject\MockObject | IClientService */
  16. private $clientService;
  17. /** @var IDiscoveryService */
  18. private $discoveryService;
  19. protected function setUp(): void {
  20. parent::setUp();
  21. $this->cacheFactory = $this->getMockBuilder(ICacheFactory::class)->getMock();
  22. $this->clientService = $this->getMockBuilder(IClientService::class)->getMock();
  23. $this->discoveryService = new DiscoveryService(
  24. $this->cacheFactory,
  25. $this->clientService
  26. );
  27. }
  28. /**
  29. * @dataProvider dataTestIsSafeUrl
  30. *
  31. * @param string $url
  32. * @param bool $expected
  33. */
  34. public function testIsSafeUrl($url, $expected) {
  35. $result = $this->invokePrivate($this->discoveryService, 'isSafeUrl', [$url]);
  36. $this->assertSame($expected, $result);
  37. }
  38. public function dataTestIsSafeUrl() {
  39. return [
  40. ['api/ocs/v1.php/foo', true],
  41. ['/api/ocs/v1.php/foo', true],
  42. ['api/ocs/v1.php/foo/', true],
  43. ['api/ocs/v1.php/foo-bar/', true],
  44. ['api/ocs/v1:php/foo', false],
  45. ['api/ocs/<v1.php/foo', false],
  46. ['api/ocs/v1.php>/foo', false],
  47. ];
  48. }
  49. /**
  50. * @dataProvider dataTestGetEndpoints
  51. *
  52. * @param array $decodedServices
  53. * @param string $service
  54. * @param array $expected
  55. */
  56. public function testGetEndpoints($decodedServices, $service, $expected) {
  57. $result = $this->invokePrivate($this->discoveryService, 'getEndpoints', [$decodedServices, $service]);
  58. $this->assertSame($expected, $result);
  59. }
  60. public function dataTestGetEndpoints() {
  61. return [
  62. [['services' => ['myService' => ['endpoints' => []]]], 'myService', []],
  63. [['services' => ['myService' => ['endpoints' => ['foo' => '/bar']]]], 'myService', ['foo' => '/bar']],
  64. [['services' => ['myService' => ['endpoints' => ['foo' => '/bar']]]], 'anotherService', []],
  65. [['services' => ['myService' => ['endpoints' => ['foo' => '/bar</foo']]]], 'myService', []],
  66. ];
  67. }
  68. }