DiscoveryServiceTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Bjoern Schiessle <bjoern@schiessle.org>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\OCS;
  22. use OC\OCS\DiscoveryService;
  23. use OCP\Http\Client\IClientService;
  24. use OCP\ICacheFactory;
  25. use OCP\OCS\IDiscoveryService;
  26. use Test\TestCase;
  27. class DiscoveryServiceTest extends TestCase {
  28. /** @var \PHPUnit_Framework_MockObject_MockObject | ICacheFactory */
  29. private $cacheFactory;
  30. /** @var \PHPUnit_Framework_MockObject_MockObject | IClientService */
  31. private $clientService;
  32. /** @var IDiscoveryService */
  33. private $discoveryService;
  34. public function setUp() {
  35. parent::setUp();
  36. $this->cacheFactory = $this->getMockBuilder(ICacheFactory::class)->getMock();
  37. $this->clientService = $this->getMockBuilder(IClientService::class)->getMock();
  38. $this->discoveryService = new DiscoveryService(
  39. $this->cacheFactory,
  40. $this->clientService
  41. );
  42. }
  43. /**
  44. * @dataProvider dataTestIsSafeUrl
  45. *
  46. * @param string $url
  47. * @param bool $expected
  48. */
  49. public function testIsSafeUrl($url, $expected) {
  50. $result = $this->invokePrivate($this->discoveryService, 'isSafeUrl', [$url]);
  51. $this->assertSame($expected, $result);
  52. }
  53. public function dataTestIsSafeUrl() {
  54. return [
  55. ['api/ocs/v1.php/foo', true],
  56. ['/api/ocs/v1.php/foo', true],
  57. ['api/ocs/v1.php/foo/', true],
  58. ['api/ocs/v1.php/foo-bar/', true],
  59. ['api/ocs/v1:php/foo', false],
  60. ['api/ocs/<v1.php/foo', false],
  61. ['api/ocs/v1.php>/foo', false],
  62. ];
  63. }
  64. /**
  65. * @dataProvider dataTestGetEndpoints
  66. *
  67. * @param array $decodedServices
  68. * @param string $service
  69. * @param array $expected
  70. */
  71. public function testGetEndpoints($decodedServices, $service, $expected) {
  72. $result = $this->invokePrivate($this->discoveryService, 'getEndpoints', [$decodedServices, $service]);
  73. $this->assertSame($expected, $result);
  74. }
  75. public function dataTestGetEndpoints() {
  76. return [
  77. [['services' => ['myService' => ['endpoints' => []]]], 'myService', []],
  78. [['services' => ['myService' => ['endpoints' => ['foo' => '/bar']]]], 'myService', ['foo' => '/bar']],
  79. [['services' => ['myService' => ['endpoints' => ['foo' => '/bar']]]], 'anotherService', []],
  80. [['services' => ['myService' => ['endpoints' => ['foo' => '/bar</foo']]]], 'myService', []],
  81. ];
  82. }
  83. }