StreamContextFactoryTest.php 8.3 KB

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