2012-03-03 20:02:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
use Composer\Util\StreamContextFactory;
|
|
|
|
|
|
|
|
class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
protected function setUp()
|
|
|
|
{
|
|
|
|
unset($_SERVER['HTTP_PROXY']);
|
|
|
|
unset($_SERVER['http_proxy']);
|
2014-12-11 17:42:55 +00:00
|
|
|
unset($_SERVER['HTTPS_PROXY']);
|
|
|
|
unset($_SERVER['https_proxy']);
|
2013-08-11 22:52:16 +00:00
|
|
|
unset($_SERVER['no_proxy']);
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
unset($_SERVER['HTTP_PROXY']);
|
|
|
|
unset($_SERVER['http_proxy']);
|
2014-12-11 17:42:55 +00:00
|
|
|
unset($_SERVER['HTTPS_PROXY']);
|
|
|
|
unset($_SERVER['https_proxy']);
|
2013-08-11 22:52:16 +00:00
|
|
|
unset($_SERVER['no_proxy']);
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetContext
|
|
|
|
*/
|
|
|
|
public function testGetContext($expectedOptions, $defaultOptions, $expectedParams, $defaultParams)
|
|
|
|
{
|
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);
|
|
|
|
|
|
|
|
$this->assertEquals($expectedOptions, $options);
|
|
|
|
$this->assertEquals($expectedParams, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataGetContext()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array(
|
2013-04-05 10:58:50 +00:00
|
|
|
$a = array('http' => array('follow_location' => 1, 'max_redirects' => 20)), array(),
|
|
|
|
array('options' => $a), array()
|
2012-03-03 20:02:47 +00:00
|
|
|
),
|
|
|
|
array(
|
2013-04-05 10:58:50 +00:00
|
|
|
$a = array('http' => array('method' => 'GET', 'max_redirects' => 20, 'follow_location' => 1)), array('http' => array('method' => 'GET')),
|
2014-06-10 14:02:44 +00:00
|
|
|
array('options' => $a, 'notification' => $f = function () {}), array('notification' => $f)
|
2012-03-03 20:02:47 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHttpProxy()
|
|
|
|
{
|
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
|
|
|
|
2013-05-30 12:58:26 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
|
2012-03-03 20:02:47 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
|
|
|
$this->assertEquals(array('http' => array(
|
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',
|
2013-10-18 09:35:09 +00:00
|
|
|
'header' => array("Proxy-Authorization: Basic " . base64_encode('username:p@ssword')),
|
2013-04-05 10:58:50 +00:00
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2012-03-03 20:02:47 +00:00
|
|
|
)), $options);
|
|
|
|
}
|
|
|
|
|
2013-08-11 22:52:16 +00:00
|
|
|
public function testHttpProxyWithNoProxy()
|
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
$_SERVER['no_proxy'] = 'foo,example.org';
|
|
|
|
|
|
|
|
$context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
|
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
|
|
|
$this->assertEquals(array('http' => array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
|
|
|
)), $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHttpProxyWithNoProxyWildcard()
|
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
$_SERVER['no_proxy'] = '*';
|
|
|
|
|
|
|
|
$context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
|
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
|
|
|
$this->assertEquals(array('http' => array(
|
|
|
|
'method' => 'GET',
|
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
|
|
|
)), $options);
|
|
|
|
}
|
|
|
|
|
2012-05-06 11:50:18 +00:00
|
|
|
public function testOptionsArePreserved()
|
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
|
2013-05-30 12:58:26 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET', 'header' => array("X-Foo: bar"), 'request_fulluri' => false)));
|
2012-05-06 11:50:18 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
|
|
|
$this->assertEquals(array('http' => array(
|
|
|
|
'proxy' => 'tcp://proxyserver.net:3128',
|
|
|
|
'request_fulluri' => false,
|
|
|
|
'method' => 'GET',
|
2013-04-05 10:58:50 +00:00
|
|
|
'header' => array("X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password')),
|
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
2012-05-06 11:50:18 +00:00
|
|
|
)), $options);
|
|
|
|
}
|
|
|
|
|
2012-04-27 01:38:12 +00:00
|
|
|
public function testHttpProxyWithoutPort()
|
2012-03-03 20:02:47 +00:00
|
|
|
{
|
2012-04-27 01:38:12 +00:00
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
|
|
|
|
|
2014-12-11 17:42:55 +00:00
|
|
|
$context = StreamContextFactory::getContext('https://example.org', array('http' => array('method' => 'GET')));
|
2012-04-27 01:38:12 +00:00
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
2014-12-12 12:25:21 +00:00
|
|
|
$expected = array(
|
|
|
|
'http' => array(
|
|
|
|
'proxy' => 'tcp://proxyserver.net:80',
|
|
|
|
'request_fulluri' => true,
|
|
|
|
'method' => 'GET',
|
|
|
|
'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password')),
|
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
|
|
|
), 'ssl' => array(
|
|
|
|
'SNI_enabled' => true,
|
|
|
|
'SNI_server_name' => 'example.org'
|
|
|
|
)
|
|
|
|
);
|
2015-05-05 18:18:24 +00:00
|
|
|
if (PHP_VERSION_ID >= 50600) {
|
2014-12-12 12:25:21 +00:00
|
|
|
unset($expected['ssl']['SNI_server_name']);
|
|
|
|
}
|
|
|
|
$this->assertEquals($expected, $options);
|
2012-04-27 01:38:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 17:42:55 +00:00
|
|
|
public function testHttpsProxyOverride()
|
|
|
|
{
|
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
|
|
|
|
|
|
|
$context = StreamContextFactory::getContext('https://example.org', array('http' => array('method' => 'GET')));
|
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
2014-12-12 12:25:21 +00:00
|
|
|
$expected = array(
|
|
|
|
'http' => array(
|
|
|
|
'proxy' => 'ssl://woopproxy.net:443',
|
|
|
|
'request_fulluri' => true,
|
|
|
|
'method' => 'GET',
|
|
|
|
'max_redirects' => 20,
|
|
|
|
'follow_location' => 1,
|
|
|
|
), 'ssl' => array(
|
|
|
|
'SNI_enabled' => true,
|
|
|
|
'SNI_server_name' => 'example.org'
|
|
|
|
)
|
|
|
|
);
|
2015-05-05 18:18:24 +00:00
|
|
|
if (PHP_VERSION_ID >= 50600) {
|
2014-12-12 12:25:21 +00:00
|
|
|
unset($expected['ssl']['SNI_server_name']);
|
|
|
|
}
|
|
|
|
$this->assertEquals($expected, $options);
|
2014-12-11 17:42:55 +00:00
|
|
|
}
|
|
|
|
|
2012-04-27 01:38:12 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider dataSSLProxy
|
|
|
|
*/
|
|
|
|
public function testSSLProxy($expected, $proxy)
|
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = $proxy;
|
2012-03-03 20:02:47 +00:00
|
|
|
|
|
|
|
if (extension_loaded('openssl')) {
|
2013-05-30 12:58:26 +00:00
|
|
|
$context = StreamContextFactory::getContext('http://example.org');
|
2012-03-03 20:02:47 +00:00
|
|
|
$options = stream_context_get_options($context);
|
2012-05-02 09:21:58 +00:00
|
|
|
|
2012-05-02 01:30:27 +00:00
|
|
|
$this->assertEquals(array('http' => array(
|
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,
|
2012-03-03 20:02:47 +00:00
|
|
|
)), $options);
|
|
|
|
} 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) {
|
2012-03-03 20:02:47 +00:00
|
|
|
$this->assertInstanceOf('RuntimeException', $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-27 01:38:12 +00:00
|
|
|
|
|
|
|
public function dataSSLProxy()
|
|
|
|
{
|
|
|
|
return array(
|
|
|
|
array('ssl://proxyserver:443', 'https://proxyserver/'),
|
|
|
|
array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
|
|
|
|
);
|
|
|
|
}
|
2013-02-27 16:07:13 +00:00
|
|
|
|
|
|
|
public function testEnsureThatfixHttpHeaderFieldMovesContentTypeToEndOfOptions()
|
|
|
|
{
|
|
|
|
$options = array(
|
|
|
|
'http' => array(
|
|
|
|
'header' => "X-Foo: bar\r\nContent-Type: application/json\r\nAuthorization: Basic aW52YWxpZA=="
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$expectedOptions = array(
|
|
|
|
'http' => array(
|
|
|
|
'header' => array(
|
|
|
|
"X-Foo: bar",
|
|
|
|
"Authorization: Basic aW52YWxpZA==",
|
|
|
|
"Content-Type: application/json"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
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);
|
2013-10-15 00:53:02 +00:00
|
|
|
$this->assertEquals(end($ctxoptions['http']['header']), end($expectedOptions['http']['header']));
|
2013-02-27 16:07:13 +00:00
|
|
|
}
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|