StreamContextFactoryTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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['no_proxy']);
  20. }
  21. protected function tearDown()
  22. {
  23. unset($_SERVER['HTTP_PROXY']);
  24. unset($_SERVER['http_proxy']);
  25. unset($_SERVER['no_proxy']);
  26. }
  27. /**
  28. * @dataProvider dataGetContext
  29. */
  30. public function testGetContext($expectedOptions, $defaultOptions, $expectedParams, $defaultParams)
  31. {
  32. $context = StreamContextFactory::getContext('http://example.org', $defaultOptions, $defaultParams);
  33. $options = stream_context_get_options($context);
  34. $params = stream_context_get_params($context);
  35. $this->assertEquals($expectedOptions, $options);
  36. $this->assertEquals($expectedParams, $params);
  37. }
  38. public function dataGetContext()
  39. {
  40. return array(
  41. array(
  42. $a = array('http' => array('follow_location' => 1, 'max_redirects' => 20)), array(),
  43. array('options' => $a), array()
  44. ),
  45. array(
  46. $a = array('http' => array('method' => 'GET', 'max_redirects' => 20, 'follow_location' => 1)), array('http' => array('method' => 'GET')),
  47. array('options' => $a, 'notification' => $f = function () {}), array('notification' => $f)
  48. ),
  49. );
  50. }
  51. public function testHttpProxy()
  52. {
  53. $_SERVER['http_proxy'] = 'http://username:p%40ssword@proxyserver.net:3128/';
  54. $_SERVER['HTTP_PROXY'] = 'http://proxyserver/';
  55. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  56. $options = stream_context_get_options($context);
  57. $this->assertEquals(array('http' => array(
  58. 'proxy' => 'tcp://proxyserver.net:3128',
  59. 'request_fulluri' => true,
  60. 'method' => 'GET',
  61. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:p@ssword')),
  62. 'max_redirects' => 20,
  63. 'follow_location' => 1,
  64. )), $options);
  65. }
  66. public function testHttpProxyWithNoProxy()
  67. {
  68. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  69. $_SERVER['no_proxy'] = 'foo,example.org';
  70. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  71. $options = stream_context_get_options($context);
  72. $this->assertEquals(array('http' => array(
  73. 'method' => 'GET',
  74. 'max_redirects' => 20,
  75. 'follow_location' => 1,
  76. )), $options);
  77. }
  78. public function testHttpProxyWithNoProxyWildcard()
  79. {
  80. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  81. $_SERVER['no_proxy'] = '*';
  82. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  83. $options = stream_context_get_options($context);
  84. $this->assertEquals(array('http' => array(
  85. 'method' => 'GET',
  86. 'max_redirects' => 20,
  87. 'follow_location' => 1,
  88. )), $options);
  89. }
  90. public function testOptionsArePreserved()
  91. {
  92. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net:3128/';
  93. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET', 'header' => array("X-Foo: bar"), 'request_fulluri' => false)));
  94. $options = stream_context_get_options($context);
  95. $this->assertEquals(array('http' => array(
  96. 'proxy' => 'tcp://proxyserver.net:3128',
  97. 'request_fulluri' => false,
  98. 'method' => 'GET',
  99. 'header' => array("X-Foo: bar", "Proxy-Authorization: Basic " . base64_encode('username:password')),
  100. 'max_redirects' => 20,
  101. 'follow_location' => 1,
  102. )), $options);
  103. }
  104. public function testHttpProxyWithoutPort()
  105. {
  106. $_SERVER['http_proxy'] = 'http://username:password@proxyserver.net';
  107. $context = StreamContextFactory::getContext('http://example.org', array('http' => array('method' => 'GET')));
  108. $options = stream_context_get_options($context);
  109. $this->assertEquals(array('http' => array(
  110. 'proxy' => 'tcp://proxyserver.net:80',
  111. 'request_fulluri' => true,
  112. 'method' => 'GET',
  113. 'header' => array("Proxy-Authorization: Basic " . base64_encode('username:password')),
  114. 'max_redirects' => 20,
  115. 'follow_location' => 1,
  116. )), $options);
  117. }
  118. /**
  119. * @dataProvider dataSSLProxy
  120. */
  121. public function testSSLProxy($expected, $proxy)
  122. {
  123. $_SERVER['http_proxy'] = $proxy;
  124. if (extension_loaded('openssl')) {
  125. $context = StreamContextFactory::getContext('http://example.org');
  126. $options = stream_context_get_options($context);
  127. $this->assertEquals(array('http' => array(
  128. 'proxy' => $expected,
  129. 'request_fulluri' => true,
  130. 'max_redirects' => 20,
  131. 'follow_location' => 1,
  132. )), $options);
  133. } else {
  134. try {
  135. StreamContextFactory::getContext('http://example.org');
  136. $this->fail();
  137. } catch (\RuntimeException $e) {
  138. $this->assertInstanceOf('RuntimeException', $e);
  139. }
  140. }
  141. }
  142. public function dataSSLProxy()
  143. {
  144. return array(
  145. array('ssl://proxyserver:443', 'https://proxyserver/'),
  146. array('ssl://proxyserver:8443', 'https://proxyserver:8443'),
  147. );
  148. }
  149. public function testEnsureThatfixHttpHeaderFieldMovesContentTypeToEndOfOptions()
  150. {
  151. $options = array(
  152. 'http' => array(
  153. 'header' => "X-Foo: bar\r\nContent-Type: application/json\r\nAuthorization: Basic aW52YWxpZA=="
  154. )
  155. );
  156. $expectedOptions = array(
  157. 'http' => array(
  158. 'header' => array(
  159. "X-Foo: bar",
  160. "Authorization: Basic aW52YWxpZA==",
  161. "Content-Type: application/json"
  162. )
  163. )
  164. );
  165. $context = StreamContextFactory::getContext('http://example.org', $options);
  166. $ctxoptions = stream_context_get_options($context);
  167. $this->assertEquals(end($ctxoptions['http']['header']), end($expectedOptions['http']['header']));
  168. }
  169. }