StreamContextFactoryTest.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. $expected = array(
  114. 'http' => array(
  115. 'proxy' => 'tcp://proxyserver.net:80',
  116. 'request_fulluri' => true,
  117. 'method' => 'GET',
  118. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password')),
  119. 'max_redirects' => 20,
  120. 'follow_location' => 1,
  121. ), 'ssl' => array(
  122. 'SNI_enabled' => true,
  123. 'SNI_server_name' => 'example.org'
  124. )
  125. );
  126. if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
  127. unset($expected['ssl']['SNI_server_name']);
  128. }
  129. $this->assertEquals($expected, $options);
  130. }
  131. public function testHttpsProxyOverride()
  132. {
  133. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  134. $_SERVER['https_proxy'] = 'https://woopproxy.net';
  135. $context = StreamContextFactory::getContext('https://example.org', array('http' => array('method' => 'GET')));
  136. $options = stream_context_get_options($context);
  137. $expected = array(
  138. 'http' => array(
  139. 'proxy' => 'ssl://woopproxy.net:443',
  140. 'request_fulluri' => true,
  141. 'method' => 'GET',
  142. 'max_redirects' => 20,
  143. 'follow_location' => 1,
  144. ), 'ssl' => array(
  145. 'SNI_enabled' => true,
  146. 'SNI_server_name' => 'example.org'
  147. )
  148. );
  149. if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
  150. unset($expected['ssl']['SNI_server_name']);
  151. }
  152. $this->assertEquals($expected, $options);
  153. }
  154. /**
  155. * @dataProvider dataSSLProxy
  156. */
  157. public function testSSLProxy($expected, $proxy)
  158. {
  159. $_SERVER['http_proxy'] = $proxy;
  160. if (extension_loaded('openssl')) {
  161. $context = StreamContextFactory::getContext('http://example.org');
  162. $options = stream_context_get_options($context);
  163. $this->assertEquals(array('http' => array(
  164. 'proxy' => $expected,
  165. 'request_fulluri' => true,
  166. 'max_redirects' => 20,
  167. 'follow_location' => 1,
  168. )), $options);
  169. } else {
  170. try {
  171. StreamContextFactory::getContext('http://example.org');
  172. $this->fail();
  173. } catch (\RuntimeException $e) {
  174. $this->assertInstanceOf('RuntimeException', $e);
  175. }
  176. }
  177. }
  178. public function dataSSLProxy()
  179. {
  180. return array(
  181. array('ssl://proxyserver:443', 'https://proxyserver/'),
  182. array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
  183. );
  184. }
  185. public function testEnsureThatfixHttpHeaderFieldMovesContentTypeToEndOfOptions()
  186. {
  187. $options = array(
  188. 'http' => array(
  189. 'header' => "X-Foo: bar\r\nContent-Type: application/json\r\nAuthorization: Basic aW52YWxpZA=="
  190. )
  191. );
  192. $expectedOptions = array(
  193. 'http' => array(
  194. 'header' => array(
  195. "X-Foo: bar",
  196. "Authorization: Basic aW52YWxpZA==",
  197. "Content-Type: application/json"
  198. )
  199. )
  200. );
  201. $context = StreamContextFactory::getContext('http://example.org', $options);
  202. $ctxoptions = stream_context_get_options($context);
  203. $this->assertEquals(end($ctxoptions['http']['header']), end($expectedOptions['http']['header']));
  204. }
  205. }