UrlTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * Url's tests
  4. */
  5. require_once 'application/Url.php';
  6. /**
  7. * Unitary tests for URL utilities
  8. */
  9. class UrlTest extends PHPUnit_Framework_TestCase
  10. {
  11. // base URL for tests
  12. protected static $baseUrl = 'http://domain.tld:3000';
  13. /**
  14. * Helper method
  15. */
  16. private function assertUrlIsCleaned($query='', $fragment='')
  17. {
  18. $url = new Url(self::$baseUrl.$query.$fragment);
  19. $url->cleanup();
  20. $this->assertEquals(self::$baseUrl, $url->toString());
  21. }
  22. /**
  23. * Instantiate an empty URL
  24. */
  25. public function testEmptyConstruct()
  26. {
  27. $url = new Url('');
  28. $this->assertEquals('', $url->toString());
  29. }
  30. /**
  31. * Instantiate a URL
  32. */
  33. public function testConstruct()
  34. {
  35. $ref = 'http://username:password@hostname:9090/path'
  36. .'?arg1=value1&arg2=value2#anchor';
  37. $url = new Url($ref);
  38. $this->assertEquals($ref, $url->toString());
  39. }
  40. /**
  41. * URL cleanup - nothing to do
  42. */
  43. public function testNoCleanup()
  44. {
  45. // URL with no query nor fragment
  46. $this->assertUrlIsCleaned();
  47. // URL with no annoying elements
  48. $ref = self::$baseUrl.'?p1=val1&p2=1234#edit';
  49. $url = new Url($ref);
  50. $this->assertEquals($ref, $url->cleanup());
  51. }
  52. /**
  53. * URL cleanup - annoying fragment
  54. */
  55. public function testCleanupFragment()
  56. {
  57. $this->assertUrlIsCleaned('', '#tk.rss_all');
  58. $this->assertUrlIsCleaned('', '#xtor=RSS-');
  59. $this->assertUrlIsCleaned('', '#xtor=RSS-U3ht0tkc4b');
  60. }
  61. /**
  62. * URL cleanup - single annoying query parameter
  63. */
  64. public function testCleanupSingleQueryParam()
  65. {
  66. $this->assertUrlIsCleaned('?action_object_map=junk');
  67. $this->assertUrlIsCleaned('?action_ref_map=Cr4p!');
  68. $this->assertUrlIsCleaned('?action_type_map=g4R84g3');
  69. $this->assertUrlIsCleaned('?fb_stuff=v41u3');
  70. $this->assertUrlIsCleaned('?fb=71m3w4573');
  71. $this->assertUrlIsCleaned('?utm_campaign=zomg');
  72. $this->assertUrlIsCleaned('?utm_medium=numnum');
  73. $this->assertUrlIsCleaned('?utm_source=c0d3');
  74. $this->assertUrlIsCleaned('?utm_term=1n4l');
  75. $this->assertUrlIsCleaned('?xtor=some-url');
  76. }
  77. /**
  78. * URL cleanup - multiple annoying query parameters
  79. */
  80. public function testCleanupMultipleQueryParams()
  81. {
  82. $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng');
  83. $this->assertUrlIsCleaned(
  84. '?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
  85. );
  86. }
  87. /**
  88. * URL cleanup - multiple annoying query parameters, annoying fragment
  89. */
  90. public function testCleanupMultipleQueryParamsAndFragment()
  91. {
  92. $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng', '#tk.rss_all');
  93. }
  94. /**
  95. * Nominal case - the URL contains both useful and annoying parameters
  96. */
  97. public function testCleanupMixedContent()
  98. {
  99. // ditch annoying query params and fragment, keep useful params
  100. $url = new Url(
  101. self::$baseUrl
  102. .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
  103. );
  104. $this->assertEquals(self::$baseUrl.'?my=stuff&is=kept', $url->cleanup());
  105. // ditch annoying query params, keep useful params and fragment
  106. $url = new Url(
  107. self::$baseUrl
  108. .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
  109. );
  110. $this->assertEquals(
  111. self::$baseUrl.'?my=stuff&is=kept#again',
  112. $url->cleanup()
  113. );
  114. }
  115. /**
  116. * Test default http scheme.
  117. */
  118. public function testDefaultScheme() {
  119. $url = new Url(self::$baseUrl);
  120. $this->assertEquals('http', $url->getScheme());
  121. $url = new Url('domain.tld');
  122. $this->assertEquals('http', $url->getScheme());
  123. $url = new Url('ssh://domain.tld');
  124. $this->assertEquals('ssh', $url->getScheme());
  125. $url = new Url('ftp://domain.tld');
  126. $this->assertEquals('ftp', $url->getScheme());
  127. $url = new Url('git://domain.tld/push?pull=clone#checkout');
  128. $this->assertEquals('git', $url->getScheme());
  129. }
  130. /**
  131. * Test add trailing slash.
  132. */
  133. function testAddTrailingSlash()
  134. {
  135. $strOn = 'http://randomstr.com/test/';
  136. $strOff = 'http://randomstr.com/test';
  137. $this->assertEquals($strOn, add_trailing_slash($strOn));
  138. $this->assertEquals($strOn, add_trailing_slash($strOff));
  139. }
  140. /**
  141. * Test valid HTTP url.
  142. */
  143. function testUrlIsHttp()
  144. {
  145. $url = new Url(self::$baseUrl);
  146. $this->assertTrue($url->isHttp());
  147. }
  148. /**
  149. * Test non HTTP url.
  150. */
  151. function testUrlIsNotHttp()
  152. {
  153. $url = new Url('ftp://save.tld/mysave');
  154. $this->assertFalse($url->isHttp());
  155. }
  156. }