NoProxyPatternTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\NoProxyPattern;
  13. use Composer\Test\TestCase;
  14. class NoProxyPatternTest extends TestCase
  15. {
  16. /**
  17. * @dataProvider dataHostName
  18. */
  19. public function testHostName($noproxy, $url, $expected)
  20. {
  21. $matcher = new NoProxyPattern($noproxy);
  22. $url = $this->getUrl($url);
  23. $this->assertEquals($expected, $matcher->test($url));
  24. }
  25. public function dataHostName()
  26. {
  27. $noproxy = 'foobar.com, .barbaz.net';
  28. // noproxy, url, expected
  29. return array(
  30. 'match as foobar.com' => array($noproxy, 'foobar.com', true),
  31. 'match foobar.com' => array($noproxy, 'www.foobar.com', true),
  32. 'no match foobar.com' => array($noproxy, 'foofoobar.com', false),
  33. 'match .barbaz.net 1' => array($noproxy, 'barbaz.net', true),
  34. 'match .barbaz.net 2' => array($noproxy, 'www.barbaz.net', true),
  35. 'no match .barbaz.net' => array($noproxy, 'barbarbaz.net', false),
  36. 'no match wrong domain' => array($noproxy, 'barbaz.com', false),
  37. 'no match FQDN' => array($noproxy, 'foobar.com.', false),
  38. );
  39. }
  40. /**
  41. * @dataProvider dataIpAddress
  42. */
  43. public function testIpAddress($noproxy, $url, $expected)
  44. {
  45. $matcher = new NoProxyPattern($noproxy);
  46. $url = $this->getUrl($url);
  47. $this->assertEquals($expected, $matcher->test($url));
  48. }
  49. public function dataIpAddress()
  50. {
  51. $noproxy = '192.168.1.1, 2001:db8::52:0:1';
  52. // noproxy, url, expected
  53. return array(
  54. 'match exact IPv4' => array($noproxy, '192.168.1.1', true),
  55. 'no match IPv4' => array($noproxy, '192.168.1.4', false),
  56. 'match exact IPv6' => array($noproxy, '[2001:db8:0:0:0:52:0:1]', true),
  57. 'no match IPv6' => array($noproxy, '[2001:db8:0:0:0:52:0:2]', false),
  58. 'match mapped IPv4' => array($noproxy, '[::FFFF:C0A8:0101]', true),
  59. 'no match mapped IPv4' => array($noproxy, '[::FFFF:C0A8:0104]', false),
  60. );
  61. }
  62. /**
  63. * @dataProvider dataIpRange
  64. */
  65. public function testIpRange($noproxy, $url, $expected)
  66. {
  67. $matcher = new NoProxyPattern($noproxy);
  68. $url = $this->getUrl($url);
  69. $this->assertEquals($expected, $matcher->test($url));
  70. }
  71. public function dataIpRange()
  72. {
  73. $noproxy = '10.0.0.0/30, 2002:db8:a::45/121';
  74. // noproxy, url, expected
  75. return array(
  76. 'match IPv4/CIDR' => array($noproxy, '10.0.0.2', true),
  77. 'no match IPv4/CIDR' => array($noproxy, '10.0.0.4', false),
  78. 'match IPv6/CIDR' => array($noproxy, '[2002:db8:a:0:0:0:0:7f]', true),
  79. 'no match IPv6' => array($noproxy, '[2002:db8:a:0:0:0:0:ff]', false),
  80. 'match mapped IPv4' => array($noproxy, '[::FFFF:0A00:0002]', true),
  81. 'no match mapped IPv4' => array($noproxy, '[::FFFF:0A00:0004]', false),
  82. );
  83. }
  84. /**
  85. * @dataProvider dataPort
  86. */
  87. public function testPort($noproxy, $url, $expected)
  88. {
  89. $matcher = new NoProxyPattern($noproxy);
  90. $url = $this->getUrl($url);
  91. $this->assertEquals($expected, $matcher->test($url));
  92. }
  93. public function dataPort()
  94. {
  95. $noproxy = '192.168.1.2:81, 192.168.1.3:80, [2001:db8::52:0:2]:443, [2001:db8::52:0:3]:80';
  96. // noproxy, url, expected
  97. return array(
  98. 'match IPv4 port' => array($noproxy, '192.168.1.3', true),
  99. 'no match IPv4 port' => array($noproxy, '192.168.1.2', false),
  100. 'match IPv6 port' => array($noproxy, '[2001:db8::52:0:3]', true),
  101. 'no match IPv6 port' => array($noproxy, '[2001:db8::52:0:2]', false),
  102. );
  103. }
  104. /**
  105. * Appends a scheme to the test url if it is missing
  106. *
  107. * @param string $url
  108. */
  109. private function getUrl($url)
  110. {
  111. if (parse_url($url, PHP_URL_SCHEME)) {
  112. return $url;
  113. }
  114. $scheme = 'http';
  115. if (strpos($url, '[') !== 0 && strrpos($url, ':') !== false) {
  116. list(, $port) = explode(':', $url);
  117. if ($port === '443') {
  118. $scheme = 'https';
  119. }
  120. }
  121. return sprintf('%s://%s', $scheme, $url);
  122. }
  123. }