123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- <?php
- declare(strict_types=1);
- namespace Test\Http\Client;
- use GuzzleHttp\Client as GuzzleClient;
- use GuzzleHttp\Handler\CurlHandler;
- use GuzzleHttp\HandlerStack;
- use GuzzleHttp\Middleware;
- use OC\Http\Client\Client;
- use OC\Http\Client\ClientService;
- use OC\Http\Client\DnsPinMiddleware;
- use OCP\Diagnostics\IEventLogger;
- use OCP\ICertificateManager;
- use OCP\IConfig;
- use OCP\Security\IRemoteHostValidator;
- use Psr\Http\Message\RequestInterface;
- use Psr\Log\LoggerInterface;
- class ClientServiceTest extends \Test\TestCase {
- public function testNewClient(): void {
-
- $config = $this->createMock(IConfig::class);
- $config->method('getSystemValueBool')
- ->with('dns_pinning', true)
- ->willReturn(true);
-
- $certificateManager = $this->createMock(ICertificateManager::class);
- $dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
- $dnsPinMiddleware
- ->expects($this->atLeastOnce())
- ->method('addDnsPinning')
- ->willReturn(function () {
- });
- $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
- $eventLogger = $this->createMock(IEventLogger::class);
- $logger = $this->createMock(LoggerInterface::class);
- $clientService = new ClientService(
- $config,
- $certificateManager,
- $dnsPinMiddleware,
- $remoteHostValidator,
- $eventLogger,
- $logger,
- );
- $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) {
- $eventLogger->end('http:request');
- }), 'event logger');
- $guzzleClient = new GuzzleClient(['handler' => $stack]);
- $this->assertEquals(
- new Client(
- $config,
- $certificateManager,
- $guzzleClient,
- $remoteHostValidator,
- $logger,
- ),
- $clientService->newClient()
- );
- }
- public function testDisableDnsPinning(): void {
-
- $config = $this->createMock(IConfig::class);
- $config->method('getSystemValueBool')
- ->with('dns_pinning', true)
- ->willReturn(false);
-
- $certificateManager = $this->createMock(ICertificateManager::class);
- $dnsPinMiddleware = $this->createMock(DnsPinMiddleware::class);
- $dnsPinMiddleware
- ->expects($this->never())
- ->method('addDnsPinning')
- ->willReturn(function () {
- });
- $remoteHostValidator = $this->createMock(IRemoteHostValidator::class);
- $eventLogger = $this->createMock(IEventLogger::class);
- $logger = $this->createMock(LoggerInterface::class);
- $clientService = new ClientService(
- $config,
- $certificateManager,
- $dnsPinMiddleware,
- $remoteHostValidator,
- $eventLogger,
- $logger,
- );
- $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) {
- $eventLogger->end('http:request');
- }), 'event logger');
- $guzzleClient = new GuzzleClient(['handler' => $stack]);
- $this->assertEquals(
- new Client(
- $config,
- $certificateManager,
- $guzzleClient,
- $remoteHostValidator,
- $logger,
- ),
- $clientService->newClient()
- );
- }
- }
|