diff options
author | Daniel Kesselberg <mail@danielkesselberg.de> | 2019-02-24 14:48:05 +0100 |
---|---|---|
committer | Daniel Kesselberg <mail@danielkesselberg.de> | 2019-04-16 21:13:29 +0200 |
commit | 2708d264071fc33d81b53a8f33de2074e4210a76 (patch) | |
tree | 79eec4f1150fd28eb8b8993285a03d936d20a633 /tests | |
parent | 5df12cbf47ecdd25a637a06ce4b65c799de8fa72 (diff) | |
download | nextcloud-server-2708d264071fc33d81b53a8f33de2074e4210a76.tar.gz nextcloud-server-2708d264071fc33d81b53a8f33de2074e4210a76.zip |
Set User-Agent as header without middleware
Signed-off-by: Daniel Kesselberg <mail@danielkesselberg.de>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/Http/Client/ClientServiceTest.php | 11 | ||||
-rw-r--r-- | tests/lib/Http/Client/ClientTest.php | 96 |
2 files changed, 68 insertions, 39 deletions
diff --git a/tests/lib/Http/Client/ClientServiceTest.php b/tests/lib/Http/Client/ClientServiceTest.php index 1bfaf050355..02f331483de 100644 --- a/tests/lib/Http/Client/ClientServiceTest.php +++ b/tests/lib/Http/Client/ClientServiceTest.php @@ -9,7 +9,6 @@ namespace Test\Http\Client; use GuzzleHttp\Client as GuzzleClient; -use GuzzleHttp\HandlerStack; use OC\Http\Client\Client; use OC\Http\Client\ClientService; use OCP\ICertificateManager; @@ -19,12 +18,16 @@ use OCP\IConfig; * Class ClientServiceTest */ class ClientServiceTest extends \Test\TestCase { - public function testNewClient() { + public function testNewClient(): void { + /** @var IConfig $config */ $config = $this->createMock(IConfig::class); + /** @var ICertificateManager $certificateManager */ $certificateManager = $this->createMock(ICertificateManager::class); - $expected = new Client($config, $certificateManager, new GuzzleClient(), HandlerStack::create()); $clientService = new ClientService($config, $certificateManager); - $this->assertEquals($expected, $clientService->newClient()); + $this->assertEquals( + new Client($config, $certificateManager, new GuzzleClient()), + $clientService->newClient() + ); } } diff --git a/tests/lib/Http/Client/ClientTest.php b/tests/lib/Http/Client/ClientTest.php index 7f12a824d17..2fd23346398 100644 --- a/tests/lib/Http/Client/ClientTest.php +++ b/tests/lib/Http/Client/ClientTest.php @@ -8,7 +8,6 @@ namespace Test\Http\Client; -use GuzzleHttp\HandlerStack; use GuzzleHttp\Psr7\Response; use OC\Http\Client\Client; use OC\Security\CertificateManager; @@ -33,19 +32,18 @@ class ClientTest extends \Test\TestCase { public function setUp() { parent::setUp(); $this->config = $this->createMock(IConfig::class); - $this->guzzleClient = $this->getMockBuilder('\GuzzleHttp\Client') + $this->guzzleClient = $this->getMockBuilder(\GuzzleHttp\Client::class) ->disableOriginalConstructor() ->getMock(); $this->certificateManager = $this->createMock(ICertificateManager::class); $this->client = new Client( $this->config, $this->certificateManager, - $this->guzzleClient, - HandlerStack::create() + $this->guzzleClient ); } - public function testGetProxyUri() { + public function testGetProxyUri(): void { $this->config ->expects($this->at(0)) ->method('getSystemValue') @@ -56,10 +54,10 @@ class ClientTest extends \Test\TestCase { ->method('getSystemValue') ->with('proxyuserpwd', null) ->willReturn(null); - $this->assertSame('', self::invokePrivate($this->client, 'getProxyUri')); + $this->assertNull(self::invokePrivate($this->client, 'getProxyUri')); } - public function testGetProxyUriProxyHostEmptyPassword() { + public function testGetProxyUriProxyHostEmptyPassword(): void { $this->config ->expects($this->at(0)) ->method('getSystemValue') @@ -73,7 +71,7 @@ class ClientTest extends \Test\TestCase { $this->assertSame('foo', self::invokePrivate($this->client, 'getProxyUri')); } - public function testGetProxyUriProxyHostWithPassword() { + public function testGetProxyUriProxyHostWithPassword(): void { $this->config ->expects($this->at(0)) ->method('getSystemValue') @@ -87,7 +85,7 @@ class ClientTest extends \Test\TestCase { $this->assertSame('username:password@foo', self::invokePrivate($this->client, 'getProxyUri')); } - private function setUpDefaultRequestOptions() { + private function setUpDefaultRequestOptions(): void { $this->config ->expects($this->at(0)) ->method('getSystemValue') @@ -106,11 +104,14 @@ class ClientTest extends \Test\TestCase { $this->defaultRequestOptions = [ 'verify' => '/my/path.crt', - 'proxy' => 'foo' + 'proxy' => 'foo', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] ]; } - public function testGet() { + public function testGet(): void { $this->setUpDefaultRequestOptions(); $this->guzzleClient->method('request') @@ -119,12 +120,15 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->get('http://localhost/', [])->getStatusCode()); } - public function testGetWithOptions() { + public function testGetWithOptions(): void { $this->setUpDefaultRequestOptions(); $options = [ 'verify' => false, - 'proxy' => 'bar' + 'proxy' => 'bar', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] ]; $this->guzzleClient->method('request') @@ -133,7 +137,7 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->get('http://localhost/', $options)->getStatusCode()); } - public function testPost() { + public function testPost(): void { $this->setUpDefaultRequestOptions(); $this->guzzleClient->method('request') @@ -142,12 +146,15 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->post('http://localhost/', [])->getStatusCode()); } - public function testPostWithOptions() { + public function testPostWithOptions(): void { $this->setUpDefaultRequestOptions(); $options = [ 'verify' => false, - 'proxy' => 'bar' + 'proxy' => 'bar', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] ]; $this->guzzleClient->method('request') @@ -156,7 +163,7 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->post('http://localhost/', $options)->getStatusCode()); } - public function testPut() { + public function testPut(): void { $this->setUpDefaultRequestOptions(); $this->guzzleClient->method('request') @@ -165,12 +172,15 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->put('http://localhost/', [])->getStatusCode()); } - public function testPutWithOptions() { + public function testPutWithOptions(): void { $this->setUpDefaultRequestOptions(); $options = [ 'verify' => false, - 'proxy' => 'bar' + 'proxy' => 'bar', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] ]; $this->guzzleClient->method('request') @@ -179,7 +189,7 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->put('http://localhost/', $options)->getStatusCode()); } - public function testDelete() { + public function testDelete(): void { $this->setUpDefaultRequestOptions(); $this->guzzleClient->method('request') @@ -188,12 +198,15 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->delete('http://localhost/', [])->getStatusCode()); } - public function testDeleteWithOptions() { + public function testDeleteWithOptions(): void { $this->setUpDefaultRequestOptions(); $options = [ 'verify' => false, - 'proxy' => 'bar' + 'proxy' => 'bar', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] ]; $this->guzzleClient->method('request') @@ -202,7 +215,7 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->delete('http://localhost/', $options)->getStatusCode()); } - public function testOptions() { + public function testOptions(): void { $this->setUpDefaultRequestOptions(); $this->guzzleClient->method('request') @@ -211,12 +224,15 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->options('http://localhost/', [])->getStatusCode()); } - public function testOptionsWithOptions() { + public function testOptionsWithOptions(): void { $this->setUpDefaultRequestOptions(); $options = [ 'verify' => false, - 'proxy' => 'bar' + 'proxy' => 'bar', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] ]; $this->guzzleClient->method('request') @@ -225,7 +241,7 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->options('http://localhost/', $options)->getStatusCode()); } - public function testHead() { + public function testHead(): void { $this->setUpDefaultRequestOptions(); $this->guzzleClient->method('request') @@ -234,12 +250,15 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->head('http://localhost/', [])->getStatusCode()); } - public function testHeadWithOptions() { + public function testHeadWithOptions(): void { $this->setUpDefaultRequestOptions(); $options = [ 'verify' => false, - 'proxy' => 'bar' + 'proxy' => 'bar', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] ]; $this->guzzleClient->method('request') @@ -248,9 +267,9 @@ class ClientTest extends \Test\TestCase { $this->assertEquals(1337, $this->client->head('http://localhost/', $options)->getStatusCode()); } - public function testSetDefaultOptionsWithNotInstalled() { + public function testSetDefaultOptionsWithNotInstalled(): void { $this->config - ->expects($this->at(0)) + ->expects($this->at(2)) ->method('getSystemValue') ->with('installed', false) ->willReturn(false); @@ -260,11 +279,15 @@ class ClientTest extends \Test\TestCase { ->willReturn([]); $this->assertEquals([ - 'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt' - ], self::invokePrivate($this->client, 'getRequestOptions')); + 'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt', + 'proxy' => null, + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] + ], self::invokePrivate($this->client, 'buildRequestOptions', [[]])); } - public function testSetDefaultOptionsWithProxy() { + public function testSetDefaultOptionsWithProxy(): void { $this->config ->expects($this->at(0)) ->method('getSystemValue') @@ -283,7 +306,10 @@ class ClientTest extends \Test\TestCase { $this->assertEquals([ 'verify' => '/my/path.crt', - 'proxy' => 'foo' - ], self::invokePrivate($this->client, 'getRequestOptions')); + 'proxy' => 'foo', + 'headers' => [ + 'User-Agent' => 'Nextcloud Server Crawler' + ] + ], self::invokePrivate($this->client, 'buildRequestOptions', [[]])); } } |