diff options
Diffstat (limited to 'tests/lib/Http/Client')
-rw-r--r-- | tests/lib/Http/Client/ClientServiceTest.php | 23 | ||||
-rw-r--r-- | tests/lib/Http/Client/ClientTest.php | 147 | ||||
-rw-r--r-- | tests/lib/Http/Client/DnsPinMiddlewareTest.php | 58 | ||||
-rw-r--r-- | tests/lib/Http/Client/NegativeDnsCacheTest.php | 29 | ||||
-rw-r--r-- | tests/lib/Http/Client/ResponseTest.php | 16 |
5 files changed, 108 insertions, 165 deletions
diff --git a/tests/lib/Http/Client/ClientServiceTest.php b/tests/lib/Http/Client/ClientServiceTest.php index 3aae7ceae25..fd5b155ca69 100644 --- a/tests/lib/Http/Client/ClientServiceTest.php +++ b/tests/lib/Http/Client/ClientServiceTest.php @@ -3,10 +3,9 @@ declare(strict_types=1); /** - * Copyright (c) 2015 Lukas Reschke <lukas@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Http\Client; @@ -41,7 +40,7 @@ class ClientServiceTest extends \Test\TestCase { $dnsPinMiddleware ->expects($this->atLeastOnce()) ->method('addDnsPinning') - ->willReturn(function () { + ->willReturn(function (): void { }); $remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $eventLogger = $this->createMock(IEventLogger::class); @@ -59,9 +58,9 @@ class ClientServiceTest extends \Test\TestCase { $handler = new CurlHandler(); $stack = HandlerStack::create($handler); $stack->push($dnsPinMiddleware->addDnsPinning()); - $stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger) { - $eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget()); - }, function () use ($eventLogger) { + $stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger): void { + $eventLogger->start('http:request', $request->getMethod() . ' request to ' . $request->getRequestTarget()); + }, function () use ($eventLogger): void { $eventLogger->end('http:request'); }), 'event logger'); $guzzleClient = new GuzzleClient(['handler' => $stack]); @@ -90,7 +89,7 @@ class ClientServiceTest extends \Test\TestCase { $dnsPinMiddleware ->expects($this->never()) ->method('addDnsPinning') - ->willReturn(function () { + ->willReturn(function (): void { }); $remoteHostValidator = $this->createMock(IRemoteHostValidator::class); $eventLogger = $this->createMock(IEventLogger::class); @@ -107,9 +106,9 @@ class ClientServiceTest extends \Test\TestCase { $handler = new CurlHandler(); $stack = HandlerStack::create($handler); - $stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger) { - $eventLogger->start('http:request', $request->getMethod() . " request to " . $request->getRequestTarget()); - }, function () use ($eventLogger) { + $stack->push(Middleware::tap(function (RequestInterface $request) use ($eventLogger): void { + $eventLogger->start('http:request', $request->getMethod() . ' request to ' . $request->getRequestTarget()); + }, function () use ($eventLogger): void { $eventLogger->end('http:request'); }), 'event logger'); $guzzleClient = new GuzzleClient(['handler' => $stack]); diff --git a/tests/lib/Http/Client/ClientTest.php b/tests/lib/Http/Client/ClientTest.php index 0e6e265584e..e76b66b52d7 100644 --- a/tests/lib/Http/Client/ClientTest.php +++ b/tests/lib/Http/Client/ClientTest.php @@ -3,10 +3,9 @@ declare(strict_types=1); /** - * Copyright (c) 2015 Lukas Reschke <lukas@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Http\Client; @@ -67,16 +66,16 @@ class ClientTest extends \Test\TestCase { public function testGetProxyUriProxyHostEmptyPassword(): void { $this->config ->method('getSystemValue') - ->will($this->returnValueMap([ + ->willReturnMap([ ['proxyexclude', [], []], - ])); + ]); $this->config ->method('getSystemValueString') - ->will($this->returnValueMap([ + ->willReturnMap([ ['proxy', '', 'foo'], ['proxyuserpwd', '', ''], - ])); + ]); $this->assertEquals([ 'http' => 'foo', @@ -93,14 +92,10 @@ class ClientTest extends \Test\TestCase { $this->config ->expects($this->exactly(2)) ->method('getSystemValueString') - ->withConsecutive( - ['proxy', ''], - ['proxyuserpwd', ''], - ) - ->willReturnOnConsecutiveCalls( - 'foo', - 'username:password', - ); + ->willReturnMap([ + ['proxy', '', 'foo'], + ['proxyuserpwd', '', 'username:password'], + ]); $this->assertEquals([ 'http' => 'username:password@foo', 'https' => 'username:password@foo' @@ -116,14 +111,10 @@ class ClientTest extends \Test\TestCase { $this->config ->expects($this->exactly(2)) ->method('getSystemValueString') - ->withConsecutive( - ['proxy', ''], - ['proxyuserpwd', ''], - ) - ->willReturnOnConsecutiveCalls( - 'foo', - 'username:password', - ); + ->willReturnMap([ + ['proxy', '', 'foo'], + ['proxyuserpwd', '', 'username:password'], + ]); $this->assertEquals([ 'http' => 'username:password@foo', 'https' => 'username:password@foo', @@ -131,7 +122,14 @@ class ClientTest extends \Test\TestCase { ], self::invokePrivate($this->client, 'getProxyUri')); } - public function dataPreventLocalAddress():array { + public function testPreventLocalAddressThrowOnInvalidUri(): void { + $this->expectException(LocalServerException::class); + $this->expectExceptionMessage('Could not detect any host'); + + self::invokePrivate($this->client, 'preventLocalAddress', ['!@#$', []]); + } + + public static function dataPreventLocalAddress(): array { return [ ['https://localhost/foo.bar'], ['https://localHost/foo.bar'], @@ -147,16 +145,15 @@ class ClientTest extends \Test\TestCase { ['https://10.0.0.1'], ['https://another-host.local'], ['https://service.localhost'], - ['!@#$', true], // test invalid url ['https://normal.host.com'], ['https://com.one-.nextcloud-one.com'], ]; } /** - * @dataProvider dataPreventLocalAddress * @param string $uri */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataPreventLocalAddress')] public function testPreventLocalAddressDisabledByGlobalConfig(string $uri): void { $this->config->expects($this->once()) ->method('getSystemValueBool') @@ -167,9 +164,9 @@ class ClientTest extends \Test\TestCase { } /** - * @dataProvider dataPreventLocalAddress * @param string $uri */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataPreventLocalAddress')] public function testPreventLocalAddressDisabledByOption(string $uri): void { $this->config->expects($this->never()) ->method('getSystemValueBool'); @@ -180,9 +177,9 @@ class ClientTest extends \Test\TestCase { } /** - * @dataProvider dataPreventLocalAddress * @param string $uri */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataPreventLocalAddress')] public function testPreventLocalAddressOnGet(string $uri): void { $host = parse_url($uri, PHP_URL_HOST); $this->expectException(LocalServerException::class); @@ -195,9 +192,9 @@ class ClientTest extends \Test\TestCase { } /** - * @dataProvider dataPreventLocalAddress * @param string $uri */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataPreventLocalAddress')] public function testPreventLocalAddressOnHead(string $uri): void { $host = parse_url($uri, PHP_URL_HOST); $this->expectException(LocalServerException::class); @@ -210,9 +207,9 @@ class ClientTest extends \Test\TestCase { } /** - * @dataProvider dataPreventLocalAddress * @param string $uri */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataPreventLocalAddress')] public function testPreventLocalAddressOnPost(string $uri): void { $host = parse_url($uri, PHP_URL_HOST); $this->expectException(LocalServerException::class); @@ -225,9 +222,9 @@ class ClientTest extends \Test\TestCase { } /** - * @dataProvider dataPreventLocalAddress * @param string $uri */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataPreventLocalAddress')] public function testPreventLocalAddressOnPut(string $uri): void { $host = parse_url($uri, PHP_URL_HOST); $this->expectException(LocalServerException::class); @@ -240,9 +237,9 @@ class ClientTest extends \Test\TestCase { } /** - * @dataProvider dataPreventLocalAddress * @param string $uri */ + #[\PHPUnit\Framework\Attributes\DataProvider('dataPreventLocalAddress')] public function testPreventLocalAddressOnDelete(string $uri): void { $host = parse_url($uri, PHP_URL_HOST); $this->expectException(LocalServerException::class); @@ -257,21 +254,21 @@ class ClientTest extends \Test\TestCase { private function setUpDefaultRequestOptions(): void { $this->config ->method('getSystemValue') - ->will($this->returnValueMap([ + ->willReturnMap([ ['proxyexclude', [], []], - ])); + ]); $this->config ->method('getSystemValueString') - ->will($this->returnValueMap([ + ->willReturnMap([ ['proxy', '', 'foo'], ['proxyuserpwd', '', ''], - ])); + ]); $this->config ->method('getSystemValueBool') - ->will($this->returnValueMap([ + ->willReturnMap([ ['installed', false, true], ['allow_local_remote_servers', false, true], - ])); + ]); $this->certificateManager ->expects($this->once()) @@ -456,14 +453,10 @@ class ClientTest extends \Test\TestCase { $this->config ->expects($this->exactly(2)) ->method('getSystemValueBool') - ->withConsecutive( - ['installed', false], - ['allow_local_remote_servers', false], - ) - ->willReturnOnConsecutiveCalls( - false, - false, - ); + ->willReturnMap([ + ['installed', false, false], + ['allow_local_remote_servers', false, false], + ]); $this->config ->expects($this->once()) ->method('getSystemValueString') @@ -487,8 +480,8 @@ class ClientTest extends \Test\TestCase { 'on_redirect' => function ( \Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response, - \Psr\Http\Message\UriInterface $uri - ) { + \Psr\Http\Message\UriInterface $uri, + ): void { }, ], ], self::invokePrivate($this->client, 'buildRequestOptions', [[]])); @@ -498,14 +491,10 @@ class ClientTest extends \Test\TestCase { $this->config ->expects($this->exactly(2)) ->method('getSystemValueBool') - ->withConsecutive( - ['installed', false], - ['allow_local_remote_servers', false], - ) - ->willReturnOnConsecutiveCalls( - true, - false, - ); + ->willReturnMap([ + ['installed', false, true], + ['allow_local_remote_servers', false, false], + ]); $this->config ->expects($this->once()) ->method('getSystemValue') @@ -514,14 +503,10 @@ class ClientTest extends \Test\TestCase { $this->config ->expects($this->exactly(2)) ->method('getSystemValueString') - ->withConsecutive( - ['proxy', ''], - ['proxyuserpwd', ''], - ) - ->willReturnOnConsecutiveCalls( - 'foo', - '', - ); + ->willReturnMap([ + ['proxy', '', 'foo'], + ['proxyuserpwd', '', ''], + ]); $this->certificateManager ->expects($this->once()) ->method('getAbsoluteBundlePath') @@ -546,8 +531,8 @@ class ClientTest extends \Test\TestCase { 'on_redirect' => function ( \Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response, - \Psr\Http\Message\UriInterface $uri - ) { + \Psr\Http\Message\UriInterface $uri, + ): void { }, ], ], self::invokePrivate($this->client, 'buildRequestOptions', [[]])); @@ -557,14 +542,10 @@ class ClientTest extends \Test\TestCase { $this->config ->expects($this->exactly(2)) ->method('getSystemValueBool') - ->withConsecutive( - ['installed', false], - ['allow_local_remote_servers', false], - ) - ->willReturnOnConsecutiveCalls( - true, - false, - ); + ->willReturnMap([ + ['installed', false, true], + ['allow_local_remote_servers', false, false], + ]); $this->config ->expects($this->once()) ->method('getSystemValue') @@ -573,14 +554,10 @@ class ClientTest extends \Test\TestCase { $this->config ->expects($this->exactly(2)) ->method('getSystemValueString') - ->withConsecutive( - ['proxy', ''], - ['proxyuserpwd', ''], - ) - ->willReturnOnConsecutiveCalls( - 'foo', - '', - ); + ->willReturnMap([ + ['proxy', '', 'foo'], + ['proxyuserpwd', '', ''], + ]); $this->certificateManager ->expects($this->once()) ->method('getAbsoluteBundlePath') @@ -606,8 +583,8 @@ class ClientTest extends \Test\TestCase { 'on_redirect' => function ( \Psr\Http\Message\RequestInterface $request, \Psr\Http\Message\ResponseInterface $response, - \Psr\Http\Message\UriInterface $uri - ) { + \Psr\Http\Message\UriInterface $uri, + ): void { }, ], ], self::invokePrivate($this->client, 'buildRequestOptions', [[]])); diff --git a/tests/lib/Http/Client/DnsPinMiddlewareTest.php b/tests/lib/Http/Client/DnsPinMiddlewareTest.php index 2fa868e6963..9c0aa198cd8 100644 --- a/tests/lib/Http/Client/DnsPinMiddlewareTest.php +++ b/tests/lib/Http/Client/DnsPinMiddlewareTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2023 Daniel Kesselberg <mail@danielkesselberg.de> - * - * @author Daniel Kesselberg <mail@danielkesselberg.de> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace lib\Http\Client; @@ -59,7 +42,7 @@ class DnsPinMiddlewareTest extends TestCase { ->getMock(); } - public function testPopulateDnsCacheIPv4() { + public function testPopulateDnsCacheIPv4(): void { $mockHandler = new MockHandler([ static function (RequestInterface $request, array $options) { self::arrayHasKey('curl', $options); @@ -152,7 +135,7 @@ class DnsPinMiddlewareTest extends TestCase { ); } - public function testPopulateDnsCacheIPv6() { + public function testPopulateDnsCacheIPv6(): void { $mockHandler = new MockHandler([ static function (RequestInterface $request, array $options) { self::arrayHasKey('curl', $options); @@ -267,7 +250,7 @@ class DnsPinMiddlewareTest extends TestCase { ); } - public function testAllowLocalAddress() { + public function testAllowLocalAddress(): void { $mockHandler = new MockHandler([ static function (RequestInterface $request, array $options) { self::assertArrayNotHasKey('curl', $options); @@ -285,12 +268,12 @@ class DnsPinMiddlewareTest extends TestCase { ); } - public function testRejectIPv4() { + public function testRejectIPv4(): void { $this->expectException(LocalServerException::class); - $this->expectExceptionMessage('Host violates local access rules'); + $this->expectExceptionMessage('violates local access rules'); $mockHandler = new MockHandler([ - static function (RequestInterface $request, array $options) { + static function (RequestInterface $request, array $options): void { // The handler should not be called }, ]); @@ -332,12 +315,12 @@ class DnsPinMiddlewareTest extends TestCase { ); } - public function testRejectIPv6() { + public function testRejectIPv6(): void { $this->expectException(LocalServerException::class); - $this->expectExceptionMessage('Host violates local access rules'); + $this->expectExceptionMessage('violates local access rules'); $mockHandler = new MockHandler([ - static function (RequestInterface $request, array $options) { + static function (RequestInterface $request, array $options): void { // The handler should not be called }, ]); @@ -379,12 +362,12 @@ class DnsPinMiddlewareTest extends TestCase { ); } - public function testRejectCanonicalName() { + public function testRejectCanonicalName(): void { $this->expectException(LocalServerException::class); - $this->expectExceptionMessage('Host violates local access rules'); + $this->expectExceptionMessage('violates local access rules'); $mockHandler = new MockHandler([ - static function (RequestInterface $request, array $options) { + static function (RequestInterface $request, array $options): void { // The handler should not be called }, ]); @@ -469,12 +452,12 @@ class DnsPinMiddlewareTest extends TestCase { ); } - public function testRejectFaultyResponse() { + public function testRejectFaultyResponse(): void { $this->expectException(LocalServerException::class); $this->expectExceptionMessage('No DNS record found for www.example.com'); $mockHandler = new MockHandler([ - static function (RequestInterface $request, array $options) { + static function (RequestInterface $request, array $options): void { // The handler should not be called }, ]); @@ -495,9 +478,9 @@ class DnsPinMiddlewareTest extends TestCase { ); } - public function testIgnoreSubdomainForSoaQuery() { + public function testIgnoreSubdomainForSoaQuery(): void { $mockHandler = new MockHandler([ - static function (RequestInterface $request, array $options) { + static function (RequestInterface $request, array $options): void { // The handler should not be called }, ]); @@ -554,10 +537,11 @@ class DnsPinMiddlewareTest extends TestCase { ['nextcloud' => ['allow_local_address' => false]] ); - $this->assertCount(4, $dnsQueries); + $this->assertCount(3, $dnsQueries); $this->assertContains('example.com' . DNS_SOA, $dnsQueries); $this->assertContains('subsubdomain.subdomain.example.com' . DNS_A, $dnsQueries); $this->assertContains('subsubdomain.subdomain.example.com' . DNS_AAAA, $dnsQueries); - $this->assertContains('subsubdomain.subdomain.example.com' . DNS_CNAME, $dnsQueries); + // CNAME should not be queried if A or AAAA succeeded already + $this->assertNotContains('subsubdomain.subdomain.example.com' . DNS_CNAME, $dnsQueries); } } diff --git a/tests/lib/Http/Client/NegativeDnsCacheTest.php b/tests/lib/Http/Client/NegativeDnsCacheTest.php index b36524acf90..eb0f86f5c7a 100644 --- a/tests/lib/Http/Client/NegativeDnsCacheTest.php +++ b/tests/lib/Http/Client/NegativeDnsCacheTest.php @@ -3,25 +3,8 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2021, Lukas Reschke <lukas@statuscode.ch> - * - * @author Lukas Reschke <lukas@statuscode.ch> - * - * @license GNU AGPL version 3 or any later version - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, either version 3 of the - * License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - * + * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Http\Client; @@ -47,7 +30,7 @@ class NegativeDnsCacheTest extends \Test\TestCase { ->method('createLocal') ->with('NegativeDnsCache') ->willReturn($this->cache); - + $this->negativeDnsCache = new NegativeDnsCache($this->cacheFactory); } @@ -57,16 +40,16 @@ class NegativeDnsCacheTest extends \Test\TestCase { ->method('set') ->with('www.example.com-1', 'true', 3600); - $this->negativeDnsCache->setNegativeCacheForDnsType("www.example.com", DNS_A, 3600); + $this->negativeDnsCache->setNegativeCacheForDnsType('www.example.com', DNS_A, 3600); } - public function testIsNegativeCached() { + public function testIsNegativeCached(): void { $this->cache ->expects($this->once()) ->method('hasKey') ->with('www.example.com-1') ->willReturn(true); - $this->assertTrue($this->negativeDnsCache->isNegativeCached("www.example.com", DNS_A)); + $this->assertTrue($this->negativeDnsCache->isNegativeCached('www.example.com', DNS_A)); } } diff --git a/tests/lib/Http/Client/ResponseTest.php b/tests/lib/Http/Client/ResponseTest.php index 1384e4e732c..1acf1eb1cbd 100644 --- a/tests/lib/Http/Client/ResponseTest.php +++ b/tests/lib/Http/Client/ResponseTest.php @@ -1,9 +1,9 @@ <?php + /** - * Copyright (c) 2015 Lukas Reschke <lukas@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Http\Client; @@ -24,22 +24,22 @@ class ResponseTest extends \Test\TestCase { $this->guzzleResponse = new GuzzleResponse(418); } - public function testGetBody() { + public function testGetBody(): void { $response = new Response($this->guzzleResponse->withBody(Utils::streamFor('MyResponse'))); $this->assertSame('MyResponse', $response->getBody()); } - public function testGetStatusCode() { + public function testGetStatusCode(): void { $response = new Response($this->guzzleResponse); $this->assertSame(418, $response->getStatusCode()); } - public function testGetHeader() { + public function testGetHeader(): void { $response = new Response($this->guzzleResponse->withHeader('bar', 'foo')); $this->assertSame('foo', $response->getHeader('bar')); } - public function testGetHeaders() { + public function testGetHeaders(): void { $response = new Response($this->guzzleResponse ->withHeader('bar', 'foo') ->withHeader('x-awesome', 'yes') |