aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/http/HttpUtils
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2018-12-03 00:34:53 +0100
committerVirtualTam <virtualtam@flibidi.net>2019-01-12 22:47:48 +0100
commit51753e403fa69c0ce124ede27d300477e3e799ca (patch)
tree0afe2a84598648b49cc53bb3e8640569ac370240 /tests/http/HttpUtils
parentfb1b182fbf0ee5afed586f77eec84d7a906831ef (diff)
downloadShaarli-51753e403fa69c0ce124ede27d300477e3e799ca.tar.gz
Shaarli-51753e403fa69c0ce124ede27d300477e3e799ca.tar.zst
Shaarli-51753e403fa69c0ce124ede27d300477e3e799ca.zip
namespacing: move HTTP utilities along \Shaarli\Http\ classes
Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests/http/HttpUtils')
-rw-r--r--tests/http/HttpUtils/ClientIpIdTest.php54
-rw-r--r--tests/http/HttpUtils/GetHttpUrlTest.php67
-rw-r--r--tests/http/HttpUtils/GetIpAdressFromProxyTest.php61
-rw-r--r--tests/http/HttpUtils/IndexUrlTest.php74
-rw-r--r--tests/http/HttpUtils/IsHttpsTest.php39
-rw-r--r--tests/http/HttpUtils/PageUrlTest.php78
-rw-r--r--tests/http/HttpUtils/ServerUrlTest.php223
7 files changed, 596 insertions, 0 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
6namespace Shaarli\Http;
7
8require_once 'application/http/HttpUtils.php';
9
10/**
11 * Unitary tests for client_ip_id()
12 */
13class 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
6namespace Shaarli\Http;
7
8require_once 'application/http/HttpUtils.php';
9
10/**
11 * Unitary tests for get_http_response()
12 */
13class 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
3namespace Shaarli\Http;
4
5require_once 'application/http/HttpUtils.php';
6
7/**
8 * Unitary tests for getIpAddressFromProxy()
9 */
10class 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
6namespace Shaarli\Http;
7
8require_once 'application/http/HttpUtils.php';
9
10/**
11 * Unitary tests for index_url()
12 */
13class 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
3namespace Shaarli\Http;
4
5require_once 'application/http/HttpUtils.php';
6
7/**
8 * Class IsHttpsTest
9 *
10 * Test class for is_https() function.
11 */
12class 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
6namespace Shaarli\Http;
7
8require_once 'application/http/HttpUtils.php';
9
10/**
11 * Unitary tests for page_url()
12 */
13class 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
6namespace Shaarli\Http;
7
8require_once 'application/http/HttpUtils.php';
9
10/**
11 * Unitary tests for server_url()
12 */
13class 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}