2022-02-23 15:58:18 +00:00
|
|
|
<?php declare(strict_types=1);
|
2012-03-03 20:02:47 +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;
|
|
|
|
|
2020-09-24 15:48:22 +00:00
|
|
|
use Composer\Util\Http\ProxyManager;
|
2012-03-03 20:02:47 +00:00
|
|
|
use Composer\Util\StreamContextFactory;
|
2020-02-07 03:18:45 +00:00
|
|
|
use Composer\Test\TestCase;
|
2012-03-03 20:02:47 +00:00
|
|
|
|
2017-11-04 14:52:13 +00:00
|
|
|
class StreamContextFactoryTest extends TestCase
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function setUp(): void
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2020-09-11 21:51:55 +00:00
|
|
|
unset($_SERVER['HTTP_PROXY'], $_SERVER['http_proxy'], $_SERVER['HTTPS_PROXY'], $_SERVER['https_proxy'], $_SERVER['NO_PROXY'], $_SERVER['no_proxy']);
|
2020-09-24 15:48:22 +00:00
|
|
|
ProxyManager::reset();
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 16:03:05 +00:00
|
|
|
protected function tearDown(): void
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2021-12-09 16:09:07 +00:00
|
|
|
parent::tearDown();
|
2020-09-11 21:51:55 +00:00
|
|
|
unset($_SERVER['HTTP_PROXY'], $_SERVER['http_proxy'], $_SERVER['HTTPS_PROXY'], $_SERVER['https_proxy'], $_SERVER['NO_PROXY'], $_SERVER['no_proxy']);
|
2020-09-24 15:48:22 +00:00
|
|
|
ProxyManager::reset();
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetContext
|
2021-10-27 14:18:46 +00:00
|
|
|
*
|
|
|
|
* @param mixed[] $expectedOptions
|
|
|
|
* @param mixed[] $defaultOptions
|
|
|
|
* @param mixed[] $expectedParams
|
|
|
|
* @param mixed[] $defaultParams
|
2012-03-03 20:02:47 +00:00
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testGetContext(array $expectedOptions, array $defaultOptions, array $expectedParams, array $defaultParams): void
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2013-05-30 12:58:26 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', $defaultOptions, $defaultParams);
|
2012-03-03 20:02:47 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
$params = stream_context_get_params($context);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals($expectedOptions, $options);
|
|
|
|
self::assertEquals($expectedParams, $params);
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function dataGetContext(): array
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
[
|
|
|
|
$a = ['http' => ['follow_location' => 1, 'max_redirects' => 20, 'header' => ['User-Agent: foo']]], ['http' => ['header' => 'User-Agent: foo']],
|
|
|
|
['options' => $a], [],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
$a = ['http' => ['method' => 'GET', 'max_redirects' => 20, 'follow_location' => 1, 'header' => ['User-Agent: foo']]], ['http' => ['method' => 'GET', 'header' => 'User-Agent: foo']],
|
|
|
|
['options' => $a, 'notification' => $f = static function (): void {
|
|
|
|
}], ['notification' => $f],
|
|
|
|
],
|
|
|
|
];
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testHttpProxy(): void
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2013-10-18 09:35:09 +00:00
|
|
|
$_SERVER['http_proxy'] = 'http://username:p%40ssword@proxyserver.net:3128/';
|
2012-03-20 13:37:14 +00:00
|
|
|
$_SERVER['HTTP_PROXY'] = 'http://proxyserver/';
|
2012-03-03 20:02:47 +00:00
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', ['http' => ['method' => 'GET', 'header' => 'User-Agent: foo']]);
|
2012-03-03 20:02:47 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(['http' => [
|
2012-05-02 00:36:05 +00:00
|
|
|
'proxy' => 'tcp://proxyserver.net:3128',
|
2012-03-03 20:02:47 +00:00
|
|
|
'request_fulluri' => true,
|
|
|
|
'method' => 'GET',
|
2022-08-17 12:20:07 +00:00
|
|
|
'header' => ['User-Agent: foo', "Proxy-Authorization: Basic " . base64_encode('username:p@ssword')],
|
2013-04-05 10:58:50 +00:00
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2022-08-17 12:20:07 +00:00
|
|
|
]], $options);
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testHttpProxyWithNoProxy(): void
|
2013-08-11 22:52:16 +00:00
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
$_SERVER['no_proxy'] = 'foo,example.org';
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', ['http' => ['method' => 'GET', 'header' => 'User-Agent: foo']]);
|
2013-08-11 22:52:16 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(['http' => [
|
2013-08-11 22:52:16 +00:00
|
|
|
'method' => 'GET',
|
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2022-08-17 12:20:07 +00:00
|
|
|
'header' => ['User-Agent: foo'],
|
|
|
|
]], $options);
|
2013-08-11 22:52:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testHttpProxyWithNoProxyWildcard(): void
|
2013-08-11 22:52:16 +00:00
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
$_SERVER['no_proxy'] = '*';
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', ['http' => ['method' => 'GET', 'header' => 'User-Agent: foo']]);
|
2013-08-11 22:52:16 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(['http' => [
|
2013-08-11 22:52:16 +00:00
|
|
|
'method' => 'GET',
|
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2022-08-17 12:20:07 +00:00
|
|
|
'header' => ['User-Agent: foo'],
|
|
|
|
]], $options);
|
2013-08-11 22:52:16 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testOptionsArePreserved(): void
|
2012-05-06 11:50:18 +00:00
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', ['http' => ['method' => 'GET', 'header' => ['User-Agent: foo', "X-Foo: bar"], 'request_fulluri' => false]]);
|
2012-05-06 11:50:18 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(['http' => [
|
2012-05-06 11:50:18 +00:00
|
|
|
'proxy' => 'tcp://proxyserver.net:3128',
|
|
|
|
'request_fulluri' => false,
|
|
|
|
'method' => 'GET',
|
2022-08-17 12:20:07 +00:00
|
|
|
'header' => ['User-Agent: foo', "X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password')],
|
2013-04-05 10:58:50 +00:00
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2022-08-17 12:20:07 +00:00
|
|
|
]], $options);
|
2012-05-06 11:50:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testHttpProxyWithoutPort(): void
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2012-04-27 01:38:12 +00:00
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$context = StreamContextFactory::getContext('https://example.org', ['http' => ['method' => 'GET', 'header' => 'User-Agent: foo']]);
|
2012-04-27 01:38:12 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(['http' => [
|
2020-09-29 12:04:30 +00:00
|
|
|
'proxy' => 'tcp://proxyserver.net:80',
|
|
|
|
'method' => 'GET',
|
2022-08-17 12:20:07 +00:00
|
|
|
'header' => ['User-Agent: foo', "Proxy-Authorization: Basic " . base64_encode('username:password')],
|
2020-09-29 12:04:30 +00:00
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2022-08-17 12:20:07 +00:00
|
|
|
]], $options);
|
2012-04-27 01:38:12 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testHttpsProxyOverride(): void
|
2014-12-11 17:42:55 +00:00
|
|
|
{
|
2014-12-12 13:15:34 +00:00
|
|
|
if (!extension_loaded('openssl')) {
|
|
|
|
$this->markTestSkipped('Requires openssl');
|
|
|
|
}
|
|
|
|
|
2014-12-11 17:42:55 +00:00
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
|
2014-12-11 21:52:29 +00:00
|
|
|
$_SERVER['https_proxy'] = 'https://woopproxy.net';
|
2014-12-11 17:42:55 +00:00
|
|
|
|
2020-09-24 15:48:22 +00:00
|
|
|
// Pointless test replaced by ProxyHelperTest.php
|
2021-12-09 19:55:26 +00:00
|
|
|
self::expectException('Composer\Downloader\TransportException');
|
2022-08-17 12:20:07 +00:00
|
|
|
$context = StreamContextFactory::getContext('https://example.org', ['http' => ['method' => 'GET', 'header' => 'User-Agent: foo']]);
|
2014-12-11 17:42:55 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 01:38:12 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider dataSSLProxy
|
|
|
|
*/
|
2022-02-22 15:47:09 +00:00
|
|
|
public function testSSLProxy(string $expected, string $proxy): void
|
2012-04-27 01:38:12 +00:00
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = $proxy;
|
2012-03-03 20:02:47 +00:00
|
|
|
|
|
|
|
if (extension_loaded('openssl')) {
|
2022-08-17 12:20:07 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', ['http' => ['header' => 'User-Agent: foo']]);
|
2012-03-03 20:02:47 +00:00
|
|
|
$options = stream_context_get_options($context);
|
2012-05-02 09:21:58 +00:00
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(['http' => [
|
2012-04-27 01:38:12 +00:00
|
|
|
'proxy' => $expected,
|
2012-03-03 20:02:47 +00:00
|
|
|
'request_fulluri' => true,
|
2013-04-05 10:58:50 +00:00
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2022-08-17 12:20:07 +00:00
|
|
|
'header' => ['User-Agent: foo'],
|
|
|
|
]], $options);
|
2012-03-03 20:02:47 +00:00
|
|
|
} else {
|
|
|
|
try {
|
2013-05-30 13:28:38 +00:00
|
|
|
StreamContextFactory::getContext('http://example.org');
|
2012-03-03 20:02:47 +00:00
|
|
|
$this->fail();
|
2013-05-30 13:28:38 +00:00
|
|
|
} catch (\RuntimeException $e) {
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertInstanceOf('Composer\Downloader\TransportException', $e);
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-27 01:38:12 +00:00
|
|
|
|
2022-11-24 13:39:08 +00:00
|
|
|
public static function dataSSLProxy(): array
|
2012-04-27 01:38:12 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
return [
|
|
|
|
['ssl://proxyserver:443', 'https://proxyserver/'],
|
|
|
|
['ssl://proxyserver:8443', 'https://proxyserver:8443'],
|
|
|
|
];
|
2012-04-27 01:38:12 +00:00
|
|
|
}
|
2013-02-27 16:07:13 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testEnsureThatfixHttpHeaderFieldMovesContentTypeToEndOfOptions(): void
|
2013-02-27 16:07:13 +00:00
|
|
|
{
|
2022-08-17 12:20:07 +00:00
|
|
|
$options = [
|
|
|
|
'http' => [
|
2015-11-23 12:33:50 +00:00
|
|
|
'header' => "User-agent: foo\r\nX-Foo: bar\r\nContent-Type: application/json\r\nAuthorization: Basic aW52YWxpZA==",
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
];
|
|
|
|
$expectedOptions = [
|
|
|
|
'http' => [
|
|
|
|
'header' => [
|
2015-11-23 12:33:50 +00:00
|
|
|
"User-agent: foo",
|
2013-02-27 16:07:13 +00:00
|
|
|
"X-Foo: bar",
|
|
|
|
"Authorization: Basic aW52YWxpZA==",
|
2015-09-28 09:51:14 +00:00
|
|
|
"Content-Type: application/json",
|
2022-08-17 12:20:07 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
2013-05-30 12:58:26 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', $options);
|
2013-02-27 16:07:13 +00:00
|
|
|
$ctxoptions = stream_context_get_options($context);
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertEquals(end($expectedOptions['http']['header']), end($ctxoptions['http']['header']));
|
2013-02-27 16:07:13 +00:00
|
|
|
}
|
2020-09-24 15:48:22 +00:00
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInitOptionsDoesIncludeProxyAuthHeaders(): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$options = [];
|
2020-09-24 15:48:22 +00:00
|
|
|
$options = StreamContextFactory::initOptions('https://example.org', $options);
|
|
|
|
$headers = implode(' ', $options['http']['header']);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertTrue(false !== stripos($headers, 'Proxy-Authorization'));
|
2020-09-24 15:48:22 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 09:38:54 +00:00
|
|
|
public function testInitOptionsForCurlDoesNotIncludeProxyAuthHeaders(): void
|
2020-09-24 15:48:22 +00:00
|
|
|
{
|
2020-10-24 17:20:31 +00:00
|
|
|
if (!extension_loaded('curl')) {
|
|
|
|
$this->markTestSkipped('The curl is not available.');
|
|
|
|
}
|
|
|
|
|
2020-09-24 15:48:22 +00:00
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
|
2022-08-17 12:20:07 +00:00
|
|
|
$options = [];
|
2020-09-24 15:48:22 +00:00
|
|
|
$options = StreamContextFactory::initOptions('https://example.org', $options, true);
|
|
|
|
$headers = implode(' ', $options['http']['header']);
|
|
|
|
|
2024-05-29 21:12:06 +00:00
|
|
|
self::assertFalse(stripos($headers, 'Proxy-Authorization'));
|
2020-09-24 15:48:22 +00:00
|
|
|
}
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|