6 require_once 'application/Url.php';
9 * Unitary tests for URL utilities
11 class UrlTest
extends PHPUnit_Framework_TestCase
14 protected static $baseUrl = 'http://domain.tld:3000';
19 private function assertUrlIsCleaned($query='', $fragment='')
21 $url = new Url(self
::$baseUrl.$query.$fragment);
23 $this->assertEquals(self
::$baseUrl, $url->toString());
27 * Instantiate an empty URL
29 public function testEmptyConstruct()
32 $this->assertEquals('', $url->toString());
38 public function testConstruct()
40 $ref = 'http://username:password@hostname:9090/path'
41 .'?arg1=value1&arg2=value2#anchor';
43 $this->assertEquals($ref, $url->toString());
47 * URL cleanup - nothing to do
49 public function testNoCleanup()
51 // URL with no query nor fragment
52 $this->assertUrlIsCleaned();
54 // URL with no annoying elements
55 $ref = self
::$baseUrl.'?p1=val1&p2=1234#edit';
57 $this->assertEquals($ref, $url->cleanup());
61 * URL cleanup - annoying fragment
63 public function testCleanupFragment()
65 $this->assertUrlIsCleaned('', '#tk.rss_all');
66 $this->assertUrlIsCleaned('', '#xtor=RSS-');
67 $this->assertUrlIsCleaned('', '#xtor=RSS-U3ht0tkc4b');
71 * URL cleanup - single annoying query parameter
73 public function testCleanupSingleQueryParam()
75 $this->assertUrlIsCleaned('?action_object_map=junk');
76 $this->assertUrlIsCleaned('?action_ref_map=Cr4p!');
77 $this->assertUrlIsCleaned('?action_type_map=g4R84g3');
79 $this->assertUrlIsCleaned('?fb_stuff=v41u3');
80 $this->assertUrlIsCleaned('?fb=71m3w4573');
82 $this->assertUrlIsCleaned('?utm_campaign=zomg');
83 $this->assertUrlIsCleaned('?utm_medium=numnum');
84 $this->assertUrlIsCleaned('?utm_source=c0d3');
85 $this->assertUrlIsCleaned('?utm_term=1n4l');
87 $this->assertUrlIsCleaned('?xtor=some-url');
91 * URL cleanup - multiple annoying query parameters
93 public function testCleanupMultipleQueryParams()
95 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng');
96 $this->assertUrlIsCleaned(
97 '?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
102 * URL cleanup - multiple annoying query parameters, annoying fragment
104 public function testCleanupMultipleQueryParamsAndFragment()
106 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng', '#tk.rss_all');
110 * Nominal case - the URL contains both useful and annoying parameters
112 public function testCleanupMixedContent()
114 // ditch annoying query params and fragment, keep useful params
117 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
119 $this->assertEquals(self
::$baseUrl.'?my=stuff&is=kept', $url->cleanup());
122 // ditch annoying query params, keep useful params and fragment
125 .'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
128 self
::$baseUrl.'?my=stuff&is=kept#again',
132 // test firefox reader url
134 'about://reader?url=' . urlencode(self
::$baseUrl .'?my=stuff&is=kept')
136 $this->assertEquals(self
::$baseUrl.'?my=stuff&is=kept', $url->cleanup());
141 * Test default http scheme.
143 public function testDefaultScheme() {
144 $url = new Url(self
::$baseUrl);
145 $this->assertEquals('http', $url->getScheme());
146 $url = new Url('domain.tld');
147 $this->assertEquals('http', $url->getScheme());
148 $url = new Url('ssh://domain.tld');
149 $this->assertEquals('ssh', $url->getScheme());
150 $url = new Url('ftp://domain.tld');
151 $this->assertEquals('ftp', $url->getScheme());
152 $url = new Url('git://domain.tld/push?pull=clone#checkout');
153 $this->assertEquals('git', $url->getScheme());
157 * Test add trailing slash.
159 function testAddTrailingSlash()
161 $strOn = 'http://randomstr.com/test/';
162 $strOff = 'http://randomstr.com/test';
163 $this->assertEquals($strOn, add_trailing_slash($strOn));
164 $this->assertEquals($strOn, add_trailing_slash($strOff));
168 * Test valid HTTP url.
170 function testUrlIsHttp()
172 $url = new Url(self
::$baseUrl);
173 $this->assertTrue($url->isHttp());
179 function testUrlIsNotHttp()
181 $url = new Url('ftp://save.tld/mysave');
182 $this->assertFalse($url->isHttp());
188 function testIndToAscii()
190 $ind = 'http://www.académie-française.fr/';
191 $expected = 'http://www.xn--acadmie-franaise-npb1a.fr/';
192 $url = new Url($ind);
193 $this->assertEquals($expected, $url->idnToAscii());
195 $notInd = 'http://www.academie-francaise.fr/';
196 $url = new Url($notInd);
197 $this->assertEquals($notInd, $url->idnToAscii());