1
0
Fork 0
composer/tests/Composer/Test/Util/Http/ProxyHelperTest.php

202 lines
5.7 KiB
PHP
Raw Normal View History

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\ProxyHelper;
use Composer\Test\TestCase;
class ProxyHelperTest 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']
);
}
2021-12-08 16:03:05 +00:00
protected function tearDown(): void
2020-09-24 15:48:22 +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']
);
}
/**
* @dataProvider dataMalformed
*/
2022-02-22 15:47:09 +00:00
public function testThrowsOnMalformedUrl(string $url): void
2020-09-24 15:48:22 +00:00
{
$_SERVER['http_proxy'] = $url;
2021-12-09 19:55:26 +00:00
self::expectException('RuntimeException');
2020-09-24 15:48:22 +00:00
ProxyHelper::getProxyData();
}
public static function dataMalformed(): array
2020-09-24 15:48:22 +00:00
{
2022-08-17 12:20:07 +00:00
return [
'no-host' => ['localhost'],
'no-port' => ['scheme://localhost'],
];
2020-09-24 15:48:22 +00:00
}
/**
* @dataProvider dataFormatting
*/
2022-02-22 15:47:09 +00:00
public function testUrlFormatting(string $url, string $expected): void
2020-09-24 15:48:22 +00:00
{
$_SERVER['http_proxy'] = $url;
2022-08-17 12:20:07 +00:00
[$httpProxy, $httpsProxy, $noProxy] = ProxyHelper::getProxyData();
2020-09-24 15:48:22 +00:00
$this->assertSame($expected, $httpProxy);
}
public static function dataFormatting(): array
2020-09-24 15:48:22 +00:00
{
// url, expected
2022-08-17 12:20:07 +00:00
return [
'lowercases-scheme' => ['HTTP://proxy.com:8888', 'http://proxy.com:8888'],
'adds-http-port' => ['http://proxy.com', 'http://proxy.com:80'],
'adds-https-port' => ['https://proxy.com', 'https://proxy.com:443'],
];
2020-09-24 15:48:22 +00:00
}
/**
* @dataProvider dataCaseOverrides
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 testLowercaseOverridesUppercase(array $server, string $expected, int $index): void
2020-09-24 15:48:22 +00:00
{
$_SERVER = array_merge($_SERVER, $server);
$list = ProxyHelper::getProxyData();
$this->assertSame($expected, $list[$index]);
}
public static function dataCaseOverrides(): array
2020-09-24 15:48:22 +00:00
{
// server, expected, list index
2022-08-17 12:20:07 +00:00
return [
[['HTTP_PROXY' => 'http://upper.com', 'http_proxy' => 'http://lower.com'], 'http://lower.com:80', 0],
[['HTTPS_PROXY' => 'http://upper.com', 'https_proxy' => 'http://lower.com'], 'http://lower.com:80', 1],
[['NO_PROXY' => 'upper.com', 'no_proxy' => 'lower.com'], 'lower.com', 2],
];
2020-09-24 15:48:22 +00:00
}
/**
* @dataProvider dataCGIOverrides
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 testCGIUpperCaseOverridesHttp(array $server, string $expected, int $index): void
2020-09-24 15:48:22 +00:00
{
$_SERVER = array_merge($_SERVER, $server);
$list = ProxyHelper::getProxyData();
$this->assertSame($expected, $list[$index]);
}
public static function dataCGIOverrides(): array
2020-09-24 15:48:22 +00:00
{
2020-11-22 13:48:56 +00:00
// server, expected, list index
2022-08-17 12:20:07 +00:00
return [
[['http_proxy' => 'http://http.com', 'CGI_HTTP_PROXY' => 'http://cgi.com'], 'http://cgi.com:80', 0],
[['http_proxy' => 'http://http.com', 'cgi_http_proxy' => 'http://cgi.com'], 'http://http.com:80', 0],
];
2020-09-24 15:48:22 +00:00
}
public function testNoHttpsProxyUsesHttpProxy(): void
2020-09-24 15:48:22 +00:00
{
$_SERVER['http_proxy'] = 'http://http.com';
2022-08-17 12:20:07 +00:00
[$httpProxy, $httpsProxy, $noProxy] = ProxyHelper::getProxyData();
2020-09-24 15:48:22 +00:00
$this->assertSame('http://http.com:80', $httpsProxy);
}
public function testNoHttpProxyDoesNotUseHttpsProxy(): void
2020-09-24 15:48:22 +00:00
{
$_SERVER['https_proxy'] = 'http://https.com';
2022-08-17 12:20:07 +00:00
[$httpProxy, $httpsProxy, $noProxy] = ProxyHelper::getProxyData();
2020-09-24 15:48:22 +00:00
$this->assertSame(null, $httpProxy);
}
/**
* @dataProvider dataContextOptions
2021-10-27 14:18:46 +00:00
*
* @param array<string, string> $expected
*
* @phpstan-param array{http: array{proxy: string, header?: string}} $expected
2020-09-24 15:48:22 +00:00
*/
2022-02-22 15:47:09 +00:00
public function testGetContextOptions(string $url, array $expected): void
2020-09-24 15:48:22 +00:00
{
$this->assertEquals($expected, ProxyHelper::getContextOptions($url));
}
public static function dataContextOptions(): array
2020-09-24 15:48:22 +00:00
{
// url, expected
2022-08-17 12:20:07 +00:00
return [
['http://proxy.com', ['http' => [
2020-09-24 15:48:22 +00:00
'proxy' => 'tcp://proxy.com:80',
2022-08-17 12:20:07 +00:00
]]],
['https://proxy.com', ['http' => [
2020-09-24 15:48:22 +00:00
'proxy' => 'ssl://proxy.com:443',
2022-08-17 12:20:07 +00:00
]]],
['http://user:p%40ss@proxy.com', ['http' => [
2020-09-24 15:48:22 +00:00
'proxy' => 'tcp://proxy.com:80',
'header' => 'Proxy-Authorization: Basic dXNlcjpwQHNz',
2022-08-17 12:20:07 +00:00
]]],
];
2020-09-24 15:48:22 +00:00
}
/**
* @dataProvider dataRequestFullUri
2021-10-27 14:18:46 +00:00
*
* @param mixed[] $expected
2020-09-24 15:48:22 +00:00
*/
2022-02-22 15:47:09 +00:00
public function testSetRequestFullUri(string $requestUrl, array $expected): void
2020-09-24 15:48:22 +00:00
{
2022-08-17 12:20:07 +00:00
$options = [];
2020-09-24 15:48:22 +00:00
ProxyHelper::setRequestFullUri($requestUrl, $options);
$this->assertEquals($expected, $options);
}
public static function dataRequestFullUri(): array
2020-09-24 15:48:22 +00:00
{
2022-08-17 12:20:07 +00:00
$options = ['http' => ['request_fulluri' => true]];
2020-09-24 15:48:22 +00:00
// $requestUrl, expected
2022-08-17 12:20:07 +00:00
return [
'http' => ['http://repo.org', $options],
'https' => ['https://repo.org', []],
'no-scheme' => ['repo.org', []],
];
2020-09-24 15:48:22 +00:00
}
}