StreamContextFactoryTest.php 9.0 KB

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