2015-03-16 13:28:23 +03:00
|
|
|
<?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.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 09:46:58 +03:00
|
|
|
namespace Test\Http\Client;
|
2015-03-16 13:28:23 +03:00
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
use GuzzleHttp\Psr7\Response;
|
2016-05-19 09:46:58 +03:00
|
|
|
use OC\Http\Client\Client;
|
2017-01-02 16:51:16 +03:00
|
|
|
use OC\Security\CertificateManager;
|
2020-03-24 16:19:57 +03:00
|
|
|
use OCP\Http\Client\LocalServerException;
|
2016-09-07 21:05:51 +03:00
|
|
|
use OCP\ICertificateManager;
|
2015-03-16 13:28:23 +03:00
|
|
|
use OCP\IConfig;
|
2020-03-24 16:19:57 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2015-03-16 13:28:23 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ClientTest
|
|
|
|
*/
|
|
|
|
class ClientTest extends \Test\TestCase {
|
2020-03-24 16:19:57 +03:00
|
|
|
/** @var \GuzzleHttp\Client|MockObject */
|
2015-03-16 13:28:23 +03:00
|
|
|
private $guzzleClient;
|
2020-03-24 16:19:57 +03:00
|
|
|
/** @var CertificateManager|MockObject */
|
2017-01-02 16:51:16 +03:00
|
|
|
private $certificateManager;
|
2015-03-16 13:28:23 +03:00
|
|
|
/** @var Client */
|
|
|
|
private $client;
|
2020-03-24 16:19:57 +03:00
|
|
|
/** @var IConfig|MockObject */
|
2015-03-16 13:28:23 +03:00
|
|
|
private $config;
|
2020-03-24 16:19:57 +03:00
|
|
|
/** @var ILogger|MockObject */
|
|
|
|
private $logger;
|
2018-06-14 22:12:29 +03:00
|
|
|
/** @var array */
|
|
|
|
private $defaultRequestOptions;
|
2015-03-16 13:28:23 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-03-16 13:28:23 +03:00
|
|
|
parent::setUp();
|
2016-09-07 21:05:51 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2020-03-24 16:19:57 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->guzzleClient = $this->createMock(\GuzzleHttp\Client::class);
|
2017-01-02 16:51:16 +03:00
|
|
|
$this->certificateManager = $this->createMock(ICertificateManager::class);
|
2015-03-16 13:28:23 +03:00
|
|
|
$this->client = new Client(
|
|
|
|
$this->config,
|
2020-03-24 16:19:57 +03:00
|
|
|
$this->logger,
|
2017-01-02 16:51:16 +03:00
|
|
|
$this->certificateManager,
|
2019-02-24 16:48:05 +03:00
|
|
|
$this->guzzleClient
|
2015-03-16 13:28:23 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testGetProxyUri(): void {
|
2015-03-16 13:28:23 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxy', null)
|
|
|
|
->willReturn(null);
|
2019-02-24 16:48:05 +03:00
|
|
|
$this->assertNull(self::invokePrivate($this->client, 'getProxyUri'));
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testGetProxyUriProxyHostEmptyPassword(): void {
|
2015-03-16 13:28:23 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxy', null)
|
|
|
|
->willReturn('foo');
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyuserpwd', null)
|
|
|
|
->willReturn(null);
|
2019-10-25 22:21:32 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyexclude', [])
|
|
|
|
->willReturn([]);
|
|
|
|
$this->assertEquals([
|
|
|
|
'http' => 'foo',
|
|
|
|
'https' => 'foo'
|
|
|
|
], self::invokePrivate($this->client, 'getProxyUri'));
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testGetProxyUriProxyHostWithPassword(): void {
|
2015-03-16 13:28:23 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
2019-08-03 01:18:01 +03:00
|
|
|
->with(
|
|
|
|
$this->equalTo('proxy'),
|
|
|
|
$this->callback(function ($input) {
|
|
|
|
return $input === '';
|
|
|
|
})
|
|
|
|
)
|
2015-03-16 13:28:23 +03:00
|
|
|
->willReturn('foo');
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
2019-08-03 01:18:01 +03:00
|
|
|
->with(
|
|
|
|
$this->equalTo('proxyuserpwd'),
|
|
|
|
$this->callback(function ($input) {
|
|
|
|
return $input === '';
|
|
|
|
})
|
|
|
|
)
|
2015-03-16 13:28:23 +03:00
|
|
|
->willReturn('username:password');
|
2019-10-25 22:21:32 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('proxyexclude'),
|
|
|
|
$this->callback(function ($input) {
|
|
|
|
return $input === [];
|
|
|
|
})
|
|
|
|
)
|
|
|
|
->willReturn([]);
|
|
|
|
$this->assertEquals([
|
|
|
|
'http' => 'username:password@foo',
|
|
|
|
'https' => 'username:password@foo'
|
|
|
|
], self::invokePrivate($this->client, 'getProxyUri'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetProxyUriProxyHostWithPasswordAndExclude(): void {
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('proxy'),
|
|
|
|
$this->callback(function ($input) {
|
|
|
|
return $input === '';
|
|
|
|
})
|
|
|
|
)
|
|
|
|
->willReturn('foo');
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('proxyuserpwd'),
|
|
|
|
$this->callback(function ($input) {
|
|
|
|
return $input === '';
|
|
|
|
})
|
|
|
|
)
|
|
|
|
->willReturn('username:password');
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with(
|
|
|
|
$this->equalTo('proxyexclude'),
|
|
|
|
$this->callback(function ($input) {
|
|
|
|
return $input === [];
|
|
|
|
})
|
|
|
|
)
|
|
|
|
->willReturn(['bar']);
|
|
|
|
$this->assertEquals([
|
|
|
|
'http' => 'username:password@foo',
|
|
|
|
'https' => 'username:password@foo',
|
|
|
|
'no' => ['bar']
|
|
|
|
], self::invokePrivate($this->client, 'getProxyUri'));
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
|
|
|
|
2020-03-24 16:19:57 +03:00
|
|
|
public function dataPreventLocalAddress():array {
|
|
|
|
return [
|
|
|
|
['localhost/foo.bar'],
|
|
|
|
['localHost/foo.bar'],
|
|
|
|
['random-host/foo.bar'],
|
|
|
|
['[::1]/bla.blub'],
|
|
|
|
['[::]/bla.blub'],
|
|
|
|
['192.168.0.1'],
|
|
|
|
['172.16.42.1'],
|
|
|
|
['[fdf8:f53b:82e4::53]/secret.ics'],
|
|
|
|
['[fe80::200:5aee:feaa:20a2]/secret.ics'],
|
|
|
|
['[0:0:0:0:0:0:10.0.0.1]/secret.ics'],
|
|
|
|
['[0:0:0:0:0:ffff:127.0.0.0]/secret.ics'],
|
|
|
|
['10.0.0.1'],
|
|
|
|
['another-host.local'],
|
|
|
|
['service.localhost'],
|
|
|
|
['!@#$'], // test invalid url
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddress(string $uri): void {
|
|
|
|
$this->expectException(LocalServerException::class);
|
|
|
|
self::invokePrivate($this->client, 'preventLocalAddress', ['http://' . $uri, []]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddressDisabledByGlobalConfig(string $uri): void {
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValueBool')
|
|
|
|
->with('allow_local_remote_servers', false)
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
// $this->expectException(LocalServerException::class);
|
|
|
|
|
|
|
|
self::invokePrivate($this->client, 'preventLocalAddress', ['http://' . $uri, []]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddressDisabledByOption(string $uri): void {
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('getSystemValueBool');
|
|
|
|
|
|
|
|
// $this->expectException(LocalServerException::class);
|
|
|
|
|
|
|
|
self::invokePrivate($this->client, 'preventLocalAddress', ['http://' . $uri, [
|
|
|
|
'nextcloud' => ['allow_local_address' => true],
|
|
|
|
]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddressOnGet(string $uri): void {
|
|
|
|
$this->expectException(LocalServerException::class);
|
|
|
|
$this->client->get('http://' . $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddressOnHead(string $uri): void {
|
|
|
|
$this->expectException(LocalServerException::class);
|
|
|
|
$this->client->head('http://' . $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddressOnPost(string $uri): void {
|
|
|
|
$this->expectException(LocalServerException::class);
|
|
|
|
$this->client->post('http://' . $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddressOnPut(string $uri): void {
|
|
|
|
$this->expectException(LocalServerException::class);
|
|
|
|
$this->client->put('http://' . $uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataPreventLocalAddress
|
|
|
|
* @param string $uri
|
|
|
|
*/
|
|
|
|
public function testPreventLocalAddressOnDelete(string $uri): void {
|
|
|
|
$this->expectException(LocalServerException::class);
|
|
|
|
$this->client->delete('http://' . $uri);
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
private function setUpDefaultRequestOptions(): void {
|
2020-03-24 16:19:57 +03:00
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('getSystemValueBool')
|
|
|
|
->with('allow_local_remote_servers', false)
|
|
|
|
->willReturn(true);
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->config
|
2020-03-24 16:19:57 +03:00
|
|
|
->expects($this->at(1))
|
2018-06-14 22:12:29 +03:00
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxy', null)
|
|
|
|
->willReturn('foo');
|
|
|
|
$this->config
|
2020-03-24 16:19:57 +03:00
|
|
|
->expects($this->at(2))
|
2018-06-14 22:12:29 +03:00
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyuserpwd', null)
|
|
|
|
->willReturn(null);
|
2019-10-25 22:21:32 +03:00
|
|
|
$this->config
|
2020-03-24 16:19:57 +03:00
|
|
|
->expects($this->at(3))
|
2019-10-25 22:21:32 +03:00
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyexclude', [])
|
|
|
|
->willReturn([]);
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->certificateManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getAbsoluteBundlePath')
|
|
|
|
->with(null)
|
|
|
|
->willReturn('/my/path.crt');
|
|
|
|
|
|
|
|
$this->defaultRequestOptions = [
|
|
|
|
'verify' => '/my/path.crt',
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'foo',
|
|
|
|
'https' => 'foo'
|
|
|
|
],
|
2019-02-24 16:48:05 +03:00
|
|
|
'headers' => [
|
2019-09-03 10:14:08 +03:00
|
|
|
'User-Agent' => 'Nextcloud Server Crawler',
|
2020-05-20 12:08:27 +03:00
|
|
|
'Accept-Encoding' => 'gzip',
|
2019-09-03 10:14:08 +03:00
|
|
|
],
|
|
|
|
'timeout' => 30,
|
2018-06-14 22:12:29 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testGet(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
$this->guzzleClient->method('request')
|
2018-06-14 22:12:29 +03:00
|
|
|
->with('get', 'http://localhost/', $this->defaultRequestOptions)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->get('http://localhost/', [])->getStatusCode());
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testGetWithOptions(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2019-09-03 10:14:08 +03:00
|
|
|
$options = array_merge($this->defaultRequestOptions, [
|
2018-06-14 22:12:29 +03:00
|
|
|
'verify' => false,
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'bar',
|
|
|
|
'https' => 'bar'
|
|
|
|
],
|
2019-09-03 10:14:08 +03:00
|
|
|
]);
|
2018-06-14 22:12:29 +03:00
|
|
|
|
|
|
|
$this->guzzleClient->method('request')
|
|
|
|
->with('get', 'http://localhost/', $options)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->get('http://localhost/', $options)->getStatusCode());
|
2018-06-14 22:12:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testPost(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
$this->guzzleClient->method('request')
|
2018-06-14 22:12:29 +03:00
|
|
|
->with('post', 'http://localhost/', $this->defaultRequestOptions)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->post('http://localhost/', [])->getStatusCode());
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testPostWithOptions(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2019-09-03 10:14:08 +03:00
|
|
|
$options = array_merge($this->defaultRequestOptions, [
|
2018-06-14 22:12:29 +03:00
|
|
|
'verify' => false,
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'bar',
|
|
|
|
'https' => 'bar'
|
|
|
|
],
|
2019-09-03 10:14:08 +03:00
|
|
|
]);
|
2018-06-14 22:12:29 +03:00
|
|
|
|
|
|
|
$this->guzzleClient->method('request')
|
|
|
|
->with('post', 'http://localhost/', $options)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->post('http://localhost/', $options)->getStatusCode());
|
2018-06-14 22:12:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testPut(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
$this->guzzleClient->method('request')
|
2018-06-14 22:12:29 +03:00
|
|
|
->with('put', 'http://localhost/', $this->defaultRequestOptions)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->put('http://localhost/', [])->getStatusCode());
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testPutWithOptions(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2019-09-03 10:14:08 +03:00
|
|
|
$options = array_merge($this->defaultRequestOptions, [
|
2018-06-14 22:12:29 +03:00
|
|
|
'verify' => false,
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'bar',
|
|
|
|
'https' => 'bar'
|
|
|
|
],
|
2019-09-03 10:14:08 +03:00
|
|
|
]);
|
2018-06-14 22:12:29 +03:00
|
|
|
|
|
|
|
$this->guzzleClient->method('request')
|
|
|
|
->with('put', 'http://localhost/', $options)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->put('http://localhost/', $options)->getStatusCode());
|
2018-06-14 22:12:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testDelete(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
$this->guzzleClient->method('request')
|
2018-06-14 22:12:29 +03:00
|
|
|
->with('delete', 'http://localhost/', $this->defaultRequestOptions)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->delete('http://localhost/', [])->getStatusCode());
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testDeleteWithOptions(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2019-09-03 10:14:08 +03:00
|
|
|
$options = array_merge($this->defaultRequestOptions, [
|
2018-06-14 22:12:29 +03:00
|
|
|
'verify' => false,
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'bar',
|
|
|
|
'https' => 'bar'
|
|
|
|
],
|
2019-09-03 10:14:08 +03:00
|
|
|
]);
|
2018-06-14 22:12:29 +03:00
|
|
|
|
|
|
|
$this->guzzleClient->method('request')
|
|
|
|
->with('delete', 'http://localhost/', $options)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->delete('http://localhost/', $options)->getStatusCode());
|
2018-06-14 22:12:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testOptions(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
$this->guzzleClient->method('request')
|
2018-06-14 22:12:29 +03:00
|
|
|
->with('options', 'http://localhost/', $this->defaultRequestOptions)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->options('http://localhost/', [])->getStatusCode());
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|
2017-01-02 16:51:16 +03:00
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testOptionsWithOptions(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2019-09-03 10:14:08 +03:00
|
|
|
$options = array_merge($this->defaultRequestOptions, [
|
2018-06-14 22:12:29 +03:00
|
|
|
'verify' => false,
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'bar',
|
|
|
|
'https' => 'bar'
|
|
|
|
],
|
2019-09-03 10:14:08 +03:00
|
|
|
]);
|
2018-06-14 22:12:29 +03:00
|
|
|
|
|
|
|
$this->guzzleClient->method('request')
|
|
|
|
->with('options', 'http://localhost/', $options)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->options('http://localhost/', $options)->getStatusCode());
|
2018-06-14 22:12:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testHead(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
$this->guzzleClient->method('request')
|
2018-06-14 22:12:29 +03:00
|
|
|
->with('head', 'http://localhost/', $this->defaultRequestOptions)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->head('http://localhost/', [])->getStatusCode());
|
2017-01-02 16:51:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testHeadWithOptions(): void {
|
2018-06-14 22:12:29 +03:00
|
|
|
$this->setUpDefaultRequestOptions();
|
|
|
|
|
2019-09-03 10:14:08 +03:00
|
|
|
$options = array_merge($this->defaultRequestOptions, [
|
2018-06-14 22:12:29 +03:00
|
|
|
'verify' => false,
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'bar',
|
|
|
|
'https' => 'bar'
|
|
|
|
],
|
2019-09-03 10:14:08 +03:00
|
|
|
]);
|
2018-06-14 22:12:29 +03:00
|
|
|
|
|
|
|
$this->guzzleClient->method('request')
|
|
|
|
->with('head', 'http://localhost/', $options)
|
2020-02-06 20:13:04 +03:00
|
|
|
->willReturn(new Response(418));
|
|
|
|
$this->assertEquals(418, $this->client->head('http://localhost/', $options)->getStatusCode());
|
2018-06-14 22:12:29 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testSetDefaultOptionsWithNotInstalled(): void {
|
2017-01-02 16:51:16 +03:00
|
|
|
$this->config
|
2019-04-16 22:27:03 +03:00
|
|
|
->expects($this->at(1))
|
2017-01-02 16:51:16 +03:00
|
|
|
->method('getSystemValue')
|
|
|
|
->with('installed', false)
|
|
|
|
->willReturn(false);
|
|
|
|
$this->certificateManager
|
2020-12-04 15:37:10 +03:00
|
|
|
->expects($this->once())
|
|
|
|
->method('listCertificates')
|
|
|
|
->willReturn([]);
|
2017-01-02 16:51:16 +03:00
|
|
|
|
2018-02-08 15:39:27 +03:00
|
|
|
$this->assertEquals([
|
2019-02-24 16:48:05 +03:00
|
|
|
'verify' => \OC::$SERVERROOT . '/resources/config/ca-bundle.crt',
|
|
|
|
'headers' => [
|
2020-05-20 12:08:27 +03:00
|
|
|
'User-Agent' => 'Nextcloud Server Crawler',
|
|
|
|
'Accept-Encoding' => 'gzip',
|
2019-09-03 10:14:08 +03:00
|
|
|
],
|
|
|
|
'timeout' => 30,
|
2019-02-24 16:48:05 +03:00
|
|
|
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
|
2017-01-02 16:51:16 +03:00
|
|
|
}
|
|
|
|
|
2019-02-24 16:48:05 +03:00
|
|
|
public function testSetDefaultOptionsWithProxy(): void {
|
2017-01-02 16:51:16 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxy', null)
|
|
|
|
->willReturn('foo');
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyuserpwd', null)
|
|
|
|
->willReturn(null);
|
2019-10-25 22:21:32 +03:00
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyexclude', [])
|
|
|
|
->willReturn([]);
|
2017-01-02 16:51:16 +03:00
|
|
|
$this->certificateManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getAbsoluteBundlePath')
|
|
|
|
->with(null)
|
|
|
|
->willReturn('/my/path.crt');
|
2018-02-08 15:39:27 +03:00
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'verify' => '/my/path.crt',
|
2019-10-25 22:21:32 +03:00
|
|
|
'proxy' => [
|
|
|
|
'http' => 'foo',
|
|
|
|
'https' => 'foo'
|
|
|
|
],
|
|
|
|
'headers' => [
|
2020-05-20 12:08:27 +03:00
|
|
|
'User-Agent' => 'Nextcloud Server Crawler',
|
|
|
|
'Accept-Encoding' => 'gzip',
|
2019-10-25 22:21:32 +03:00
|
|
|
],
|
|
|
|
'timeout' => 30,
|
|
|
|
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetDefaultOptionsWithProxyAndExclude(): void {
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxy', null)
|
|
|
|
->willReturn('foo');
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyuserpwd', null)
|
|
|
|
->willReturn(null);
|
|
|
|
$this->config
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getSystemValue')
|
|
|
|
->with('proxyexclude', [])
|
|
|
|
->willReturn(['bar']);
|
|
|
|
$this->certificateManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getAbsoluteBundlePath')
|
|
|
|
->with(null)
|
|
|
|
->willReturn('/my/path.crt');
|
|
|
|
|
|
|
|
$this->assertEquals([
|
|
|
|
'verify' => '/my/path.crt',
|
|
|
|
'proxy' => [
|
|
|
|
'http' => 'foo',
|
|
|
|
'https' => 'foo',
|
|
|
|
'no' => ['bar']
|
|
|
|
],
|
2019-02-24 16:48:05 +03:00
|
|
|
'headers' => [
|
2020-05-20 12:08:27 +03:00
|
|
|
'User-Agent' => 'Nextcloud Server Crawler',
|
|
|
|
'Accept-Encoding' => 'gzip',
|
2019-09-03 10:14:08 +03:00
|
|
|
],
|
|
|
|
'timeout' => 30,
|
2019-02-24 16:48:05 +03:00
|
|
|
], self::invokePrivate($this->client, 'buildRequestOptions', [[]]));
|
2017-01-02 16:51:16 +03:00
|
|
|
}
|
2015-03-16 13:28:23 +03:00
|
|
|
}
|