StreamContextFactoryTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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->assertEquals(array('http' => array(
  56. 'proxy' => 'tcp://proxyserver.net:3128',
  57. 'request_fulluri' => true,
  58. 'method' => 'GET',
  59. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password'))
  60. )), $options);
  61. }
  62. public function testOptionsArePreserved()
  63. {
  64. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  65. $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET', 'header' => array("X-Foo: bar"), 'request_fulluri' => false)));
  66. $options = stream_context_get_options($context);
  67. $this->assertEquals(array('http' => array(
  68. 'proxy' => 'tcp://proxyserver.net:3128',
  69. 'request_fulluri' => false,
  70. 'method' => 'GET',
  71. 'header' => array("X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password'))
  72. )), $options);
  73. }
  74. public function testHttpProxyWithoutPort()
  75. {
  76. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  77. $context = StreamContextFactory::getContext(array('http' => array('method' => 'GET')));
  78. $options = stream_context_get_options($context);
  79. $this->assertEquals(array('http' => array(
  80. 'proxy' => 'tcp://proxyserver.net:80',
  81. 'request_fulluri' => true,
  82. 'method' => 'GET',
  83. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password'))
  84. )), $options);
  85. }
  86. /**
  87. * @dataProvider dataSSLProxy
  88. */
  89. public function testSSLProxy($expected, $proxy)
  90. {
  91. $_SERVER['http_proxy'] = $proxy;
  92. if (extension_loaded('openssl')) {
  93. $context = StreamContextFactory::getContext();
  94. $options = stream_context_get_options($context);
  95. $this->assertEquals(array('http' => array(
  96. 'proxy' => $expected,
  97. 'request_fulluri' => true,
  98. )), $options);
  99. } else {
  100. try {
  101. StreamContextFactory::getContext();
  102. $this->fail();
  103. } catch (\Exception $e) {
  104. $this->assertInstanceOf('RuntimeException', $e);
  105. }
  106. }
  107. }
  108. public function dataSSLProxy()
  109. {
  110. return array(
  111. array('ssl://proxyserver:443', 'https://proxyserver/'),
  112. array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
  113. );
  114. }
  115. }