StreamContextFactoryTest.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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:1234';
  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:1234',
  57. 'request_fulluri' => true,
  58. 'method' => 'GET',
  59. 'header' => "Proxy-Authorization: Basic " . base64_encode('username:password') . "\r\n"
  60. )), $options);
  61. }
  62. public function testSSLProxy()
  63. {
  64. $_SERVER['http_proxy'] = 'https://proxyserver';
  65. if (extension_loaded('openssl')) {
  66. $context = StreamContextFactory::getContext();
  67. $options = stream_context_get_options($context);
  68. $this->assertSame(array('http' => array(
  69. 'proxy' => 'ssl://proxyserver',
  70. 'request_fulluri' => true,
  71. )), $options);
  72. } else {
  73. try {
  74. StreamContextFactory::getContext();
  75. $this->fail();
  76. } catch (\Exception $e) {
  77. $this->assertInstanceOf('RuntimeException', $e);
  78. }
  79. }
  80. }
  81. }