]> git.immae.eu Git - github/shaarli/Shaarli.git/blame - tests/http/UrlTest.php
namespacing: move HTTP utilities along \Shaarli\Http\ classes
[github/shaarli/Shaarli.git] / tests / http / UrlTest.php
CommitLineData
d9d776af
V
1<?php
2/**
51753e40 3 * UrlUtils's tests
d9d776af
V
4 */
5
fb1b182f
V
6namespace Shaarli\Http;
7
d9d776af 8
d9d776af
V
9/**
10 * Unitary tests for URL utilities
11 */
fb1b182f 12class UrlTest extends \PHPUnit\Framework\TestCase
d9d776af
V
13{
14 // base URL for tests
15 protected static $baseUrl = 'http://domain.tld:3000';
16
17 /**
18 * Helper method
19 */
067c2dd8 20 private function assertUrlIsCleaned($query = '', $fragment = '')
d9d776af 21 {
fb1b182f 22 $url = new Url(self::$baseUrl . $query . $fragment);
d9d776af 23 $url->cleanup();
ef591e7e 24 $this->assertEquals(self::$baseUrl, $url->toString());
d9d776af
V
25 }
26
27 /**
28 * Instantiate an empty URL
29 */
30 public function testEmptyConstruct()
31 {
ef591e7e
GV
32 $url = new Url('');
33 $this->assertEquals('', $url->toString());
d9d776af
V
34 }
35
36 /**
37 * Instantiate a URL
38 */
39 public function testConstruct()
40 {
41 $ref = 'http://username:password@hostname:9090/path'
fb1b182f 42 . '?arg1=value1&arg2=value2#anchor';
ef591e7e
GV
43 $url = new Url($ref);
44 $this->assertEquals($ref, $url->toString());
d9d776af
V
45 }
46
47 /**
48 * URL cleanup - nothing to do
49 */
50 public function testNoCleanup()
51 {
52 // URL with no query nor fragment
53 $this->assertUrlIsCleaned();
54
55 // URL with no annoying elements
fb1b182f 56 $ref = self::$baseUrl . '?p1=val1&p2=1234#edit';
d9d776af
V
57 $url = new Url($ref);
58 $this->assertEquals($ref, $url->cleanup());
59 }
60
61 /**
62 * URL cleanup - annoying fragment
63 */
64 public function testCleanupFragment()
65 {
66 $this->assertUrlIsCleaned('', '#tk.rss_all');
67 $this->assertUrlIsCleaned('', '#xtor=RSS-');
68 $this->assertUrlIsCleaned('', '#xtor=RSS-U3ht0tkc4b');
69 }
70
71 /**
72 * URL cleanup - single annoying query parameter
73 */
74 public function testCleanupSingleQueryParam()
75 {
76 $this->assertUrlIsCleaned('?action_object_map=junk');
77 $this->assertUrlIsCleaned('?action_ref_map=Cr4p!');
78 $this->assertUrlIsCleaned('?action_type_map=g4R84g3');
79
80 $this->assertUrlIsCleaned('?fb_stuff=v41u3');
81 $this->assertUrlIsCleaned('?fb=71m3w4573');
82
83 $this->assertUrlIsCleaned('?utm_campaign=zomg');
84 $this->assertUrlIsCleaned('?utm_medium=numnum');
85 $this->assertUrlIsCleaned('?utm_source=c0d3');
86 $this->assertUrlIsCleaned('?utm_term=1n4l');
87
88 $this->assertUrlIsCleaned('?xtor=some-url');
725ca094 89 $this->assertUrlIsCleaned('?PHPSESSID=012345678910111213');
d9d776af
V
90 }
91
92 /**
93 * URL cleanup - multiple annoying query parameters
94 */
95 public function testCleanupMultipleQueryParams()
96 {
97 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng');
98 $this->assertUrlIsCleaned(
99 '?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3'
100 );
101 }
102
103 /**
104 * URL cleanup - multiple annoying query parameters, annoying fragment
105 */
106 public function testCleanupMultipleQueryParamsAndFragment()
107 {
108 $this->assertUrlIsCleaned('?xtor=some-url&fb=som3th1ng', '#tk.rss_all');
109 }
110
111 /**
112 * Nominal case - the URL contains both useful and annoying parameters
113 */
114 public function testCleanupMixedContent()
115 {
116 // ditch annoying query params and fragment, keep useful params
117 $url = new Url(
118 self::$baseUrl
fb1b182f 119 . '?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
d9d776af 120 );
fb1b182f 121 $this->assertEquals(self::$baseUrl . '?my=stuff&is=kept', $url->cleanup());
d9d776af
V
122
123
124 // ditch annoying query params, keep useful params and fragment
125 $url = new Url(
126 self::$baseUrl
fb1b182f 127 . '?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
d9d776af
V
128 );
129 $this->assertEquals(
fb1b182f 130 self::$baseUrl . '?my=stuff&is=kept#again',
d9d776af
V
131 $url->cleanup()
132 );
c9da01e7
A
133
134 // test firefox reader url
135 $url = new Url(
fb1b182f 136 'about://reader?url=' . urlencode(self::$baseUrl . '?my=stuff&is=kept')
c9da01e7 137 );
fb1b182f 138 $this->assertEquals(self::$baseUrl . '?my=stuff&is=kept', $url->cleanup());
d9d776af 139 }
9e1724f1
A
140
141 /**
142 * Test default http scheme.
143 */
067c2dd8
V
144 public function testDefaultScheme()
145 {
9e1724f1
A
146 $url = new Url(self::$baseUrl);
147 $this->assertEquals('http', $url->getScheme());
148 $url = new Url('domain.tld');
149 $this->assertEquals('http', $url->getScheme());
150 $url = new Url('ssh://domain.tld');
151 $this->assertEquals('ssh', $url->getScheme());
152 $url = new Url('ftp://domain.tld');
153 $this->assertEquals('ftp', $url->getScheme());
154 $url = new Url('git://domain.tld/push?pull=clone#checkout');
155 $this->assertEquals('git', $url->getScheme());
156 }
938d9cce
A
157
158 /**
159 * Test add trailing slash.
160 */
93b1fe54 161 public function testAddTrailingSlash()
938d9cce
A
162 {
163 $strOn = 'http://randomstr.com/test/';
164 $strOff = 'http://randomstr.com/test';
165 $this->assertEquals($strOn, add_trailing_slash($strOn));
166 $this->assertEquals($strOn, add_trailing_slash($strOff));
167 }
1557cefb
A
168
169 /**
170 * Test valid HTTP url.
171 */
93b1fe54 172 public function testUrlIsHttp()
1557cefb
A
173 {
174 $url = new Url(self::$baseUrl);
175 $this->assertTrue($url->isHttp());
176 }
177
178 /**
179 * Test non HTTP url.
180 */
93b1fe54 181 public function testUrlIsNotHttp()
1557cefb
A
182 {
183 $url = new Url('ftp://save.tld/mysave');
184 $this->assertFalse($url->isHttp());
185 }
ce7b0b64
A
186
187 /**
1336a732 188 * Test International Domain Name to ASCII conversion
ce7b0b64 189 */
93b1fe54 190 public function testIdnToAscii()
ce7b0b64
A
191 {
192 $ind = 'http://www.académie-française.fr/';
193 $expected = 'http://www.xn--acadmie-franaise-npb1a.fr/';
194 $url = new Url($ind);
caa69b58 195 $this->assertEquals($expected, $url->idnToAscii());
ce7b0b64
A
196
197 $notInd = 'http://www.academie-francaise.fr/';
198 $url = new Url($notInd);
caa69b58 199 $this->assertEquals($notInd, $url->idnToAscii());
ce7b0b64 200 }
d9d776af 201}