You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
5.9 KiB

  1. <?php
  2. class URLTest extends PHPUnit_Framework_TestCase {
  3. public function testDisplayURL() {
  4. $url = p3k\url\display_url('http://example.com');
  5. $this->assertEquals('example.com', $url);
  6. $url = p3k\url\display_url('http://example.com/');
  7. $this->assertEquals('example.com', $url);
  8. $url = p3k\url\display_url('example.com');
  9. $this->assertEquals('example.com', $url);
  10. $url = p3k\url\display_url('http://example.com/foo');
  11. $this->assertEquals('example.com/foo', $url);
  12. $url = p3k\url\display_url('http://example.com/foo/');
  13. $this->assertEquals('example.com/foo/', $url);
  14. $url = p3k\url\display_url('http://www.example.com/foo/');
  15. $this->assertEquals('example.com/foo/', $url);
  16. $url = p3k\url\display_url('https://www.example.com/foo/');
  17. $this->assertEquals('example.com/foo/', $url);
  18. $url = p3k\url\display_url('https://example.www.example.com/foo/');
  19. $this->assertEquals('example.www.example.com/foo/', $url);
  20. }
  21. public function testAddQueryParamsToURLNoExistingParams() {
  22. $url = p3k\url\add_query_params_to_url('http://example.com', ['q'=>1]);
  23. $this->assertEquals('http://example.com?q=1', $url);
  24. $url = p3k\url\add_query_params_to_url('http://example.com/', ['q'=>1]);
  25. $this->assertEquals('http://example.com/?q=1', $url);
  26. $url = p3k\url\add_query_params_to_url('http://example.com/foo', ['q'=>1]);
  27. $this->assertEquals('http://example.com/foo?q=1', $url);
  28. $url = p3k\url\add_query_params_to_url('http://example.com/foo#fragment', ['q'=>1]);
  29. $this->assertEquals('http://example.com/foo?q=1#fragment', $url);
  30. }
  31. public function testAddQueryParamsToURLWithExistingParams() {
  32. $url = p3k\url\add_query_params_to_url('http://example.com?a=b', ['q'=>1]);
  33. $this->assertEquals('http://example.com?a=b&q=1', $url);
  34. $url = p3k\url\add_query_params_to_url('http://example.com/?a=b', ['q'=>1]);
  35. $this->assertEquals('http://example.com/?a=b&q=1', $url);
  36. $url = p3k\url\add_query_params_to_url('http://example.com/foo?a=b', ['q'=>1]);
  37. $this->assertEquals('http://example.com/foo?a=b&q=1', $url);
  38. $url = p3k\url\add_query_params_to_url('http://example.com/foo?a=b#fragment', ['q'=>1]);
  39. $this->assertEquals('http://example.com/foo?a=b&q=1#fragment', $url);
  40. }
  41. public function testStripTrackingParams() {
  42. $url = p3k\url\strip_tracking_params('http://example.com/');
  43. $this->assertEquals('http://example.com/', $url);
  44. $url = p3k\url\strip_tracking_params('http://example.com/?utm_source=foo');
  45. $this->assertEquals('http://example.com/', $url);
  46. $url = p3k\url\strip_tracking_params('http://example.com/?foo=bar');
  47. $this->assertEquals('http://example.com/?foo=bar', $url);
  48. $url = p3k\url\strip_tracking_params('http://example.com/?foo=bar&utm_source=froogle');
  49. $this->assertEquals('http://example.com/?foo=bar', $url);
  50. }
  51. public function testNormalizeURL() {
  52. $url = p3k\url\normalize('http://example.com/');
  53. $this->assertEquals('http://example.com/', $url);
  54. $url = p3k\url\normalize('http://example.com');
  55. $this->assertEquals('http://example.com/', $url);
  56. $url = p3k\url\normalize('example.com');
  57. $this->assertEquals('http://example.com/', $url);
  58. $url = p3k\url\normalize('mailto:user@example.com');
  59. $this->assertEquals(false, $url);
  60. }
  61. public function testBuildURL() {
  62. $parts = p3k\url\build_url(parse_url('http://example.com'));
  63. $this->assertEquals('http://example.com', $parts);
  64. $parts = p3k\url\build_url(parse_url('http://example.com/'));
  65. $this->assertEquals('http://example.com/', $parts);
  66. $parts = p3k\url\build_url(parse_url('https://example.com/?'));
  67. $this->assertEquals('https://example.com/', $parts);
  68. $parts = p3k\url\build_url(parse_url('https://example.com/?foo=bar'));
  69. $this->assertEquals('https://example.com/?foo=bar', $parts);
  70. $parts = p3k\url\build_url(parse_url('https://example.com?foo=bar'));
  71. $this->assertEquals('https://example.com?foo=bar', $parts);
  72. $parts = p3k\url\build_url(parse_url('https://user:pass@example.com/?foo=bar'));
  73. $this->assertEquals('https://user:pass@example.com/?foo=bar', $parts);
  74. $parts = p3k\url\build_url(parse_url('https://user:pass@example.com:3000/?foo=bar#f'));
  75. $this->assertEquals('https://user:pass@example.com:3000/?foo=bar#f', $parts);
  76. $parts = p3k\url\build_url(parse_url('https://user@example.com/?foo=bar'));
  77. $this->assertEquals('https://user@example.com/?foo=bar', $parts);
  78. $parts = p3k\url\build_url(parse_url('https://user:@example.com/?foo=bar'));
  79. $this->assertEquals('https://user@example.com/?foo=bar', $parts);
  80. }
  81. public function testHostMatches() {
  82. $this->assertTrue(p3k\url\host_matches('http://example.com/', 'https://example.com/foo'));
  83. $this->assertFalse(p3k\url\host_matches('http://example.com/', 'https://subdomain.example.com/foo'));
  84. }
  85. public function testIsURL() {
  86. $this->assertTrue(p3k\url\is_url('http://example.com/'));
  87. $this->assertTrue(p3k\url\is_url('http://example'));
  88. $this->assertTrue(p3k\url\is_url('https://example.com/foo?a=b#f'));
  89. $this->assertFalse(p3k\url\is_url('mailto:user@example.com'));
  90. $this->assertFalse(p3k\url\is_url('geo:45.5,-122.6'));
  91. }
  92. public function testIsPublicIP() {
  93. $this->assertTrue(p3k\url\is_public_ip('45.1.200.42'));
  94. $this->assertFalse(p3k\url\is_public_ip('192.168.200.1'));
  95. $this->assertFalse(p3k\url\is_public_ip('127.0.0.1'));
  96. $this->assertFalse(p3k\url\is_public_ip('0.10.0.0'));
  97. $this->assertFalse(p3k\url\is_public_ip('10.10.0.0'));
  98. }
  99. public function testGeoToLatLng() {
  100. $coords = p3k\url\geo_to_latlng('geo:45.521296,-122.626412');
  101. $this->assertEquals(['latitude'=>45.521296, 'longitude'=>-122.626412], $coords);
  102. $coords = p3k\url\geo_to_latlng('geo:45.521296,-122.626412;u=35');
  103. $this->assertEquals(['latitude'=>45.521296, 'longitude'=>-122.626412], $coords);
  104. $coords = p3k\url\geo_to_latlng('http://example.com/');
  105. $this->assertEquals(false, $coords);
  106. }
  107. }