StreamContextFactoryTest.php 9.0 KB

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