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.

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