UrlTest.php 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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\Url;
  13. use Composer\Test\TestCase;
  14. use Composer\Config;
  15. class UrlTest extends TestCase
  16. {
  17. /**
  18. * @dataProvider distRefsProvider
  19. */
  20. public function testUpdateDistReference($url, $expectedUrl, $conf = array(), $ref = 'newref')
  21. {
  22. $config = new Config();
  23. $config->merge(array('config' => $conf));
  24. $this->assertSame($expectedUrl, Url::updateDistReference($config, $url, $ref));
  25. }
  26. public static function distRefsProvider()
  27. {
  28. return array(
  29. // github
  30. array('https://github.com/foo/bar/zipball/abcd', 'https://api.github.com/repos/foo/bar/zipball/newref'),
  31. array('https://www.github.com/foo/bar/zipball/abcd', 'https://api.github.com/repos/foo/bar/zipball/newref'),
  32. array('https://github.com/foo/bar/archive/abcd.zip', 'https://api.github.com/repos/foo/bar/zipball/newref'),
  33. array('https://github.com/foo/bar/archive/abcd.tar.gz', 'https://api.github.com/repos/foo/bar/tarball/newref'),
  34. array('https://api.github.com/repos/foo/bar/tarball', 'https://api.github.com/repos/foo/bar/tarball/newref'),
  35. array('https://api.github.com/repos/foo/bar/tarball/abcd', 'https://api.github.com/repos/foo/bar/tarball/newref'),
  36. // github enterprise
  37. array('https://mygithub.com/api/v3/repos/foo/bar/tarball/abcd', 'https://mygithub.com/api/v3/repos/foo/bar/tarball/newref', array('github-domains' => array('mygithub.com'))),
  38. // bitbucket
  39. array('https://bitbucket.org/foo/bar/get/abcd.zip', 'https://bitbucket.org/foo/bar/get/newref.zip'),
  40. array('https://www.bitbucket.org/foo/bar/get/abcd.tar.bz2', 'https://bitbucket.org/foo/bar/get/newref.tar.bz2'),
  41. // gitlab
  42. array('https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=abcd', 'https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=newref'),
  43. array('https://www.gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=abcd', 'https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.zip?sha=newref'),
  44. array('https://gitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.gz?sha=abcd', 'https://gitlab.com/api/v4/projects/foo%2Fbar/repository/archive.tar.gz?sha=newref'),
  45. // gitlab enterprise
  46. array('https://mygitlab.com/api/v4/projects/foo%2Fbar/repository/archive.tar.gz?sha=abcd', 'https://mygitlab.com/api/v4/projects/foo%2Fbar/repository/archive.tar.gz?sha=newref', array('gitlab-domains' => array('mygitlab.com'))),
  47. array('https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=abcd', 'https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=newref', array('gitlab-domains' => array('mygitlab.com'))),
  48. array('https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=abcd', 'https://mygitlab.com/api/v3/projects/foo%2Fbar/repository/archive.tar.bz2?sha=65', array('gitlab-domains' => array('mygitlab.com')), '65'),
  49. );
  50. }
  51. /**
  52. * @dataProvider sanitizeProvider
  53. */
  54. public function testSanitize($expected, $url)
  55. {
  56. $this->assertSame($expected, Url::sanitize($url));
  57. }
  58. public static function sanitizeProvider()
  59. {
  60. return array(
  61. array('https://foo:***@example.org/', 'https://foo:bar@example.org/'),
  62. array('https://foo@example.org/', 'https://foo@example.org/'),
  63. array('https://example.org/', 'https://example.org/'),
  64. array('http://***:***@example.org', 'http://10a8f08e8d7b7b9:foo@example.org'),
  65. array('https://foo:***@example.org:123/', 'https://foo:bar@example.org:123/'),
  66. array('https://example.org/foo/bar?access_token=***', 'https://example.org/foo/bar?access_token=abcdef'),
  67. array('https://example.org/foo/bar?foo=bar&access_token=***', 'https://example.org/foo/bar?foo=bar&access_token=abcdef'),
  68. );
  69. }
  70. }