StreamContextFactoryTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Test\Util;
  12. use Composer\Util\StreamContextFactory;
  13. class StreamContextFactoryTest extends \PHPUnit_Framework_TestCase
  14. {
  15. protected function setUp()
  16. {
  17. unset($_SERVER['HTTP_PROXY']);
  18. unset($_SERVER['http_proxy']);
  19. }
  20. protected function tearDown()
  21. {
  22. unset($_SERVER['HTTP_PROXY']);
  23. unset($_SERVER['http_proxy']);
  24. }
  25. /**
  26. * @dataProvider dataGetContext
  27. */
  28. public function testGetContext($expectedOptions, $defaultOptions, $expectedParams, $defaultParams)
  29. {
  30. $context = StreamContextFactory::getContext($defaultOptions, $defaultParams);
  31. $options = stream_context_get_options($context);
  32. $params = stream_context_get_params($context);
  33. $this->assertEquals($expectedOptions, $options);
  34. $this->assertEquals($expectedParams, $params);
  35. }
  36. public function dataGetContext()
  37. {
  38. return array(
  39. array(
  40. array(), array(),
  41. array('options' => array()), array()
  42. ),
  43. array(
  44. $a = array('http' => array('method' => 'GET')), $a,
  45. array('options' => $a, 'notification' => $f = function() {}), array('notification' => $f)
  46. ),
  47. );
  48. }
  49. public function testHttpProxy()
  50. {
  51. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  52. $_SERVER['HTTP_PROXY'] = 'http://proxyserver/';
  53. $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
  54. $options = stream_context_get_options($context);
  55. $this->assertSame('http://proxyserver/', $_SERVER['HTTP_PROXY']);
  56. $this->assertEquals(array('http' => array(
  57. 'proxy' => 'tcp://username:password@proxyserver.net:3128',
  58. 'request_fulluri' => true,
  59. 'method' => 'GET',
  60. )), $options);
  61. }
  62. public function testHttpProxyWithoutPort()
  63. {
  64. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  65. $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
  66. $options = stream_context_get_options($context);
  67. $this->assertEquals(array('http' => array(
  68. 'proxy' => 'tcp://username:password@proxyserver.net:80',
  69. 'request_fulluri' => true,
  70. 'method' => 'GET',
  71. )), $options);
  72. }
  73. /**
  74. * @dataProvider dataSSLProxy
  75. */
  76. public function testSSLProxy($expected, $proxy)
  77. {
  78. $_SERVER['http_proxy'] = $proxy;
  79. if (extension_loaded('openssl')) {
  80. $context = StreamContextFactory::getContext();
  81. $options = stream_context_get_options($context);
  82. $this->assertEquals(array('http' => array(
  83. 'proxy' => $expected,
  84. 'request_fulluri' => true,
  85. )), $options);
  86. } else {
  87. try {
  88. StreamContextFactory::getContext();
  89. $this->fail();
  90. } catch (\Exception $e) {
  91. $this->assertInstanceOf('RuntimeException', $e);
  92. }
  93. }
  94. }
  95. public function dataSSLProxy()
  96. {
  97. return array(
  98. array('ssl://proxyserver:443', 'https://proxyserver/'),
  99. array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
  100. );
  101. }
  102. }