summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/private/Http/Client/Client.php59
-rw-r--r--lib/private/Http/Client/ClientService.php3
-rw-r--r--lib/private/Http/Client/Response.php10
-rw-r--r--tests/Settings/Controller/CheckSetupControllerTest.php5
-rw-r--r--tests/lib/Http/Client/ClientServiceTest.php3
-rw-r--r--tests/lib/Http/Client/ClientTest.php49
-rw-r--r--tests/lib/Http/Client/ResponseTest.php28
7 files changed, 84 insertions, 73 deletions
diff --git a/lib/private/Http/Client/Client.php b/lib/private/Http/Client/Client.php
index 4e6843d7b9f..e853099e67f 100644
--- a/lib/private/Http/Client/Client.php
+++ b/lib/private/Http/Client/Client.php
@@ -25,10 +25,13 @@ declare(strict_types=1);
namespace OC\Http\Client;
use GuzzleHttp\Client as GuzzleClient;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Middleware;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IResponse;
use OCP\ICertificateManager;
use OCP\IConfig;
+use Psr\Http\Message\RequestInterface;
/**
* Class Client
@@ -43,17 +46,23 @@ class Client implements IClient {
/** @var ICertificateManager */
private $certificateManager;
private $configured = false;
+ /** @var HandlerStack */
+ private $stack;
/**
* @param IConfig $config
* @param ICertificateManager $certificateManager
* @param GuzzleClient $client
*/
- public function __construct(IConfig $config,
- ICertificateManager $certificateManager,
- GuzzleClient $client) {
+ public function __construct(
+ IConfig $config,
+ ICertificateManager $certificateManager,
+ GuzzleClient $client,
+ HandlerStack $stack
+ ) {
$this->config = $config;
$this->client = $client;
+ $this->stack = $stack;
$this->certificateManager = $certificateManager;
}
@@ -65,25 +74,37 @@ class Client implements IClient {
return;
}
$this->configured = true;
- // Either use user bundle or the system bundle if nothing is specified
+
+ $this->stack->push(Middleware::mapRequest(function (RequestInterface $request) {
+ return $request
+ ->withHeader('User-Agent', 'Nextcloud Server Crawler');
+ }));
+ }
+
+ private function getRequestOptions() {
+ $options = [
+ 'verify' => $this->getCertBundle(),
+ ];
+ $proxyUri = $this->getProxyUri();
+ if ($proxyUri !== '') {
+ $options['proxy'] = $proxyUri;
+ }
+ return $options;
+ }
+
+ private function getCertBundle() {
if ($this->certificateManager->listCertificates() !== []) {
- $this->client->setDefaultOption('verify', $this->certificateManager->getAbsoluteBundlePath());
+ return $this->certificateManager->getAbsoluteBundlePath();
} else {
// If the instance is not yet setup we need to use the static path as
// $this->certificateManager->getAbsoluteBundlePath() tries to instantiiate
// a view
if ($this->config->getSystemValue('installed', false)) {
- $this->client->setDefaultOption('verify', $this->certificateManager->getAbsoluteBundlePath(null));
+ return $this->certificateManager->getAbsoluteBundlePath(null);
} else {
- $this->client->setDefaultOption('verify', \OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
+ return \OC::$SERVERROOT . '/resources/config/ca-bundle.crt';
}
}
-
- $this->client->setDefaultOption('headers/User-Agent', 'Nextcloud Server Crawler');
- $proxyUri = $this->getProxyUri();
- if ($proxyUri !== '') {
- $this->client->setDefaultOption('proxy', $proxyUri);
- }
}
/**
@@ -137,7 +158,7 @@ class Client implements IClient {
*/
public function get(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->get($uri, $options);
+ $response = $this->client->request('get', $uri, array_merge($options, $this->getRequestOptions()));
$isStream = isset($options['stream']) && $options['stream'];
return new Response($response, $isStream);
}
@@ -168,7 +189,7 @@ class Client implements IClient {
*/
public function head(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->head($uri, $options);
+ $response = $this->client->request('head', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -203,7 +224,7 @@ class Client implements IClient {
*/
public function post(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->post($uri, $options);
+ $response = $this->client->request('post', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -238,7 +259,7 @@ class Client implements IClient {
*/
public function put(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->put($uri, $options);
+ $response = $this->client->request('put', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -273,7 +294,7 @@ class Client implements IClient {
*/
public function delete(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->delete($uri, $options);
+ $response = $this->client->request('delete', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
@@ -309,7 +330,7 @@ class Client implements IClient {
*/
public function options(string $uri, array $options = []): IResponse {
$this->setDefaultOptions();
- $response = $this->client->options($uri, $options);
+ $response = $this->client->request('options', $uri, array_merge($options, $this->getRequestOptions()));
return new Response($response);
}
}
diff --git a/lib/private/Http/Client/ClientService.php b/lib/private/Http/Client/ClientService.php
index 1df54010a2d..fa8544f07a5 100644
--- a/lib/private/Http/Client/ClientService.php
+++ b/lib/private/Http/Client/ClientService.php
@@ -24,6 +24,7 @@ declare(strict_types=1);
namespace OC\Http\Client;
use GuzzleHttp\Client as GuzzleClient;
+use GuzzleHttp\HandlerStack;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\ICertificateManager;
@@ -54,6 +55,6 @@ class ClientService implements IClientService {
* @return Client
*/
public function newClient(): IClient {
- return new Client($this->config, $this->certificateManager, new GuzzleClient());
+ return new Client($this->config, $this->certificateManager, new GuzzleClient(), HandlerStack::create());
}
}
diff --git a/lib/private/Http/Client/Response.php b/lib/private/Http/Client/Response.php
index 0ce6cc98e0d..73c14c2926d 100644
--- a/lib/private/Http/Client/Response.php
+++ b/lib/private/Http/Client/Response.php
@@ -25,7 +25,7 @@ declare(strict_types=1);
namespace OC\Http\Client;
use OCP\Http\Client\IResponse;
-use GuzzleHttp\Message\ResponseInterface as GuzzleResponse;
+use Psr\Http\Message\ResponseInterface;
/**
* Class Response
@@ -33,7 +33,7 @@ use GuzzleHttp\Message\ResponseInterface as GuzzleResponse;
* @package OC\Http
*/
class Response implements IResponse {
- /** @var GuzzleResponse */
+ /** @var ResponseInterface */
private $response;
/**
@@ -42,10 +42,10 @@ class Response implements IResponse {
private $stream;
/**
- * @param GuzzleResponse $response
+ * @param ResponseInterface $response
* @param bool $stream
*/
- public function __construct(GuzzleResponse $response, $stream = false) {
+ public function __construct(ResponseInterface $response, $stream = false) {
$this->response = $response;
$this->stream = $stream;
}
@@ -71,7 +71,7 @@ class Response implements IResponse {
* @return string
*/
public function getHeader(string $key): string {
- return $this->response->getHeader($key);
+ return $this->response->getHeader($key)[0];
}
/**
diff --git a/tests/Settings/Controller/CheckSetupControllerTest.php b/tests/Settings/Controller/CheckSetupControllerTest.php
index 3f47819bcbd..a0ee7f6cae6 100644
--- a/tests/Settings/Controller/CheckSetupControllerTest.php
+++ b/tests/Settings/Controller/CheckSetupControllerTest.php
@@ -21,6 +21,7 @@
namespace Tests\Settings\Controller;
+use Guzzle\Http\Message\Response;
use OC\Settings\Controller\CheckSetupController;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\DataDisplayResponse;
@@ -460,7 +461,7 @@ class CheckSetupControllerTest extends TestCase {
->disableOriginalConstructor()->getMock();
$exception = $this->getMockBuilder('\GuzzleHttp\Exception\ClientException')
->disableOriginalConstructor()->getMock();
- $response = $this->getMockBuilder('\GuzzleHttp\Message\ResponseInterface')
+ $response = $this->getMockBuilder(Response::class)
->disableOriginalConstructor()->getMock();
$response->expects($this->once())
->method('getStatusCode')
@@ -494,7 +495,7 @@ class CheckSetupControllerTest extends TestCase {
->disableOriginalConstructor()->getMock();
$exception = $this->getMockBuilder('\GuzzleHttp\Exception\ClientException')
->disableOriginalConstructor()->getMock();
- $response = $this->getMockBuilder('\GuzzleHttp\Message\ResponseInterface')
+ $response = $this->getMockBuilder(Response::class)
->disableOriginalConstructor()->getMock();
$response->expects($this->once())
->method('getStatusCode')
diff --git a/tests/lib/Http/Client/ClientServiceTest.php b/tests/lib/Http/Client/ClientServiceTest.php
index 48330dc33c0..1bfaf050355 100644
--- a/tests/lib/Http/Client/ClientServiceTest.php
+++ b/tests/lib/Http/Client/ClientServiceTest.php
@@ -9,6 +9,7 @@
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;
@@ -22,7 +23,7 @@ class ClientServiceTest extends \Test\TestCase {
$config = $this->createMock(IConfig::class);
$certificateManager = $this->createMock(ICertificateManager::class);
- $expected = new Client($config, $certificateManager, new GuzzleClient());
+ $expected = new Client($config, $certificateManager, new GuzzleClient(), HandlerStack::create());
$clientService = new ClientService($config, $certificateManager);
$this->assertEquals($expected, $clientService->newClient());
}
diff --git a/tests/lib/Http/Client/ClientTest.php b/tests/lib/Http/Client/ClientTest.php
index 1b0a51b7395..ec4ca6ec90c 100644
--- a/tests/lib/Http/Client/ClientTest.php
+++ b/tests/lib/Http/Client/ClientTest.php
@@ -8,7 +8,8 @@
namespace Test\Http\Client;
-use GuzzleHttp\Message\Response;
+use GuzzleHttp\HandlerStack;
+use GuzzleHttp\Psr7\Response;
use OC\Http\Client\Client;
use OC\Security\CertificateManager;
use OCP\ICertificateManager;
@@ -37,7 +38,8 @@ class ClientTest extends \Test\TestCase {
$this->client = new Client(
$this->config,
$this->certificateManager,
- $this->guzzleClient
+ $this->guzzleClient,
+ HandlerStack::create()
);
}
@@ -84,37 +86,37 @@ class ClientTest extends \Test\TestCase {
}
public function testGet() {
- $this->guzzleClient->method('get')
+ $this->guzzleClient->method('request')
->willReturn(new Response(1337));
$this->assertEquals(1337, $this->client->get('http://localhost/', [])->getStatusCode());
}
public function testPost() {
- $this->guzzleClient->method('post')
+ $this->guzzleClient->method('request')
->willReturn(new Response(1337));
$this->assertEquals(1337, $this->client->post('http://localhost/', [])->getStatusCode());
}
public function testPut() {
- $this->guzzleClient->method('put')
+ $this->guzzleClient->method('request')
->willReturn(new Response(1337));
$this->assertEquals(1337, $this->client->put('http://localhost/', [])->getStatusCode());
}
public function testDelete() {
- $this->guzzleClient->method('delete')
+ $this->guzzleClient->method('request')
->willReturn(new Response(1337));
$this->assertEquals(1337, $this->client->delete('http://localhost/', [])->getStatusCode());
}
public function testOptions() {
- $this->guzzleClient->method('options')
+ $this->guzzleClient->method('request')
->willReturn(new Response(1337));
$this->assertEquals(1337, $this->client->options('http://localhost/', [])->getStatusCode());
}
public function testHead() {
- $this->guzzleClient->method('head')
+ $this->guzzleClient->method('request')
->willReturn(new Response(1337));
$this->assertEquals(1337, $this->client->head('http://localhost/', [])->getStatusCode());
}
@@ -129,16 +131,10 @@ class ClientTest extends \Test\TestCase {
->expects($this->once())
->method('listCertificates')
->willReturn([]);
- $this->guzzleClient
- ->expects($this->at(0))
- ->method('setDefaultOption')
- ->with('verify', \OC::$SERVERROOT . '/resources/config/ca-bundle.crt');
- $this->guzzleClient
- ->expects($this->at(1))
- ->method('setDefaultOption')
- ->with('headers/User-Agent', 'Nextcloud Server Crawler');
- self::invokePrivate($this->client, 'setDefaultOptions');
+ $this->assertEquals([
+ 'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt'
+ ], self::invokePrivate($this->client, 'getRequestOptions'));
}
public function testSetDefaultOptionsWithProxy() {
@@ -157,19 +153,10 @@ class ClientTest extends \Test\TestCase {
->method('getAbsoluteBundlePath')
->with(null)
->willReturn('/my/path.crt');
- $this->guzzleClient
- ->expects($this->at(0))
- ->method('setDefaultOption')
- ->with('verify', '/my/path.crt');
- $this->guzzleClient
- ->expects($this->at(1))
- ->method('setDefaultOption')
- ->with('headers/User-Agent', 'Nextcloud Server Crawler');
- $this->guzzleClient
- ->expects($this->at(2))
- ->method('setDefaultOption')
- ->with('proxy', 'foo');
-
- self::invokePrivate($this->client, 'setDefaultOptions');
+
+ $this->assertEquals([
+ 'verify' => '/my/path.crt',
+ 'proxy' => 'foo'
+ ], self::invokePrivate($this->client, 'getRequestOptions'));
}
}
diff --git a/tests/lib/Http/Client/ResponseTest.php b/tests/lib/Http/Client/ResponseTest.php
index 2e5a47b7f4a..d50f9a717d5 100644
--- a/tests/lib/Http/Client/ResponseTest.php
+++ b/tests/lib/Http/Client/ResponseTest.php
@@ -8,42 +8,42 @@
namespace Test\Http\Client;
-use GuzzleHttp\Stream\Stream;
-use GuzzleHttp\Message\Response as GuzzleResponse;
+use function GuzzleHttp\Psr7\stream_for;
+use GuzzleHttp\Psr7\Response as GuzzleResponse;
use OC\Http\Client\Response;
/**
* Class ResponseTest
*/
class ResponseTest extends \Test\TestCase {
- /** @var Response */
- private $response;
/** @var GuzzleResponse */
private $guzzleResponse;
public function setUp() {
parent::setUp();
$this->guzzleResponse = new GuzzleResponse(1337);
- $this->response = new Response($this->guzzleResponse);
}
public function testGetBody() {
- $this->guzzleResponse->setBody(Stream::factory('MyResponse'));
- $this->assertSame('MyResponse', $this->response->getBody());
+ $response = new Response($this->guzzleResponse->withBody(stream_for('MyResponse')));
+ $this->assertSame('MyResponse', $response->getBody());
}
public function testGetStatusCode() {
- $this->assertSame(1337, $this->response->getStatusCode());
+ $response = new Response($this->guzzleResponse);
+ $this->assertSame(1337, $response->getStatusCode());
}
public function testGetHeader() {
- $this->guzzleResponse->setHeader('bar', 'foo');
- $this->assertSame('foo', $this->response->getHeader('bar'));
+ $response = new Response($this->guzzleResponse->withHeader('bar', 'foo'));
+ $this->assertSame('foo', $response->getHeader('bar'));
}
public function testGetHeaders() {
- $this->guzzleResponse->setHeader('bar', 'foo');
- $this->guzzleResponse->setHeader('x-awesome', 'yes');
+ $response = new Response($this->guzzleResponse
+ ->withHeader('bar', 'foo')
+ ->withHeader('x-awesome', 'yes')
+ );
$expected = [
'bar' => [
@@ -53,7 +53,7 @@ class ResponseTest extends \Test\TestCase {
0 => 'yes',
],
];
- $this->assertSame($expected, $this->response->getHeaders());
- $this->assertSame('yes', $this->response->getHeader('x-awesome'));
+ $this->assertSame($expected, $response->getHeaders());
+ $this->assertSame('yes', $response->getHeader('x-awesome'));
}
}