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']);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function tearDown()
|
|
|
|
{
|
|
|
|
unset($_SERVER['HTTP_PROXY']);
|
|
|
|
unset($_SERVER['http_proxy']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetContext
|
|
|
|
*/
|
|
|
|
public function testGetContext($expectedOptions, $defaultOptions, $expectedParams, $defaultParams)
|
|
|
|
{
|
|
|
|
$context = StreamContextFactory::getContext($defaultOptions, $defaultParams);
|
|
|
|
$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(
|
|
|
|
array(), array(),
|
|
|
|
array('options' => array()), array()
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
$a = array('http' => array('method' => 'GET')), $a,
|
|
|
|
array('options' => $a, 'notification' => $f = function() {}), array('notification' => $f)
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testHttpProxy()
|
|
|
|
{
|
2012-04-27 01:38:12 +00:00
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
2012-03-20 13:37:14 +00:00
|
|
|
$_SERVER['HTTP_PROXY'] = 'http://proxyserver/';
|
2012-03-03 20:02:47 +00:00
|
|
|
|
|
|
|
$context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
|
|
|
|
$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',
|
2012-10-21 15:57:16 +00:00
|
|
|
'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password'))
|
2012-03-03 20:02:47 +00:00
|
|
|
)), $options);
|
|
|
|
}
|
|
|
|
|
2012-05-06 11:50:18 +00:00
|
|
|
public function testOptionsArePreserved()
|
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
|
|
|
|
|
2012-10-21 15:57:16 +00:00
|
|
|
$context = StreamContextFactory::getContext(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',
|
2012-10-21 15:57:16 +00:00
|
|
|
'header' => array("X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password'))
|
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';
|
|
|
|
|
|
|
|
$context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
|
|
|
|
$options = stream_context_get_options($context);
|
|
|
|
|
|
|
|
$this->assertEquals(array('http' => array(
|
2012-05-02 00:36:05 +00:00
|
|
|
'proxy' => 'tcp://proxyserver.net:80',
|
2012-04-27 01:38:12 +00:00
|
|
|
'request_fulluri' => true,
|
|
|
|
'method' => 'GET',
|
2012-10-21 15:57:16 +00:00
|
|
|
'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password'))
|
2012-04-27 01:38:12 +00:00
|
|
|
)), $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataSSLProxy
|
|
|
|
*/
|
|
|
|
public function testSSLProxy($expected, $proxy)
|
|
|
|
{
|
|
|
|
$_SERVER['http_proxy'] = $proxy;
|
2012-03-03 20:02:47 +00:00
|
|
|
|
|
|
|
if (extension_loaded('openssl')) {
|
|
|
|
$context = StreamContextFactory::getContext();
|
|
|
|
$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,
|
|
|
|
)), $options);
|
|
|
|
} else {
|
|
|
|
try {
|
|
|
|
StreamContextFactory::getContext();
|
|
|
|
$this->fail();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$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
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Markus Tacker <m@coderbyheart.de>
|
|
|
|
*/
|
|
|
|
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"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$context = StreamContextFactory::getContext($options);
|
|
|
|
$ctxoptions = stream_context_get_options($context);
|
|
|
|
$this->assertEquals(join("\n", $ctxoptions['http']['header']), join("\n", $expectedOptions['http']['header']));
|
|
|
|
}
|
2012-03-03 20:02:47 +00:00
|
|
|
}
|