1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
<?php declare(strict_types=1);
/*
* 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;
use PHPUnit\Framework\Attributes\DataProvider;
/**
* @phpstan-import-type contextOptions from \Composer\Util\Http\RequestProxy
*/
class ProxyManagerTest extends TestCase
{
protected function setUp(): void
{
unset(
$_SERVER['HTTP_PROXY'],
$_SERVER['http_proxy'],
$_SERVER['HTTPS_PROXY'],
$_SERVER['https_proxy'],
$_SERVER['NO_PROXY'],
$_SERVER['no_proxy'],
$_SERVER['CGI_HTTP_PROXY'],
$_SERVER['cgi_http_proxy']
);
ProxyManager::reset();
}
protected function tearDown(): void
{
parent::tearDown();
unset(
$_SERVER['HTTP_PROXY'],
$_SERVER['http_proxy'],
$_SERVER['HTTPS_PROXY'],
$_SERVER['https_proxy'],
$_SERVER['NO_PROXY'],
$_SERVER['no_proxy'],
$_SERVER['CGI_HTTP_PROXY'],
$_SERVER['cgi_http_proxy']
);
ProxyManager::reset();
}
public function testInstantiation(): void
{
$originalInstance = ProxyManager::getInstance();
$sameInstance = ProxyManager::getInstance();
self::assertTrue($originalInstance === $sameInstance);
ProxyManager::reset();
$newInstance = ProxyManager::getInstance();
self::assertFalse($sameInstance === $newInstance);
}
public function testGetProxyForRequestThrowsOnBadProxyUrl(): void
{
$_SERVER['http_proxy'] = 'localhost';
$proxyManager = ProxyManager::getInstance();
self::expectException('Composer\Downloader\TransportException');
$proxyManager->getProxyForRequest('http://example.com');
}
/**
* @param array<string, string> $server
* @param non-empty-string $url
*/
#[DataProvider('dataCaseOverrides')]
public function testLowercaseOverridesUppercase(array $server, string $url, string $expectedUrl): void
{
$_SERVER = array_merge($_SERVER, $server);
$proxyManager = ProxyManager::getInstance();
$proxy = $proxyManager->getProxyForRequest($url);
self::assertSame($expectedUrl, $proxy->getStatus());
}
/**
* @return list<array{0: array<string, string>, 1: string, 2: string}>
*/
public static function dataCaseOverrides(): array
{
// server, url, expectedUrl
return [
[['HTTP_PROXY' => 'http://upper.com', 'http_proxy' => 'http://lower.com'], 'http://repo.org', 'http://lower.com:80'],
[['CGI_HTTP_PROXY' => 'http://upper.com', 'cgi_http_proxy' => 'http://lower.com'], 'http://repo.org', 'http://lower.com:80'],
[['HTTPS_PROXY' => 'http://upper.com', 'https_proxy' => 'http://lower.com'], 'https://repo.org', 'http://lower.com:80'],
];
}
/**
* @param array<string, string> $server
*/
#[DataProvider('dataCGIProxy')]
public function testCGIProxyIsOnlyUsedWhenNoHttpProxy(array $server, string $expectedUrl): void
{
$_SERVER = array_merge($_SERVER, $server);
$proxyManager = ProxyManager::getInstance();
$proxy = $proxyManager->getProxyForRequest('http://repo.org');
self::assertSame($expectedUrl, $proxy->getStatus());
}
/**
* @return list<array{0: array<string, string>, 1: string}>
*/
public static function dataCGIProxy(): array
{
// server, expectedUrl
return [
[['CGI_HTTP_PROXY' => 'http://cgi.com:80'], 'http://cgi.com:80'],
[['http_proxy' => 'http://http.com:80', 'CGI_HTTP_PROXY' => 'http://cgi.com:80'], 'http://http.com:80'],
];
}
public function testNoHttpProxyDoesNotUseHttpsProxy(): void
{
$_SERVER['https_proxy'] = 'https://proxy.com:443';
$proxyManager = ProxyManager::getInstance();
$proxy = $proxyManager->getProxyForRequest('http://repo.org');
self::assertSame('', $proxy->getStatus());
}
public function testNoHttpsProxyDoesNotUseHttpProxy(): void
{
$_SERVER['http_proxy'] = 'http://proxy.com:80';
$proxyManager = ProxyManager::getInstance();
$proxy = $proxyManager->getProxyForRequest('https://repo.org');
self::assertSame('', $proxy->getStatus());
}
/**
* @param array<string, string> $server
* @param non-empty-string $url
* @param ?contextOptions $options
*/
#[DataProvider('dataRequest')]
public function testGetProxyForRequest(array $server, string $url, ?array $options, string $status, bool $excluded): void
{
$_SERVER = array_merge($_SERVER, $server);
$proxyManager = ProxyManager::getInstance();
$proxy = $proxyManager->getProxyForRequest($url);
self::assertSame($options, $proxy->getContextOptions());
self::assertSame($status, $proxy->getStatus());
self::assertSame($excluded, $proxy->isExcludedByNoProxy());
}
/**
* Tests context options. curl options are tested in RequestProxyTest.php
*
* @return list<array{0: array<string, string>, 1: string, 2: ?contextOptions, 3: string, 4: bool}>
*/
public static function dataRequest(): array
{
$server = [
'http_proxy' => 'http://user:p%40ss@proxy.com',
'https_proxy' => 'https://proxy.com:443',
'no_proxy' => 'other.repo.org',
];
// server, url, options, status, excluded
return [
[[], 'http://repo.org', null, '', false],
[$server, 'http://repo.org',
['http' => [
'proxy' => 'tcp://proxy.com:80',
'header' => 'Proxy-Authorization: Basic dXNlcjpwQHNz',
'request_fulluri' => true,
]],
'http://***:***@proxy.com:80',
false,
],
[$server, 'https://repo.org',
['http' => [
'proxy' => 'ssl://proxy.com:443',
]],
'https://proxy.com:443',
false,
],
[$server, 'https://other.repo.org', null, 'excluded by no_proxy', true],
];
}
}
|