StreamContextFactoryTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. unset($_SERVER['HTTPS_PROXY']);
  20. unset($_SERVER['https_proxy']);
  21. unset($_SERVER['no_proxy']);
  22. }
  23. protected function tearDown()
  24. {
  25. unset($_SERVER['HTTP_PROXY']);
  26. unset($_SERVER['http_proxy']);
  27. unset($_SERVER['HTTPS_PROXY']);
  28. unset($_SERVER['https_proxy']);
  29. unset($_SERVER['no_proxy']);
  30. }
  31. /**
  32. * @dataProvider dataGetContext
  33. */
  34. public function testGetContext($expectedOptions, $defaultOptions, $expectedParams, $defaultParams)
  35. {
  36. $context = StreamContextFactory::getContext('http://example.org', $defaultOptions, $defaultParams);
  37. $options = stream_context_get_options($context);
  38. $params = stream_context_get_params($context);
  39. $this->assertEquals($expectedOptions, $options);
  40. $this->assertEquals($expectedParams, $params);
  41. }
  42. public function dataGetContext()
  43. {
  44. return array(
  45. array(
  46. $a = array('http' => array('follow_location' => 1, 'max_redirects' => 20)), array(),
  47. array('options' => $a), array()
  48. ),
  49. array(
  50. $a = array('http' => array('method' => 'GET', 'max_redirects' => 20, 'follow_location' => 1)), array('http' => array('method' => 'GET')),
  51. array('options' => $a, 'notification' => $f = function () {}), array('notification' => $f)
  52. ),
  53. );
  54. }
  55. public function testHttpProxy()
  56. {
  57. $_SERVER['http_proxy'] = 'http://username:p%40ssword@proxyserver.net:3128/';
  58. $_SERVER['HTTP_PROXY'] = 'http://proxyserver/';
  59. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  60. $options = stream_context_get_options($context);
  61. $this->assertEquals(array('http' => array(
  62. 'proxy' => 'tcp://proxyserver.net:3128',
  63. 'request_fulluri' => true,
  64. 'method' => 'GET',
  65. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:p@ssword')),
  66. 'max_redirects' => 20,
  67. 'follow_location' => 1,
  68. )), $options);
  69. }
  70. public function testHttpProxyWithNoProxy()
  71. {
  72. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  73. $_SERVER['no_proxy'] = 'foo,example.org';
  74. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  75. $options = stream_context_get_options($context);
  76. $this->assertEquals(array('http' => array(
  77. 'method' => 'GET',
  78. 'max_redirects' => 20,
  79. 'follow_location' => 1,
  80. )), $options);
  81. }
  82. public function testHttpProxyWithNoProxyWildcard()
  83. {
  84. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  85. $_SERVER['no_proxy'] = '*';
  86. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  87. $options = stream_context_get_options($context);
  88. $this->assertEquals(array('http' => array(
  89. 'method' => 'GET',
  90. 'max_redirects' => 20,
  91. 'follow_location' => 1,
  92. )), $options);
  93. }
  94. public function testOptionsArePreserved()
  95. {
  96. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  97. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET', 'header' => array("X-Foo: bar"), 'request_fulluri' => false)));
  98. $options = stream_context_get_options($context);
  99. $this->assertEquals(array('http' => array(
  100. 'proxy' => 'tcp://proxyserver.net:3128',
  101. 'request_fulluri' => false,
  102. 'method' => 'GET',
  103. 'header' => array("X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password')),
  104. 'max_redirects' => 20,
  105. 'follow_location' => 1,
  106. )), $options);
  107. }
  108. public function testHttpProxyWithoutPort()
  109. {
  110. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  111. $context = StreamContextFactory::getContext('https://example.org', array('http' => array('method' => 'GET')));
  112. $options = stream_context_get_options($context);
  113. $this->assertEquals(array('http' => array(
  114. 'proxy' => 'tcp://proxyserver.net:80',
  115. 'request_fulluri' => true,
  116. 'method' => 'GET',
  117. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password')),
  118. 'max_redirects' => 20,
  119. 'follow_location' => 1,
  120. )), $options);
  121. }
  122. public function testHttpsProxyOverride()
  123. {
  124. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  125. $_SERVER['http_proxy'] = 'https://woopproxy.net';
  126. $context = StreamContextFactory::getContext('https://example.org', array('http' => array('method' => 'GET')));
  127. $options = stream_context_get_options($context);
  128. $this->assertEquals(array('http' => array(
  129. 'proxy' => 'ssl://woopproxy.net:443',
  130. 'request_fulluri' => true,
  131. 'method' => 'GET',
  132. 'max_redirects' => 20,
  133. 'follow_location' => 1,
  134. )), $options);
  135. }
  136. /**
  137. * @dataProvider dataSSLProxy
  138. */
  139. public function testSSLProxy($expected, $proxy)
  140. {
  141. $_SERVER['http_proxy'] = $proxy;
  142. if (extension_loaded('openssl')) {
  143. $context = StreamContextFactory::getContext('http://example.org');
  144. $options = stream_context_get_options($context);
  145. $this->assertEquals(array('http' => array(
  146. 'proxy' => $expected,
  147. 'request_fulluri' => true,
  148. 'max_redirects' => 20,
  149. 'follow_location' => 1,
  150. )), $options);
  151. } else {
  152. try {
  153. StreamContextFactory::getContext('http://example.org');
  154. $this->fail();
  155. } catch (\RuntimeException $e) {
  156. $this->assertInstanceOf('RuntimeException', $e);
  157. }
  158. }
  159. }
  160. public function dataSSLProxy()
  161. {
  162. return array(
  163. array('ssl://proxyserver:443', 'https://proxyserver/'),
  164. array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
  165. );
  166. }
  167. public function testEnsureThatfixHttpHeaderFieldMovesContentTypeToEndOfOptions()
  168. {
  169. $options = array(
  170. 'http' => array(
  171. 'header' => "X-Foo: bar\r\nContent-Type: application/json\r\nAuthorization: Basic aW52YWxpZA=="
  172. )
  173. );
  174. $expectedOptions = array(
  175. 'http' => array(
  176. 'header' => array(
  177. "X-Foo: bar",
  178. "Authorization: Basic aW52YWxpZA==",
  179. "Content-Type: application/json"
  180. )
  181. )
  182. );
  183. $context = StreamContextFactory::getContext('http://example.org', $options);
  184. $ctxoptions = stream_context_get_options($context);
  185. $this->assertEquals(end($ctxoptions['http']['header']), end($expectedOptions['http']['header']));
  186. }
  187. }