StreamContextFactoryTest.php 9.1 KB

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