StreamContextFactoryTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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('http://example.org', $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. $a = array('http' => array('follow_location' => 1, 'max_redirects' => 20)), array(),
  41. array('options' => $a), array()
  42. ),
  43. array(
  44. $a = array('http' => array('method' => 'GET', 'max_redirects' => 20, 'follow_location' => 1)), array('http' => array('method' => 'GET')),
  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('http://example.org', 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. 'max_redirects' => 20,
  61. 'follow_location' => 1,
  62. )), $options);
  63. }
  64. public function testOptionsArePreserved()
  65. {
  66. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  67. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET', 'header' => array("X-Foo: bar"), 'request_fulluri' => false)));
  68. $options = stream_context_get_options($context);
  69. $this->assertEquals(array('http' => array(
  70. 'proxy' => 'tcp://proxyserver.net:3128',
  71. 'request_fulluri' => false,
  72. 'method' => 'GET',
  73. 'header' => array("X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password')),
  74. 'max_redirects' => 20,
  75. 'follow_location' => 1,
  76. )), $options);
  77. }
  78. public function testHttpProxyWithoutPort()
  79. {
  80. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  81. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  82. $options = stream_context_get_options($context);
  83. $this->assertEquals(array('http' => array(
  84. 'proxy' => 'tcp://proxyserver.net:80',
  85. 'request_fulluri' => true,
  86. 'method' => 'GET',
  87. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password')),
  88. 'max_redirects' => 20,
  89. 'follow_location' => 1,
  90. )), $options);
  91. }
  92. /**
  93. * @dataProvider dataSSLProxy
  94. */
  95. public function testSSLProxy($expected, $proxy)
  96. {
  97. $_SERVER['http_proxy'] = $proxy;
  98. if (extension_loaded('openssl')) {
  99. $context = StreamContextFactory::getContext('http://example.org');
  100. $options = stream_context_get_options($context);
  101. $this->assertEquals(array('http' => array(
  102. 'proxy' => $expected,
  103. 'request_fulluri' => true,
  104. 'max_redirects' => 20,
  105. 'follow_location' => 1,
  106. )), $options);
  107. } else {
  108. try {
  109. StreamContextFactory::getContext('http://example.org');
  110. $this->fail();
  111. } catch (\RuntimeException $e) {
  112. $this->assertInstanceOf('RuntimeException', $e);
  113. }
  114. }
  115. }
  116. public function dataSSLProxy()
  117. {
  118. return array(
  119. array('ssl://proxyserver:443', 'https://proxyserver/'),
  120. array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
  121. );
  122. }
  123. public function testEnsureThatfixHttpHeaderFieldMovesContentTypeToEndOfOptions()
  124. {
  125. $options = array(
  126. 'http' => array(
  127. 'header' => "X-Foo: bar\r\nContent-Type: application/json\r\nAuthorization: Basic aW52YWxpZA=="
  128. )
  129. );
  130. $expectedOptions = array(
  131. 'http' => array(
  132. 'header' => array(
  133. "X-Foo: bar",
  134. "Authorization: Basic aW52YWxpZA==",
  135. "Content-Type: application/json"
  136. )
  137. )
  138. );
  139. $context = StreamContextFactory::getContext('http://example.org', $options);
  140. $ctxoptions = stream_context_get_options($context);
  141. $this->assertEquals(join("\n", $ctxoptions['http']['header']), join("\n", $expectedOptions['http']['header']));
  142. }
  143. }