diff options
Diffstat (limited to 'tests/http')
-rw-r--r-- | tests/http/HttpUtils/ClientIpIdTest.php | 54 | ||||
-rw-r--r-- | tests/http/HttpUtils/GetHttpUrlTest.php | 67 | ||||
-rw-r--r-- | tests/http/HttpUtils/GetIpAdressFromProxyTest.php | 61 | ||||
-rw-r--r-- | tests/http/HttpUtils/IndexUrlTest.php | 74 | ||||
-rw-r--r-- | tests/http/HttpUtils/IsHttpsTest.php | 39 | ||||
-rw-r--r-- | tests/http/HttpUtils/PageUrlTest.php | 78 | ||||
-rw-r--r-- | tests/http/HttpUtils/ServerUrlTest.php | 223 | ||||
-rw-r--r-- | tests/http/UrlTest.php | 2 | ||||
-rw-r--r-- | tests/http/UrlUtils/CleanupUrlTest.php | 111 | ||||
-rw-r--r-- | tests/http/UrlUtils/GetUrlSchemeTest.php | 32 | ||||
-rw-r--r-- | tests/http/UrlUtils/UnparseUrlTest.php | 32 | ||||
-rw-r--r-- | tests/http/UrlUtils/WhitelistProtocolsTest.php | 63 |
12 files changed, 835 insertions, 1 deletions
diff --git a/tests/http/HttpUtils/ClientIpIdTest.php b/tests/http/HttpUtils/ClientIpIdTest.php new file mode 100644 index 00000000..982e57e0 --- /dev/null +++ b/tests/http/HttpUtils/ClientIpIdTest.php | |||
@@ -0,0 +1,54 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * HttpUtils' tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/HttpUtils.php'; | ||
9 | |||
10 | /** | ||
11 | * Unitary tests for client_ip_id() | ||
12 | */ | ||
13 | class ClientIpIdTest extends \PHPUnit\Framework\TestCase | ||
14 | { | ||
15 | /** | ||
16 | * Get a remote client ID based on its IP | ||
17 | */ | ||
18 | public function testClientIpIdRemote() | ||
19 | { | ||
20 | $this->assertEquals( | ||
21 | '10.1.167.42', | ||
22 | client_ip_id(['REMOTE_ADDR' => '10.1.167.42']) | ||
23 | ); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Get a remote client ID based on its IP and proxy information (1) | ||
28 | */ | ||
29 | public function testClientIpIdRemoteForwarded() | ||
30 | { | ||
31 | $this->assertEquals( | ||
32 | '10.1.167.42_127.0.1.47', | ||
33 | client_ip_id([ | ||
34 | 'REMOTE_ADDR' => '10.1.167.42', | ||
35 | 'HTTP_X_FORWARDED_FOR' => '127.0.1.47' | ||
36 | ]) | ||
37 | ); | ||
38 | } | ||
39 | |||
40 | /** | ||
41 | * Get a remote client ID based on its IP and proxy information (2) | ||
42 | */ | ||
43 | public function testClientIpIdRemoteForwardedClient() | ||
44 | { | ||
45 | $this->assertEquals( | ||
46 | '10.1.167.42_10.1.167.56_127.0.1.47', | ||
47 | client_ip_id([ | ||
48 | 'REMOTE_ADDR' => '10.1.167.42', | ||
49 | 'HTTP_X_FORWARDED_FOR' => '10.1.167.56', | ||
50 | 'HTTP_CLIENT_IP' => '127.0.1.47' | ||
51 | ]) | ||
52 | ); | ||
53 | } | ||
54 | } | ||
diff --git a/tests/http/HttpUtils/GetHttpUrlTest.php b/tests/http/HttpUtils/GetHttpUrlTest.php new file mode 100644 index 00000000..3dc5bc9b --- /dev/null +++ b/tests/http/HttpUtils/GetHttpUrlTest.php | |||
@@ -0,0 +1,67 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * HttpUtils' tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/HttpUtils.php'; | ||
9 | |||
10 | /** | ||
11 | * Unitary tests for get_http_response() | ||
12 | */ | ||
13 | class GetHttpUrlTest extends \PHPUnit\Framework\TestCase | ||
14 | { | ||
15 | /** | ||
16 | * Get an invalid local URL | ||
17 | */ | ||
18 | public function testGetInvalidLocalUrl() | ||
19 | { | ||
20 | // Local | ||
21 | list($headers, $content) = get_http_response('/non/existent', 1); | ||
22 | $this->assertEquals('Invalid HTTP UrlUtils', $headers[0]); | ||
23 | $this->assertFalse($content); | ||
24 | |||
25 | // Non HTTP | ||
26 | list($headers, $content) = get_http_response('ftp://save.tld/mysave', 1); | ||
27 | $this->assertEquals('Invalid HTTP UrlUtils', $headers[0]); | ||
28 | $this->assertFalse($content); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Get an invalid remote URL | ||
33 | */ | ||
34 | public function testGetInvalidRemoteUrl() | ||
35 | { | ||
36 | list($headers, $content) = @get_http_response('http://non.existent', 1); | ||
37 | $this->assertFalse($headers); | ||
38 | $this->assertFalse($content); | ||
39 | } | ||
40 | |||
41 | /** | ||
42 | * Test getAbsoluteUrl with relative target URL. | ||
43 | */ | ||
44 | public function testGetAbsoluteUrlWithRelative() | ||
45 | { | ||
46 | $origin = 'http://non.existent/blabla/?test'; | ||
47 | $target = '/stuff.php'; | ||
48 | |||
49 | $expected = 'http://non.existent/stuff.php'; | ||
50 | $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); | ||
51 | |||
52 | $target = 'stuff.php'; | ||
53 | $expected = 'http://non.existent/blabla/stuff.php'; | ||
54 | $this->assertEquals($expected, getAbsoluteUrl($origin, $target)); | ||
55 | } | ||
56 | |||
57 | /** | ||
58 | * Test getAbsoluteUrl with absolute target URL. | ||
59 | */ | ||
60 | public function testGetAbsoluteUrlWithAbsolute() | ||
61 | { | ||
62 | $origin = 'http://non.existent/blabla/?test'; | ||
63 | $target = 'http://other.url/stuff.php'; | ||
64 | |||
65 | $this->assertEquals($target, getAbsoluteUrl($origin, $target)); | ||
66 | } | ||
67 | } | ||
diff --git a/tests/http/HttpUtils/GetIpAdressFromProxyTest.php b/tests/http/HttpUtils/GetIpAdressFromProxyTest.php new file mode 100644 index 00000000..fe3a639e --- /dev/null +++ b/tests/http/HttpUtils/GetIpAdressFromProxyTest.php | |||
@@ -0,0 +1,61 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Http; | ||
4 | |||
5 | require_once 'application/http/HttpUtils.php'; | ||
6 | |||
7 | /** | ||
8 | * Unitary tests for getIpAddressFromProxy() | ||
9 | */ | ||
10 | class GetIpAdressFromProxyTest extends \PHPUnit\Framework\TestCase | ||
11 | { | ||
12 | |||
13 | /** | ||
14 | * Test without proxy | ||
15 | */ | ||
16 | public function testWithoutProxy() | ||
17 | { | ||
18 | $this->assertFalse(getIpAddressFromProxy(array(), array())); | ||
19 | } | ||
20 | |||
21 | /** | ||
22 | * Test with a single IP in proxy header. | ||
23 | */ | ||
24 | public function testWithOneForwardedIp() | ||
25 | { | ||
26 | $ip = '1.1.1.1'; | ||
27 | $server = array('HTTP_X_FORWARDED_FOR' => $ip); | ||
28 | $this->assertEquals($ip, getIpAddressFromProxy($server, array())); | ||
29 | } | ||
30 | |||
31 | /** | ||
32 | * Test with a multiple IPs in proxy header. | ||
33 | */ | ||
34 | public function testWithMultipleForwardedIp() | ||
35 | { | ||
36 | $ip = '1.1.1.1'; | ||
37 | $ip2 = '2.2.2.2'; | ||
38 | |||
39 | $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); | ||
40 | $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); | ||
41 | |||
42 | $server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2); | ||
43 | $this->assertEquals($ip2, getIpAddressFromProxy($server, array())); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * Test with a trusted IP address. | ||
48 | */ | ||
49 | public function testWithTrustedIp() | ||
50 | { | ||
51 | $ip = '1.1.1.1'; | ||
52 | $ip2 = '2.2.2.2'; | ||
53 | |||
54 | $server = array('HTTP_X_FORWARDED_FOR' => $ip); | ||
55 | $this->assertFalse(getIpAddressFromProxy($server, array($ip))); | ||
56 | |||
57 | $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2); | ||
58 | $this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip))); | ||
59 | $this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2))); | ||
60 | } | ||
61 | } | ||
diff --git a/tests/http/HttpUtils/IndexUrlTest.php b/tests/http/HttpUtils/IndexUrlTest.php new file mode 100644 index 00000000..bcbe59cb --- /dev/null +++ b/tests/http/HttpUtils/IndexUrlTest.php | |||
@@ -0,0 +1,74 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * HttpUtils' tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/HttpUtils.php'; | ||
9 | |||
10 | /** | ||
11 | * Unitary tests for index_url() | ||
12 | */ | ||
13 | class IndexUrlTest extends \PHPUnit\Framework\TestCase | ||
14 | { | ||
15 | /** | ||
16 | * If on the main page, remove "index.php" from the URL resource | ||
17 | */ | ||
18 | public function testRemoveIndex() | ||
19 | { | ||
20 | $this->assertEquals( | ||
21 | 'http://host.tld/', | ||
22 | index_url( | ||
23 | array( | ||
24 | 'HTTPS' => 'Off', | ||
25 | 'SERVER_NAME' => 'host.tld', | ||
26 | 'SERVER_PORT' => '80', | ||
27 | 'SCRIPT_NAME' => '/index.php' | ||
28 | ) | ||
29 | ) | ||
30 | ); | ||
31 | |||
32 | $this->assertEquals( | ||
33 | 'http://host.tld/admin/', | ||
34 | index_url( | ||
35 | array( | ||
36 | 'HTTPS' => 'Off', | ||
37 | 'SERVER_NAME' => 'host.tld', | ||
38 | 'SERVER_PORT' => '80', | ||
39 | 'SCRIPT_NAME' => '/admin/index.php' | ||
40 | ) | ||
41 | ) | ||
42 | ); | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * The resource is != "index.php" | ||
47 | */ | ||
48 | public function testOtherResource() | ||
49 | { | ||
50 | $this->assertEquals( | ||
51 | 'http://host.tld/page.php', | ||
52 | page_url( | ||
53 | array( | ||
54 | 'HTTPS' => 'Off', | ||
55 | 'SERVER_NAME' => 'host.tld', | ||
56 | 'SERVER_PORT' => '80', | ||
57 | 'SCRIPT_NAME' => '/page.php' | ||
58 | ) | ||
59 | ) | ||
60 | ); | ||
61 | |||
62 | $this->assertEquals( | ||
63 | 'http://host.tld/admin/page.php', | ||
64 | page_url( | ||
65 | array( | ||
66 | 'HTTPS' => 'Off', | ||
67 | 'SERVER_NAME' => 'host.tld', | ||
68 | 'SERVER_PORT' => '80', | ||
69 | 'SCRIPT_NAME' => '/admin/page.php' | ||
70 | ) | ||
71 | ) | ||
72 | ); | ||
73 | } | ||
74 | } | ||
diff --git a/tests/http/HttpUtils/IsHttpsTest.php b/tests/http/HttpUtils/IsHttpsTest.php new file mode 100644 index 00000000..348956c6 --- /dev/null +++ b/tests/http/HttpUtils/IsHttpsTest.php | |||
@@ -0,0 +1,39 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Http; | ||
4 | |||
5 | require_once 'application/http/HttpUtils.php'; | ||
6 | |||
7 | /** | ||
8 | * Class IsHttpsTest | ||
9 | * | ||
10 | * Test class for is_https() function. | ||
11 | */ | ||
12 | class IsHttpsTest extends \PHPUnit\Framework\TestCase | ||
13 | { | ||
14 | |||
15 | /** | ||
16 | * Test is_https with HTTPS values. | ||
17 | */ | ||
18 | public function testIsHttpsTrue() | ||
19 | { | ||
20 | $this->assertTrue(is_https(['HTTPS' => true])); | ||
21 | $this->assertTrue(is_https(['HTTPS' => '1'])); | ||
22 | $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 443])); | ||
23 | $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443'])); | ||
24 | $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443,123,456,'])); | ||
25 | } | ||
26 | |||
27 | /** | ||
28 | * Test is_https with HTTP values. | ||
29 | */ | ||
30 | public function testIsHttpsFalse() | ||
31 | { | ||
32 | $this->assertFalse(is_https([])); | ||
33 | $this->assertFalse(is_https(['HTTPS' => false])); | ||
34 | $this->assertFalse(is_https(['HTTPS' => '0'])); | ||
35 | $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 123])); | ||
36 | $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '123'])); | ||
37 | $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => ',123,456,'])); | ||
38 | } | ||
39 | } | ||
diff --git a/tests/http/HttpUtils/PageUrlTest.php b/tests/http/HttpUtils/PageUrlTest.php new file mode 100644 index 00000000..f1991716 --- /dev/null +++ b/tests/http/HttpUtils/PageUrlTest.php | |||
@@ -0,0 +1,78 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * HttpUtils' tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/HttpUtils.php'; | ||
9 | |||
10 | /** | ||
11 | * Unitary tests for page_url() | ||
12 | */ | ||
13 | class PageUrlTest extends \PHPUnit\Framework\TestCase | ||
14 | { | ||
15 | /** | ||
16 | * If on the main page, remove "index.php" from the URL resource | ||
17 | */ | ||
18 | public function testRemoveIndex() | ||
19 | { | ||
20 | $this->assertEquals( | ||
21 | 'http://host.tld/?p1=v1&p2=v2', | ||
22 | page_url( | ||
23 | array( | ||
24 | 'HTTPS' => 'Off', | ||
25 | 'SERVER_NAME' => 'host.tld', | ||
26 | 'SERVER_PORT' => '80', | ||
27 | 'SCRIPT_NAME' => '/index.php', | ||
28 | 'QUERY_STRING' => 'p1=v1&p2=v2' | ||
29 | ) | ||
30 | ) | ||
31 | ); | ||
32 | |||
33 | $this->assertEquals( | ||
34 | 'http://host.tld/admin/?action=edit_tag', | ||
35 | page_url( | ||
36 | array( | ||
37 | 'HTTPS' => 'Off', | ||
38 | 'SERVER_NAME' => 'host.tld', | ||
39 | 'SERVER_PORT' => '80', | ||
40 | 'SCRIPT_NAME' => '/admin/index.php', | ||
41 | 'QUERY_STRING' => 'action=edit_tag' | ||
42 | ) | ||
43 | ) | ||
44 | ); | ||
45 | } | ||
46 | |||
47 | /** | ||
48 | * The resource is != "index.php" | ||
49 | */ | ||
50 | public function testOtherResource() | ||
51 | { | ||
52 | $this->assertEquals( | ||
53 | 'http://host.tld/page.php?p1=v1&p2=v2', | ||
54 | page_url( | ||
55 | array( | ||
56 | 'HTTPS' => 'Off', | ||
57 | 'SERVER_NAME' => 'host.tld', | ||
58 | 'SERVER_PORT' => '80', | ||
59 | 'SCRIPT_NAME' => '/page.php', | ||
60 | 'QUERY_STRING' => 'p1=v1&p2=v2' | ||
61 | ) | ||
62 | ) | ||
63 | ); | ||
64 | |||
65 | $this->assertEquals( | ||
66 | 'http://host.tld/admin/page.php?action=edit_tag', | ||
67 | page_url( | ||
68 | array( | ||
69 | 'HTTPS' => 'Off', | ||
70 | 'SERVER_NAME' => 'host.tld', | ||
71 | 'SERVER_PORT' => '80', | ||
72 | 'SCRIPT_NAME' => '/admin/page.php', | ||
73 | 'QUERY_STRING' => 'action=edit_tag' | ||
74 | ) | ||
75 | ) | ||
76 | ); | ||
77 | } | ||
78 | } | ||
diff --git a/tests/http/HttpUtils/ServerUrlTest.php b/tests/http/HttpUtils/ServerUrlTest.php new file mode 100644 index 00000000..9caf1049 --- /dev/null +++ b/tests/http/HttpUtils/ServerUrlTest.php | |||
@@ -0,0 +1,223 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * HttpUtils' tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/HttpUtils.php'; | ||
9 | |||
10 | /** | ||
11 | * Unitary tests for server_url() | ||
12 | */ | ||
13 | class ServerUrlTest extends \PHPUnit\Framework\TestCase | ||
14 | { | ||
15 | /** | ||
16 | * Detect if the server uses SSL | ||
17 | */ | ||
18 | public function testHttpsScheme() | ||
19 | { | ||
20 | $this->assertEquals( | ||
21 | 'https://host.tld', | ||
22 | server_url( | ||
23 | array( | ||
24 | 'HTTPS' => 'ON', | ||
25 | 'SERVER_NAME' => 'host.tld', | ||
26 | 'SERVER_PORT' => '443' | ||
27 | ) | ||
28 | ) | ||
29 | ); | ||
30 | |||
31 | $this->assertEquals( | ||
32 | 'https://host.tld:8080', | ||
33 | server_url( | ||
34 | array( | ||
35 | 'HTTPS' => 'ON', | ||
36 | 'SERVER_NAME' => 'host.tld', | ||
37 | 'SERVER_PORT' => '8080' | ||
38 | ) | ||
39 | ) | ||
40 | ); | ||
41 | } | ||
42 | |||
43 | /** | ||
44 | * Detect a Proxy that sets Forwarded-Host | ||
45 | */ | ||
46 | public function testHttpsProxyForwardedHost() | ||
47 | { | ||
48 | $this->assertEquals( | ||
49 | 'https://host.tld:8080', | ||
50 | server_url( | ||
51 | array( | ||
52 | 'HTTP_X_FORWARDED_PROTO' => 'https', | ||
53 | 'HTTP_X_FORWARDED_PORT' => '8080', | ||
54 | 'HTTP_X_FORWARDED_HOST' => 'host.tld' | ||
55 | ) | ||
56 | ) | ||
57 | ); | ||
58 | |||
59 | $this->assertEquals( | ||
60 | 'https://host.tld:4974', | ||
61 | server_url( | ||
62 | array( | ||
63 | 'HTTP_X_FORWARDED_PROTO' => 'https, https', | ||
64 | 'HTTP_X_FORWARDED_PORT' => '4974, 80', | ||
65 | 'HTTP_X_FORWARDED_HOST' => 'host.tld, example.com' | ||
66 | ) | ||
67 | ) | ||
68 | ); | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * Detect a Proxy with SSL enabled | ||
73 | */ | ||
74 | public function testHttpsProxyForward() | ||
75 | { | ||
76 | $this->assertEquals( | ||
77 | 'https://host.tld:8080', | ||
78 | server_url( | ||
79 | array( | ||
80 | 'HTTPS' => 'Off', | ||
81 | 'SERVER_NAME' => 'host.tld', | ||
82 | 'SERVER_PORT' => '80', | ||
83 | 'HTTP_X_FORWARDED_PROTO' => 'https', | ||
84 | 'HTTP_X_FORWARDED_PORT' => '8080' | ||
85 | ) | ||
86 | ) | ||
87 | ); | ||
88 | |||
89 | $this->assertEquals( | ||
90 | 'https://host.tld', | ||
91 | server_url( | ||
92 | array( | ||
93 | 'HTTPS' => 'Off', | ||
94 | 'SERVER_NAME' => 'host.tld', | ||
95 | 'SERVER_PORT' => '80', | ||
96 | 'HTTP_X_FORWARDED_PROTO' => 'https' | ||
97 | ) | ||
98 | ) | ||
99 | ); | ||
100 | |||
101 | $this->assertEquals( | ||
102 | 'https://host.tld', | ||
103 | server_url( | ||
104 | array( | ||
105 | 'HTTPS' => 'Off', | ||
106 | 'SERVER_NAME' => 'host.tld', | ||
107 | 'SERVER_PORT' => '80', | ||
108 | 'HTTP_X_FORWARDED_PROTO' => 'https', | ||
109 | 'HTTP_X_FORWARDED_PORT' => '443' | ||
110 | ) | ||
111 | ) | ||
112 | ); | ||
113 | |||
114 | $this->assertEquals( | ||
115 | 'https://host.tld:4974', | ||
116 | server_url( | ||
117 | array( | ||
118 | 'HTTPS' => 'Off', | ||
119 | 'SERVER_NAME' => 'host.tld', | ||
120 | 'SERVER_PORT' => '80', | ||
121 | 'HTTP_X_FORWARDED_PROTO' => 'https, https', | ||
122 | 'HTTP_X_FORWARDED_PORT' => '4974, 80' | ||
123 | ) | ||
124 | ) | ||
125 | ); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * Detect if the server uses a specific port (!= 80) | ||
130 | */ | ||
131 | public function testPort() | ||
132 | { | ||
133 | // HTTP | ||
134 | $this->assertEquals( | ||
135 | 'http://host.tld:8080', | ||
136 | server_url( | ||
137 | array( | ||
138 | 'HTTPS' => 'OFF', | ||
139 | 'SERVER_NAME' => 'host.tld', | ||
140 | 'SERVER_PORT' => '8080' | ||
141 | ) | ||
142 | ) | ||
143 | ); | ||
144 | |||
145 | // HTTPS | ||
146 | $this->assertEquals( | ||
147 | 'https://host.tld:8080', | ||
148 | server_url( | ||
149 | array( | ||
150 | 'HTTPS' => 'ON', | ||
151 | 'SERVER_NAME' => 'host.tld', | ||
152 | 'SERVER_PORT' => '8080' | ||
153 | ) | ||
154 | ) | ||
155 | ); | ||
156 | } | ||
157 | |||
158 | /** | ||
159 | * HTTP server on port 80 | ||
160 | */ | ||
161 | public function testStandardHttpPort() | ||
162 | { | ||
163 | $this->assertEquals( | ||
164 | 'http://host.tld', | ||
165 | server_url( | ||
166 | array( | ||
167 | 'HTTPS' => 'OFF', | ||
168 | 'SERVER_NAME' => 'host.tld', | ||
169 | 'SERVER_PORT' => '80' | ||
170 | ) | ||
171 | ) | ||
172 | ); | ||
173 | } | ||
174 | |||
175 | /** | ||
176 | * HTTPS server on port 443 | ||
177 | */ | ||
178 | public function testStandardHttpsPort() | ||
179 | { | ||
180 | $this->assertEquals( | ||
181 | 'https://host.tld', | ||
182 | server_url( | ||
183 | array( | ||
184 | 'HTTPS' => 'ON', | ||
185 | 'SERVER_NAME' => 'host.tld', | ||
186 | 'SERVER_PORT' => '443' | ||
187 | ) | ||
188 | ) | ||
189 | ); | ||
190 | } | ||
191 | |||
192 | /** | ||
193 | * Misconfigured server (see #1022): Proxy HTTP but 443 | ||
194 | */ | ||
195 | public function testHttpWithPort433() | ||
196 | { | ||
197 | $this->assertEquals( | ||
198 | 'https://host.tld', | ||
199 | server_url( | ||
200 | array( | ||
201 | 'HTTPS' => 'Off', | ||
202 | 'SERVER_NAME' => 'host.tld', | ||
203 | 'SERVER_PORT' => '80', | ||
204 | 'HTTP_X_FORWARDED_PROTO' => 'http', | ||
205 | 'HTTP_X_FORWARDED_PORT' => '443' | ||
206 | ) | ||
207 | ) | ||
208 | ); | ||
209 | |||
210 | $this->assertEquals( | ||
211 | 'https://host.tld', | ||
212 | server_url( | ||
213 | array( | ||
214 | 'HTTPS' => 'Off', | ||
215 | 'SERVER_NAME' => 'host.tld', | ||
216 | 'SERVER_PORT' => '80', | ||
217 | 'HTTP_X_FORWARDED_PROTO' => 'https, http', | ||
218 | 'HTTP_X_FORWARDED_PORT' => '443, 80' | ||
219 | ) | ||
220 | ) | ||
221 | ); | ||
222 | } | ||
223 | } | ||
diff --git a/tests/http/UrlTest.php b/tests/http/UrlTest.php index 011b416d..342b78a4 100644 --- a/tests/http/UrlTest.php +++ b/tests/http/UrlTest.php | |||
@@ -1,6 +1,6 @@ | |||
1 | <?php | 1 | <?php |
2 | /** | 2 | /** |
3 | * Url's tests | 3 | * UrlUtils's tests |
4 | */ | 4 | */ |
5 | 5 | ||
6 | namespace Shaarli\Http; | 6 | namespace Shaarli\Http; |
diff --git a/tests/http/UrlUtils/CleanupUrlTest.php b/tests/http/UrlUtils/CleanupUrlTest.php new file mode 100644 index 00000000..6c4d124b --- /dev/null +++ b/tests/http/UrlUtils/CleanupUrlTest.php | |||
@@ -0,0 +1,111 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Unitary tests for cleanup_url() | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/UrlUtils.php'; | ||
9 | |||
10 | class CleanupUrlTest extends \PHPUnit\Framework\TestCase | ||
11 | { | ||
12 | /** | ||
13 | * @var string reference URL | ||
14 | */ | ||
15 | protected $ref = 'http://domain.tld:3000'; | ||
16 | |||
17 | |||
18 | /** | ||
19 | * Clean empty URL | ||
20 | */ | ||
21 | public function testCleanupUrlEmpty() | ||
22 | { | ||
23 | $this->assertEquals('', cleanup_url('')); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Clean an already cleaned URL | ||
28 | */ | ||
29 | public function testCleanupUrlAlreadyClean() | ||
30 | { | ||
31 | $this->assertEquals($this->ref, cleanup_url($this->ref)); | ||
32 | $this->ref2 = $this->ref.'/path/to/dir/'; | ||
33 | $this->assertEquals($this->ref2, cleanup_url($this->ref2)); | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Clean URL fragments | ||
38 | */ | ||
39 | public function testCleanupUrlFragment() | ||
40 | { | ||
41 | $this->assertEquals($this->ref, cleanup_url($this->ref.'#tk.rss_all')); | ||
42 | $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-')); | ||
43 | $this->assertEquals($this->ref, cleanup_url($this->ref.'#xtor=RSS-U3ht0tkc4b')); | ||
44 | } | ||
45 | |||
46 | /** | ||
47 | * Clean URL query - single annoying parameter | ||
48 | */ | ||
49 | public function testCleanupUrlQuerySingle() | ||
50 | { | ||
51 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_object_map=junk')); | ||
52 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_ref_map=Cr4p!')); | ||
53 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?action_type_map=g4R84g3')); | ||
54 | |||
55 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb_stuff=v41u3')); | ||
56 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?fb=71m3w4573')); | ||
57 | |||
58 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_campaign=zomg')); | ||
59 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_medium=numnum')); | ||
60 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_source=c0d3')); | ||
61 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?utm_term=1n4l')); | ||
62 | |||
63 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url')); | ||
64 | |||
65 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_name=junk')); | ||
66 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_start=junk')); | ||
67 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?campaign_item_index=junk')); | ||
68 | } | ||
69 | |||
70 | /** | ||
71 | * Clean URL query - multiple annoying parameters | ||
72 | */ | ||
73 | public function testCleanupUrlQueryMultiple() | ||
74 | { | ||
75 | $this->assertEquals($this->ref, cleanup_url($this->ref.'?xtor=some-url&fb=som3th1ng')); | ||
76 | |||
77 | $this->assertEquals($this->ref, cleanup_url( | ||
78 | $this->ref.'?fb=stuff&utm_campaign=zomg&utm_medium=numnum&utm_source=c0d3' | ||
79 | )); | ||
80 | |||
81 | $this->assertEquals($this->ref, cleanup_url( | ||
82 | $this->ref.'?campaign_start=zomg&campaign_name=numnum' | ||
83 | )); | ||
84 | } | ||
85 | |||
86 | /** | ||
87 | * Clean URL query - multiple annoying parameters and fragment | ||
88 | */ | ||
89 | public function testCleanupUrlQueryFragment() | ||
90 | { | ||
91 | $this->assertEquals($this->ref, cleanup_url( | ||
92 | $this->ref.'?xtor=some-url&fb=som3th1ng#tk.rss_all' | ||
93 | )); | ||
94 | |||
95 | // ditch annoying query params and fragment, keep useful params | ||
96 | $this->assertEquals( | ||
97 | $this->ref.'?my=stuff&is=kept', | ||
98 | cleanup_url( | ||
99 | $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#tk.rss_all' | ||
100 | ) | ||
101 | ); | ||
102 | |||
103 | // ditch annoying query params, keep useful params and fragment | ||
104 | $this->assertEquals( | ||
105 | $this->ref.'?my=stuff&is=kept#again', | ||
106 | cleanup_url( | ||
107 | $this->ref.'?fb=zomg&my=stuff&utm_medium=numnum&is=kept#again' | ||
108 | ) | ||
109 | ); | ||
110 | } | ||
111 | } | ||
diff --git a/tests/http/UrlUtils/GetUrlSchemeTest.php b/tests/http/UrlUtils/GetUrlSchemeTest.php new file mode 100644 index 00000000..2b97f7be --- /dev/null +++ b/tests/http/UrlUtils/GetUrlSchemeTest.php | |||
@@ -0,0 +1,32 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Unitary tests for get_url_scheme() | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/UrlUtils.php'; | ||
9 | |||
10 | class GetUrlSchemeTest extends \PHPUnit\Framework\TestCase | ||
11 | { | ||
12 | /** | ||
13 | * Get empty scheme string for empty UrlUtils | ||
14 | */ | ||
15 | public function testGetUrlSchemeEmpty() | ||
16 | { | ||
17 | $this->assertEquals('', get_url_scheme('')); | ||
18 | } | ||
19 | |||
20 | /** | ||
21 | * Get normal scheme of UrlUtils | ||
22 | */ | ||
23 | public function testGetUrlScheme() | ||
24 | { | ||
25 | $this->assertEquals('http', get_url_scheme('http://domain.tld:3000')); | ||
26 | $this->assertEquals('https', get_url_scheme('https://domain.tld:3000')); | ||
27 | $this->assertEquals('http', get_url_scheme('domain.tld')); | ||
28 | $this->assertEquals('ssh', get_url_scheme('ssh://domain.tld')); | ||
29 | $this->assertEquals('ftp', get_url_scheme('ftp://domain.tld')); | ||
30 | $this->assertEquals('git', get_url_scheme('git://domain.tld/push?pull=clone#checkout')); | ||
31 | } | ||
32 | } | ||
diff --git a/tests/http/UrlUtils/UnparseUrlTest.php b/tests/http/UrlUtils/UnparseUrlTest.php new file mode 100644 index 00000000..040d8c54 --- /dev/null +++ b/tests/http/UrlUtils/UnparseUrlTest.php | |||
@@ -0,0 +1,32 @@ | |||
1 | <?php | ||
2 | /** | ||
3 | * Unpares UrlUtils's tests | ||
4 | */ | ||
5 | |||
6 | namespace Shaarli\Http; | ||
7 | |||
8 | require_once 'application/http/UrlUtils.php'; | ||
9 | |||
10 | /** | ||
11 | * Unitary tests for unparse_url() | ||
12 | */ | ||
13 | class UnparseUrlTest extends \PHPUnit\Framework\TestCase | ||
14 | { | ||
15 | /** | ||
16 | * Thanks for building nothing | ||
17 | */ | ||
18 | public function testUnparseEmptyArray() | ||
19 | { | ||
20 | $this->assertEquals('', unparse_url(array())); | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * Rebuild a full-featured URL | ||
25 | */ | ||
26 | public function testUnparseFull() | ||
27 | { | ||
28 | $ref = 'http://username:password@hostname:9090/path' | ||
29 | .'?arg1=value1&arg2=value2#anchor'; | ||
30 | $this->assertEquals($ref, unparse_url(parse_url($ref))); | ||
31 | } | ||
32 | } | ||
diff --git a/tests/http/UrlUtils/WhitelistProtocolsTest.php b/tests/http/UrlUtils/WhitelistProtocolsTest.php new file mode 100644 index 00000000..69512dbd --- /dev/null +++ b/tests/http/UrlUtils/WhitelistProtocolsTest.php | |||
@@ -0,0 +1,63 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Shaarli\Http; | ||
4 | |||
5 | require_once 'application/http/UrlUtils.php'; | ||
6 | |||
7 | /** | ||
8 | * Class WhitelistProtocolsTest | ||
9 | * | ||
10 | * Test whitelist_protocols() function of UrlUtils. | ||
11 | */ | ||
12 | class WhitelistProtocolsTest extends \PHPUnit\Framework\TestCase | ||
13 | { | ||
14 | /** | ||
15 | * Test whitelist_protocols() on a note (relative URL). | ||
16 | */ | ||
17 | public function testWhitelistProtocolsRelative() | ||
18 | { | ||
19 | $whitelist = ['ftp', 'magnet']; | ||
20 | $url = '?12443564'; | ||
21 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
22 | $url = '/path.jpg'; | ||
23 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
24 | } | ||
25 | |||
26 | /** | ||
27 | * Test whitelist_protocols() on a note (relative URL). | ||
28 | */ | ||
29 | public function testWhitelistProtocolMissing() | ||
30 | { | ||
31 | $whitelist = ['ftp', 'magnet']; | ||
32 | $url = 'test.tld/path/?query=value#hash'; | ||
33 | $this->assertEquals('http://'. $url, whitelist_protocols($url, $whitelist)); | ||
34 | } | ||
35 | |||
36 | /** | ||
37 | * Test whitelist_protocols() with allowed protocols. | ||
38 | */ | ||
39 | public function testWhitelistAllowedProtocol() | ||
40 | { | ||
41 | $whitelist = ['ftp', 'magnet']; | ||
42 | $url = 'http://test.tld/path/?query=value#hash'; | ||
43 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
44 | $url = 'https://test.tld/path/?query=value#hash'; | ||
45 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
46 | $url = 'ftp://test.tld/path/?query=value#hash'; | ||
47 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
48 | $url = 'magnet:test.tld/path/?query=value#hash'; | ||
49 | $this->assertEquals($url, whitelist_protocols($url, $whitelist)); | ||
50 | } | ||
51 | |||
52 | /** | ||
53 | * Test whitelist_protocols() with allowed protocols. | ||
54 | */ | ||
55 | public function testWhitelistDisallowedProtocol() | ||
56 | { | ||
57 | $whitelist = ['ftp', 'magnet']; | ||
58 | $url = 'javascript:alert("xss");'; | ||
59 | $this->assertEquals('http://alert("xss");', whitelist_protocols($url, $whitelist)); | ||
60 | $url = 'other://test.tld/path/?query=value#hash'; | ||
61 | $this->assertEquals('http://test.tld/path/?query=value#hash', whitelist_protocols($url, $whitelist)); | ||
62 | } | ||
63 | } | ||