2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2020-09-24 15:48:22 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of Composer.
|
|
|
|
*
|
|
|
|
* (c) Nils Adermann <naderman@naderman.de>
|
|
|
|
* Jordi Boggiano <j.boggiano@seld.be>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Composer\Test\Util\Http;
|
|
|
|
|
|
|
|
use Composer\Util\Http\ProxyManager;
|
|
|
|
use Composer\Test\TestCase;
|
|
|
|
|
|
|
|
class ProxyManagerTest extends TestCase
|
|
|
|
{
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
|
|
|
unset(
|
|
|
|
$_SERVER['HTTP_PROXY'],
|
|
|
|
$_SERVER['http_proxy'],
|
|
|
|
$_SERVER['HTTPS_PROXY'],
|
|
|
|
$_SERVER['https_proxy'],
|
|
|
|
$_SERVER['NO_PROXY'],
|
|
|
|
$_SERVER['no_proxy'],
|
|
|
|
$_SERVER['CGI_HTTP_PROXY']
|
|
|
|
);
|
|
|
|
ProxyManager::reset();
|
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function tearDown(): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
2021-12-09 16:09:07 +00:00
|
|
|
parent::tearDown();
|
2020-09-24 15:48:22 +00:00
|
|
|
unset(
|
|
|
|
$_SERVER['HTTP_PROXY'],
|
|
|
|
$_SERVER['http_proxy'],
|
|
|
|
$_SERVER['HTTPS_PROXY'],
|
|
|
|
$_SERVER['https_proxy'],
|
|
|
|
$_SERVER['NO_PROXY'],
|
|
|
|
$_SERVER['no_proxy'],
|
|
|
|
$_SERVER['CGI_HTTP_PROXY']
|
|
|
|
);
|
|
|
|
ProxyManager::reset();
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInstantiation(): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
|
|
|
$originalInstance = ProxyManager::getInstance();
|
|
|
|
$this->assertInstanceOf('Composer\Util\Http\ProxyManager', $originalInstance);
|
|
|
|
|
|
|
|
$sameInstance = ProxyManager::getInstance();
|
|
|
|
$this->assertTrue($originalInstance === $sameInstance);
|
|
|
|
|
|
|
|
ProxyManager::reset();
|
|
|
|
$newInstance = ProxyManager::getInstance();
|
|
|
|
$this->assertFalse($sameInstance === $newInstance);
|
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testGetProxyForRequestThrowsOnBadProxyUrl(): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'localhost';
|
|
|
|
$proxyManager = ProxyManager::getInstance();
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('Composer\Downloader\TransportException');
|
2020-09-24 15:48:22 +00:00
|
|
|
$proxyManager->getProxyForRequest('http://example.com');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataRequest
|
2021-10-27 14:18:46 +00:00
|
|
|
*
|
|
|
|
* @param array<string, mixed> $server
|
|
|
|
* @param mixed[] $expectedOptions
|
2022-10-11 14:51:47 +00:00
|
|
|
* @param non-empty-string $url
|
2020-09-24 15:48:22 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testGetProxyForRequest(array $server, string $url, string $expectedUrl, array $expectedOptions, bool $expectedSecure, string $expectedMessage): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
|
|
|
$_SERVER = array_merge($_SERVER, $server);
|
|
|
|
$proxyManager = ProxyManager::getInstance();
|
|
|
|
|
|
|
|
$proxy = $proxyManager->getProxyForRequest($url);
|
|
|
|
$this->assertInstanceOf('Composer\Util\Http\RequestProxy', $proxy);
|
|
|
|
|
|
|
|
$this->assertSame($expectedUrl, $proxy->getUrl());
|
|
|
|
$this->assertSame($expectedOptions, $proxy->getContextOptions());
|
|
|
|
$this->assertSame($expectedSecure, $proxy->isSecure());
|
|
|
|
|
2020-10-24 08:36:39 +00:00
|
|
|
$message = $proxy->getFormattedUrl();
|
2020-09-24 15:48:22 +00:00
|
|
|
|
|
|
|
if ($expectedMessage) {
|
|
|
|
$condition = stripos($message, $expectedMessage) !== false;
|
|
|
|
} else {
|
|
|
|
$condition = $expectedMessage === $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertTrue($condition, 'lastProxy check');
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function dataRequest(): array
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$server = [
|
2020-09-24 15:48:22 +00:00
|
|
|
'http_proxy' => 'http://user:p%40ss@proxy.com',
|
|
|
|
'https_proxy' => 'https://proxy.com:443',
|
|
|
|
'no_proxy' => 'other.repo.org',
|
2022-08-17 12:20:07 +00:00
|
|
|
];
|
2020-09-24 15:48:22 +00:00
|
|
|
|
|
|
|
// server, url, expectedUrl, expectedOptions, expectedSecure, expectedMessage
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[[], 'http://repo.org', '', [], false, ''],
|
|
|
|
[$server, 'http://repo.org', 'http://user:p%40ss@proxy.com:80',
|
|
|
|
['http' => [
|
2020-09-24 15:48:22 +00:00
|
|
|
'proxy' => 'tcp://proxy.com:80',
|
|
|
|
'header' => 'Proxy-Authorization: Basic dXNlcjpwQHNz',
|
|
|
|
'request_fulluri' => true,
|
2022-08-17 12:20:07 +00:00
|
|
|
]],
|
2020-09-24 15:48:22 +00:00
|
|
|
false,
|
|
|
|
'http://user:***@proxy.com:80',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
2020-09-24 15:48:22 +00:00
|
|
|
$server, 'https://repo.org', 'https://proxy.com:443',
|
2022-08-17 12:20:07 +00:00
|
|
|
['http' => [
|
2020-09-24 15:48:22 +00:00
|
|
|
'proxy' => 'ssl://proxy.com:443',
|
2022-08-17 12:20:07 +00:00
|
|
|
]],
|
2020-09-24 15:48:22 +00:00
|
|
|
true,
|
|
|
|
'https://proxy.com:443',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[$server, 'https://other.repo.org', '', [], false, 'no_proxy'],
|
|
|
|
];
|
2020-09-24 15:48:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataStatus
|
2021-10-27 14:18:46 +00:00
|
|
|
*
|
|
|
|
* @param array<string, mixed> $server
|
2020-09-24 15:48:22 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testGetStatus(array $server, bool $expectedStatus, ?string $expectedMessage): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
|
|
|
$_SERVER = array_merge($_SERVER, $server);
|
|
|
|
$proxyManager = ProxyManager::getInstance();
|
2020-10-24 08:36:39 +00:00
|
|
|
$status = $proxyManager->isProxying();
|
|
|
|
$message = $proxyManager->getFormattedProxy();
|
2020-09-24 15:48:22 +00:00
|
|
|
|
|
|
|
$this->assertSame($expectedStatus, $status);
|
|
|
|
|
2022-02-22 15:47:09 +00:00
|
|
|
if ($expectedMessage !== null) {
|
2020-09-24 15:48:22 +00:00
|
|
|
$condition = stripos($message, $expectedMessage) !== false;
|
|
|
|
} else {
|
|
|
|
$condition = $expectedMessage === $message;
|
|
|
|
}
|
|
|
|
$this->assertTrue($condition, 'message check');
|
|
|
|
}
|
|
|
|
|
2022-02-21 12:42:28 +00:00
|
|
|
public function dataStatus(): array
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
|
|
|
// server, expectedStatus, expectedMessage
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[[], false, null],
|
|
|
|
[['http_proxy' => 'localhost'], false, 'malformed'],
|
|
|
|
[
|
|
|
|
['http_proxy' => 'http://user:p%40ss@proxy.com:80'],
|
2020-09-24 15:48:22 +00:00
|
|
|
true,
|
2020-11-22 13:48:56 +00:00
|
|
|
'http=http://user:***@proxy.com:80',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
[
|
|
|
|
['http_proxy' => 'proxy.com:80', 'https_proxy' => 'proxy.com:80'],
|
2020-09-24 15:48:22 +00:00
|
|
|
true,
|
2020-11-22 13:48:56 +00:00
|
|
|
'http=proxy.com:80, https=proxy.com:80',
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
];
|
2020-09-24 15:48:22 +00:00
|
|
|
}
|
|
|
|
}
|