StreamContextFactoryTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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, 'header' => array('User-Agent: foo'))), array('http' => array('header' => 'User-Agent: foo')),
  47. array('options' => $a), array(),
  48. ),
  49. array(
  50. $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')),
  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', 'header' => 'User-Agent: foo')));
  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('User-Agent: foo', "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', 'header' => 'User-Agent: foo')));
  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. 'header' => array('User-Agent: foo'),
  81. )), $options);
  82. }
  83. public function testHttpProxyWithNoProxyWildcard()
  84. {
  85. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  86. $_SERVER['no_proxy'] = '*';
  87. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET', 'header' => 'User-Agent: foo')));
  88. $options = stream_context_get_options($context);
  89. $this->assertEquals(array('http' => array(
  90. 'method' => 'GET',
  91. 'max_redirects' => 20,
  92. 'follow_location' => 1,
  93. 'header' => array('User-Agent: foo'),
  94. )), $options);
  95. }
  96. public function testOptionsArePreserved()
  97. {
  98. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  99. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET', 'header' => array('User-Agent: foo', "X-Foo: bar"), 'request_fulluri' => false)));
  100. $options = stream_context_get_options($context);
  101. $this->assertEquals(array('http' => array(
  102. 'proxy' => 'tcp://proxyserver.net:3128',
  103. 'request_fulluri' => false,
  104. 'method' => 'GET',
  105. 'header' => array('User-Agent: foo', "X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password')),
  106. 'max_redirects' => 20,
  107. 'follow_location' => 1,
  108. )), $options);
  109. }
  110. public function testHttpProxyWithoutPort()
  111. {
  112. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  113. $context = StreamContextFactory::getContext('https://example.org', array('http' => array('method' => 'GET', 'header' => 'User-Agent: foo')));
  114. $options = stream_context_get_options($context);
  115. $expected = array(
  116. 'http' => array(
  117. 'proxy' => 'tcp://proxyserver.net:80',
  118. 'request_fulluri' => true,
  119. 'method' => 'GET',
  120. 'header' => array('User-Agent: foo', "Proxy-Authorization: Basic " . base64_encode('username:password')),
  121. 'max_redirects' => 20,
  122. 'follow_location' => 1,
  123. ), 'ssl' => array(
  124. 'SNI_enabled' => true,
  125. 'SNI_server_name' => 'example.org',
  126. ),
  127. );
  128. if (PHP_VERSION_ID >= 50600) {
  129. unset($expected['ssl']['SNI_server_name']);
  130. }
  131. $this->assertEquals($expected, $options);
  132. }
  133. public function testHttpsProxyOverride()
  134. {
  135. if (!extension_loaded('openssl')) {
  136. $this->markTestSkipped('Requires openssl');
  137. }
  138. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  139. $_SERVER['https_proxy'] = 'https://woopproxy.net';
  140. $context = StreamContextFactory::getContext('https://example.org', array('http' => array('method' => 'GET', 'header' => 'User-Agent: foo')));
  141. $options = stream_context_get_options($context);
  142. $expected = array(
  143. 'http' => array(
  144. 'proxy' => 'ssl://woopproxy.net:443',
  145. 'request_fulluri' => true,
  146. 'method' => 'GET',
  147. 'max_redirects' => 20,
  148. 'follow_location' => 1,
  149. 'header' => array('User-Agent: foo'),
  150. ), 'ssl' => array(
  151. 'SNI_enabled' => true,
  152. 'SNI_server_name' => 'example.org',
  153. ),
  154. );
  155. if (PHP_VERSION_ID >= 50600) {
  156. unset($expected['ssl']['SNI_server_name']);
  157. }
  158. $this->assertEquals($expected, $options);
  159. }
  160. /**
  161. * @dataProvider dataSSLProxy
  162. */
  163. public function testSSLProxy($expected, $proxy)
  164. {
  165. $_SERVER['http_proxy'] = $proxy;
  166. if (extension_loaded('openssl')) {
  167. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('header' => 'User-Agent: foo')));
  168. $options = stream_context_get_options($context);
  169. $this->assertEquals(array('http' => array(
  170. 'proxy' => $expected,
  171. 'request_fulluri' => true,
  172. 'max_redirects' => 20,
  173. 'follow_location' => 1,
  174. 'header' => array('User-Agent: foo'),
  175. )), $options);
  176. } else {
  177. try {
  178. StreamContextFactory::getContext('http://example.org');
  179. $this->fail();
  180. } catch (\RuntimeException $e) {
  181. $this->assertInstanceOf('RuntimeException', $e);
  182. }
  183. }
  184. }
  185. public function dataSSLProxy()
  186. {
  187. return array(
  188. array('ssl://proxyserver:443', 'https://proxyserver/'),
  189. array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
  190. );
  191. }
  192. public function testEnsureThatfixHttpHeaderFieldMovesContentTypeToEndOfOptions()
  193. {
  194. $options = array(
  195. 'http' => array(
  196. 'header' => "User-agent: foo\r\nX-Foo: bar\r\nContent-Type: application/json\r\nAuthorization: Basic aW52YWxpZA==",
  197. ),
  198. );
  199. $expectedOptions = array(
  200. 'http' => array(
  201. 'header' => array(
  202. "User-agent: foo",
  203. "X-Foo: bar",
  204. "Authorization: Basic aW52YWxpZA==",
  205. "Content-Type: application/json",
  206. ),
  207. ),
  208. );
  209. $context = StreamContextFactory::getContext('http://example.org', $options);
  210. $ctxoptions = stream_context_get_options($context);
  211. $this->assertEquals(end($expectedOptions['http']['header']), end($ctxoptions['http']['header']));
  212. }
  213. }