aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/http/UrlTest.php
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2018-12-03 00:23:35 +0100
committerVirtualTam <virtualtam@flibidi.net>2019-01-12 22:47:48 +0100
commitfb1b182fbf0ee5afed586f77eec84d7a906831ef (patch)
treea076afe14d039b0a02d1ecc64eb83e84698c3d1f /tests/http/UrlTest.php
parent00af48d9d20af1ce51c8ad42fe354fafc9ceb8a3 (diff)
downloadShaarli-fb1b182fbf0ee5afed586f77eec84d7a906831ef.tar.gz
Shaarli-fb1b182fbf0ee5afed586f77eec84d7a906831ef.tar.zst
Shaarli-fb1b182fbf0ee5afed586f77eec84d7a906831ef.zip
namespacing: \Shaarli\Http\Url
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests/http/UrlTest.php')
-rw-r--r--tests/http/UrlTest.php201
1 files changed, 201 insertions, 0 deletions
diff --git a/tests/http/UrlTest.php b/tests/http/UrlTest.php
new file mode 100644
index 00000000..011b416d
--- /dev/null
+++ b/tests/http/UrlTest.php
@@ -0,0 +1,201 @@
1<?php
2/**
3 * Url's tests
4 */
5
6namespace Shaarli\Http;
7
8
9/**
10 * Unitary tests for URL utilities
11 */
12class UrlTest extends \PHPUnit\Framework\TestCase
13{
14 // base URL for tests
15 protected static $baseUrl = 'http://domain.tld:3000';
16
17 /**
18 * Helper method
19 */
20 private function assertUrlIsCleaned($query = '', $fragment = '')
21 {
22 $url = new Url(self::$baseUrl . $query . $fragment);
23 $url->cleanup();
24 $this->assertEquals(self::$baseUrl, $url->toString());
25 }
26
27 /**
28 * Instantiate an empty URL
29 */
30 public function testEmptyConstruct()
31 {
32 $url = new Url('');
33 $this->assertEquals('', $url->toString());
34 }
35
36 /**
37 * Instantiate a URL
38 */
39 public function testConstruct()
40 {
41 $ref = 'http://username:password@hostname:9090/path'
42 . '?arg1=value1&arg2=value2#anchor';
43 $url = new Url($ref);
44 $this->assertEquals($ref, $url->toString());
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
56 $ref = self::$baseUrl . '?p1=val1&p2=1234#edit';
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');
89 $this->assertUrlIsCleaned('?PHPSESSID=012345678910111213');
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
119 . '?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all'
120 );
121 $this->assertEquals(self::$baseUrl . '?my=stuff&is=kept', $url->cleanup());
122
123
124 // ditch annoying query params, keep useful params and fragment
125 $url = new Url(
126 self::$baseUrl
127 . '?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again'
128 );
129 $this->assertEquals(
130 self::$baseUrl . '?my=stuff&is=kept#again',
131 $url->cleanup()
132 );
133
134 // test firefox reader url
135 $url = new Url(
136 'about://reader?url=' . urlencode(self::$baseUrl . '?my=stuff&is=kept')
137 );
138 $this->assertEquals(self::$baseUrl . '?my=stuff&is=kept', $url->cleanup());
139 }
140
141 /**
142 * Test default http scheme.
143 */
144 public function testDefaultScheme()
145 {
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 }
157
158 /**
159 * Test add trailing slash.
160 */
161 public function testAddTrailingSlash()
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 }
168
169 /**
170 * Test valid HTTP url.
171 */
172 public function testUrlIsHttp()
173 {
174 $url = new Url(self::$baseUrl);
175 $this->assertTrue($url->isHttp());
176 }
177
178 /**
179 * Test non HTTP url.
180 */
181 public function testUrlIsNotHttp()
182 {
183 $url = new Url('ftp://save.tld/mysave');
184 $this->assertFalse($url->isHttp());
185 }
186
187 /**
188 * Test International Domain Name to ASCII conversion
189 */
190 public function testIdnToAscii()
191 {
192 $ind = 'http://www.académie-française.fr/';
193 $expected = 'http://www.xn--acadmie-franaise-npb1a.fr/';
194 $url = new Url($ind);
195 $this->assertEquals($expected, $url->idnToAscii());
196
197 $notInd = 'http://www.academie-francaise.fr/';
198 $url = new Url($notInd);
199 $this->assertEquals($notInd, $url->idnToAscii());
200 }
201}