aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/HttpUtils
diff options
context:
space:
mode:
Diffstat (limited to 'tests/HttpUtils')
-rw-r--r--tests/HttpUtils/ClientIpIdTest.php52
-rw-r--r--tests/HttpUtils/GetHttpUrlTest.php65
-rw-r--r--tests/HttpUtils/GetIpAdressFromProxyTest.php59
-rw-r--r--tests/HttpUtils/IndexUrlTest.php72
-rw-r--r--tests/HttpUtils/IsHttpsTest.php36
-rw-r--r--tests/HttpUtils/PageUrlTest.php76
-rw-r--r--tests/HttpUtils/ServerUrlTest.php221
7 files changed, 0 insertions, 581 deletions
diff --git a/tests/HttpUtils/ClientIpIdTest.php b/tests/HttpUtils/ClientIpIdTest.php
deleted file mode 100644
index c15ac5cc..00000000
--- a/tests/HttpUtils/ClientIpIdTest.php
+++ /dev/null
@@ -1,52 +0,0 @@
1<?php
2/**
3 * HttpUtils' tests
4 */
5
6require_once 'application/HttpUtils.php';
7
8/**
9 * Unitary tests for client_ip_id()
10 */
11class ClientIpIdTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * Get a remote client ID based on its IP
15 */
16 public function testClientIpIdRemote()
17 {
18 $this->assertEquals(
19 '10.1.167.42',
20 client_ip_id(['REMOTE_ADDR' => '10.1.167.42'])
21 );
22 }
23
24 /**
25 * Get a remote client ID based on its IP and proxy information (1)
26 */
27 public function testClientIpIdRemoteForwarded()
28 {
29 $this->assertEquals(
30 '10.1.167.42_127.0.1.47',
31 client_ip_id([
32 'REMOTE_ADDR' => '10.1.167.42',
33 'HTTP_X_FORWARDED_FOR' => '127.0.1.47'
34 ])
35 );
36 }
37
38 /**
39 * Get a remote client ID based on its IP and proxy information (2)
40 */
41 public function testClientIpIdRemoteForwardedClient()
42 {
43 $this->assertEquals(
44 '10.1.167.42_10.1.167.56_127.0.1.47',
45 client_ip_id([
46 'REMOTE_ADDR' => '10.1.167.42',
47 'HTTP_X_FORWARDED_FOR' => '10.1.167.56',
48 'HTTP_CLIENT_IP' => '127.0.1.47'
49 ])
50 );
51 }
52}
diff --git a/tests/HttpUtils/GetHttpUrlTest.php b/tests/HttpUtils/GetHttpUrlTest.php
deleted file mode 100644
index ea53de5f..00000000
--- a/tests/HttpUtils/GetHttpUrlTest.php
+++ /dev/null
@@ -1,65 +0,0 @@
1<?php
2/**
3 * HttpUtils' tests
4 */
5
6require_once 'application/HttpUtils.php';
7
8/**
9 * Unitary tests for get_http_response()
10 */
11class GetHttpUrlTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * Get an invalid local URL
15 */
16 public function testGetInvalidLocalUrl()
17 {
18 // Local
19 list($headers, $content) = get_http_response('/non/existent', 1);
20 $this->assertEquals('Invalid HTTP Url', $headers[0]);
21 $this->assertFalse($content);
22
23 // Non HTTP
24 list($headers, $content) = get_http_response('ftp://save.tld/mysave', 1);
25 $this->assertEquals('Invalid HTTP Url', $headers[0]);
26 $this->assertFalse($content);
27 }
28
29 /**
30 * Get an invalid remote URL
31 */
32 public function testGetInvalidRemoteUrl()
33 {
34 list($headers, $content) = @get_http_response('http://non.existent', 1);
35 $this->assertFalse($headers);
36 $this->assertFalse($content);
37 }
38
39 /**
40 * Test getAbsoluteUrl with relative target URL.
41 */
42 public function testGetAbsoluteUrlWithRelative()
43 {
44 $origin = 'http://non.existent/blabla/?test';
45 $target = '/stuff.php';
46
47 $expected = 'http://non.existent/stuff.php';
48 $this->assertEquals($expected, getAbsoluteUrl($origin, $target));
49
50 $target = 'stuff.php';
51 $expected = 'http://non.existent/blabla/stuff.php';
52 $this->assertEquals($expected, getAbsoluteUrl($origin, $target));
53 }
54
55 /**
56 * Test getAbsoluteUrl with absolute target URL.
57 */
58 public function testGetAbsoluteUrlWithAbsolute()
59 {
60 $origin = 'http://non.existent/blabla/?test';
61 $target = 'http://other.url/stuff.php';
62
63 $this->assertEquals($target, getAbsoluteUrl($origin, $target));
64 }
65}
diff --git a/tests/HttpUtils/GetIpAdressFromProxyTest.php b/tests/HttpUtils/GetIpAdressFromProxyTest.php
deleted file mode 100644
index 7af5bd9d..00000000
--- a/tests/HttpUtils/GetIpAdressFromProxyTest.php
+++ /dev/null
@@ -1,59 +0,0 @@
1<?php
2
3require_once 'application/HttpUtils.php';
4
5/**
6 * Unitary tests for getIpAddressFromProxy()
7 */
8class GetIpAdressFromProxyTest extends PHPUnit_Framework_TestCase
9{
10
11 /**
12 * Test without proxy
13 */
14 public function testWithoutProxy()
15 {
16 $this->assertFalse(getIpAddressFromProxy(array(), array()));
17 }
18
19 /**
20 * Test with a single IP in proxy header.
21 */
22 public function testWithOneForwardedIp()
23 {
24 $ip = '1.1.1.1';
25 $server = array('HTTP_X_FORWARDED_FOR' => $ip);
26 $this->assertEquals($ip, getIpAddressFromProxy($server, array()));
27 }
28
29 /**
30 * Test with a multiple IPs in proxy header.
31 */
32 public function testWithMultipleForwardedIp()
33 {
34 $ip = '1.1.1.1';
35 $ip2 = '2.2.2.2';
36
37 $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2);
38 $this->assertEquals($ip2, getIpAddressFromProxy($server, array()));
39
40 $server = array('HTTP_X_FORWARDED_FOR' => $ip .' , '. $ip2);
41 $this->assertEquals($ip2, getIpAddressFromProxy($server, array()));
42 }
43
44 /**
45 * Test with a trusted IP address.
46 */
47 public function testWithTrustedIp()
48 {
49 $ip = '1.1.1.1';
50 $ip2 = '2.2.2.2';
51
52 $server = array('HTTP_X_FORWARDED_FOR' => $ip);
53 $this->assertFalse(getIpAddressFromProxy($server, array($ip)));
54
55 $server = array('HTTP_X_FORWARDED_FOR' => $ip .','. $ip2);
56 $this->assertEquals($ip2, getIpAddressFromProxy($server, array($ip)));
57 $this->assertFalse(getIpAddressFromProxy($server, array($ip, $ip2)));
58 }
59}
diff --git a/tests/HttpUtils/IndexUrlTest.php b/tests/HttpUtils/IndexUrlTest.php
deleted file mode 100644
index 337dcab0..00000000
--- a/tests/HttpUtils/IndexUrlTest.php
+++ /dev/null
@@ -1,72 +0,0 @@
1<?php
2/**
3 * HttpUtils' tests
4 */
5
6require_once 'application/HttpUtils.php';
7
8/**
9 * Unitary tests for index_url()
10 */
11class IndexUrlTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * If on the main page, remove "index.php" from the URL resource
15 */
16 public function testRemoveIndex()
17 {
18 $this->assertEquals(
19 'http://host.tld/',
20 index_url(
21 array(
22 'HTTPS' => 'Off',
23 'SERVER_NAME' => 'host.tld',
24 'SERVER_PORT' => '80',
25 'SCRIPT_NAME' => '/index.php'
26 )
27 )
28 );
29
30 $this->assertEquals(
31 'http://host.tld/admin/',
32 index_url(
33 array(
34 'HTTPS' => 'Off',
35 'SERVER_NAME' => 'host.tld',
36 'SERVER_PORT' => '80',
37 'SCRIPT_NAME' => '/admin/index.php'
38 )
39 )
40 );
41 }
42
43 /**
44 * The resource is != "index.php"
45 */
46 public function testOtherResource()
47 {
48 $this->assertEquals(
49 'http://host.tld/page.php',
50 page_url(
51 array(
52 'HTTPS' => 'Off',
53 'SERVER_NAME' => 'host.tld',
54 'SERVER_PORT' => '80',
55 'SCRIPT_NAME' => '/page.php'
56 )
57 )
58 );
59
60 $this->assertEquals(
61 'http://host.tld/admin/page.php',
62 page_url(
63 array(
64 'HTTPS' => 'Off',
65 'SERVER_NAME' => 'host.tld',
66 'SERVER_PORT' => '80',
67 'SCRIPT_NAME' => '/admin/page.php'
68 )
69 )
70 );
71 }
72}
diff --git a/tests/HttpUtils/IsHttpsTest.php b/tests/HttpUtils/IsHttpsTest.php
deleted file mode 100644
index 097f2bcf..00000000
--- a/tests/HttpUtils/IsHttpsTest.php
+++ /dev/null
@@ -1,36 +0,0 @@
1<?php
2
3
4/**
5 * Class IsHttpsTest
6 *
7 * Test class for is_https() function.
8 */
9class IsHttpsTest extends PHPUnit_Framework_TestCase
10{
11
12 /**
13 * Test is_https with HTTPS values.
14 */
15 public function testIsHttpsTrue()
16 {
17 $this->assertTrue(is_https(['HTTPS' => true]));
18 $this->assertTrue(is_https(['HTTPS' => '1']));
19 $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 443]));
20 $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443']));
21 $this->assertTrue(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '443,123,456,']));
22 }
23
24 /**
25 * Test is_https with HTTP values.
26 */
27 public function testIsHttpsFalse()
28 {
29 $this->assertFalse(is_https([]));
30 $this->assertFalse(is_https(['HTTPS' => false]));
31 $this->assertFalse(is_https(['HTTPS' => '0']));
32 $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => 123]));
33 $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => '123']));
34 $this->assertFalse(is_https(['HTTPS' => false, 'HTTP_X_FORWARDED_PORT' => ',123,456,']));
35 }
36}
diff --git a/tests/HttpUtils/PageUrlTest.php b/tests/HttpUtils/PageUrlTest.php
deleted file mode 100644
index 4dbbe9cf..00000000
--- a/tests/HttpUtils/PageUrlTest.php
+++ /dev/null
@@ -1,76 +0,0 @@
1<?php
2/**
3 * HttpUtils' tests
4 */
5
6require_once 'application/HttpUtils.php';
7
8/**
9 * Unitary tests for page_url()
10 */
11class PageUrlTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * If on the main page, remove "index.php" from the URL resource
15 */
16 public function testRemoveIndex()
17 {
18 $this->assertEquals(
19 'http://host.tld/?p1=v1&p2=v2',
20 page_url(
21 array(
22 'HTTPS' => 'Off',
23 'SERVER_NAME' => 'host.tld',
24 'SERVER_PORT' => '80',
25 'SCRIPT_NAME' => '/index.php',
26 'QUERY_STRING' => 'p1=v1&p2=v2'
27 )
28 )
29 );
30
31 $this->assertEquals(
32 'http://host.tld/admin/?action=edit_tag',
33 page_url(
34 array(
35 'HTTPS' => 'Off',
36 'SERVER_NAME' => 'host.tld',
37 'SERVER_PORT' => '80',
38 'SCRIPT_NAME' => '/admin/index.php',
39 'QUERY_STRING' => 'action=edit_tag'
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?p1=v1&p2=v2',
52 page_url(
53 array(
54 'HTTPS' => 'Off',
55 'SERVER_NAME' => 'host.tld',
56 'SERVER_PORT' => '80',
57 'SCRIPT_NAME' => '/page.php',
58 'QUERY_STRING' => 'p1=v1&p2=v2'
59 )
60 )
61 );
62
63 $this->assertEquals(
64 'http://host.tld/admin/page.php?action=edit_tag',
65 page_url(
66 array(
67 'HTTPS' => 'Off',
68 'SERVER_NAME' => 'host.tld',
69 'SERVER_PORT' => '80',
70 'SCRIPT_NAME' => '/admin/page.php',
71 'QUERY_STRING' => 'action=edit_tag'
72 )
73 )
74 );
75 }
76}
diff --git a/tests/HttpUtils/ServerUrlTest.php b/tests/HttpUtils/ServerUrlTest.php
deleted file mode 100644
index 324b827a..00000000
--- a/tests/HttpUtils/ServerUrlTest.php
+++ /dev/null
@@ -1,221 +0,0 @@
1<?php
2/**
3 * HttpUtils' tests
4 */
5
6require_once 'application/HttpUtils.php';
7
8/**
9 * Unitary tests for server_url()
10 */
11class ServerUrlTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * Detect if the server uses SSL
15 */
16 public function testHttpsScheme()
17 {
18 $this->assertEquals(
19 'https://host.tld',
20 server_url(
21 array(
22 'HTTPS' => 'ON',
23 'SERVER_NAME' => 'host.tld',
24 'SERVER_PORT' => '443'
25 )
26 )
27 );
28
29 $this->assertEquals(
30 'https://host.tld:8080',
31 server_url(
32 array(
33 'HTTPS' => 'ON',
34 'SERVER_NAME' => 'host.tld',
35 'SERVER_PORT' => '8080'
36 )
37 )
38 );
39 }
40
41 /**
42 * Detect a Proxy that sets Forwarded-Host
43 */
44 public function testHttpsProxyForwardedHost()
45 {
46 $this->assertEquals(
47 'https://host.tld:8080',
48 server_url(
49 array(
50 'HTTP_X_FORWARDED_PROTO' => 'https',
51 'HTTP_X_FORWARDED_PORT' => '8080',
52 'HTTP_X_FORWARDED_HOST' => 'host.tld'
53 )
54 )
55 );
56
57 $this->assertEquals(
58 'https://host.tld:4974',
59 server_url(
60 array(
61 'HTTP_X_FORWARDED_PROTO' => 'https, https',
62 'HTTP_X_FORWARDED_PORT' => '4974, 80',
63 'HTTP_X_FORWARDED_HOST' => 'host.tld, example.com'
64 )
65 )
66 );
67 }
68
69 /**
70 * Detect a Proxy with SSL enabled
71 */
72 public function testHttpsProxyForward()
73 {
74 $this->assertEquals(
75 'https://host.tld:8080',
76 server_url(
77 array(
78 'HTTPS' => 'Off',
79 'SERVER_NAME' => 'host.tld',
80 'SERVER_PORT' => '80',
81 'HTTP_X_FORWARDED_PROTO' => 'https',
82 'HTTP_X_FORWARDED_PORT' => '8080'
83 )
84 )
85 );
86
87 $this->assertEquals(
88 'https://host.tld',
89 server_url(
90 array(
91 'HTTPS' => 'Off',
92 'SERVER_NAME' => 'host.tld',
93 'SERVER_PORT' => '80',
94 'HTTP_X_FORWARDED_PROTO' => 'https'
95 )
96 )
97 );
98
99 $this->assertEquals(
100 'https://host.tld',
101 server_url(
102 array(
103 'HTTPS' => 'Off',
104 'SERVER_NAME' => 'host.tld',
105 'SERVER_PORT' => '80',
106 'HTTP_X_FORWARDED_PROTO' => 'https',
107 'HTTP_X_FORWARDED_PORT' => '443'
108 )
109 )
110 );
111
112 $this->assertEquals(
113 'https://host.tld:4974',
114 server_url(
115 array(
116 'HTTPS' => 'Off',
117 'SERVER_NAME' => 'host.tld',
118 'SERVER_PORT' => '80',
119 'HTTP_X_FORWARDED_PROTO' => 'https, https',
120 'HTTP_X_FORWARDED_PORT' => '4974, 80'
121 )
122 )
123 );
124 }
125
126 /**
127 * Detect if the server uses a specific port (!= 80)
128 */
129 public function testPort()
130 {
131 // HTTP
132 $this->assertEquals(
133 'http://host.tld:8080',
134 server_url(
135 array(
136 'HTTPS' => 'OFF',
137 'SERVER_NAME' => 'host.tld',
138 'SERVER_PORT' => '8080'
139 )
140 )
141 );
142
143 // HTTPS
144 $this->assertEquals(
145 'https://host.tld:8080',
146 server_url(
147 array(
148 'HTTPS' => 'ON',
149 'SERVER_NAME' => 'host.tld',
150 'SERVER_PORT' => '8080'
151 )
152 )
153 );
154 }
155
156 /**
157 * HTTP server on port 80
158 */
159 public function testStandardHttpPort()
160 {
161 $this->assertEquals(
162 'http://host.tld',
163 server_url(
164 array(
165 'HTTPS' => 'OFF',
166 'SERVER_NAME' => 'host.tld',
167 'SERVER_PORT' => '80'
168 )
169 )
170 );
171 }
172
173 /**
174 * HTTPS server on port 443
175 */
176 public function testStandardHttpsPort()
177 {
178 $this->assertEquals(
179 'https://host.tld',
180 server_url(
181 array(
182 'HTTPS' => 'ON',
183 'SERVER_NAME' => 'host.tld',
184 'SERVER_PORT' => '443'
185 )
186 )
187 );
188 }
189
190 /**
191 * Misconfigured server (see #1022): Proxy HTTP but 443
192 */
193 public function testHttpWithPort433()
194 {
195 $this->assertEquals(
196 'https://host.tld',
197 server_url(
198 array(
199 'HTTPS' => 'Off',
200 'SERVER_NAME' => 'host.tld',
201 'SERVER_PORT' => '80',
202 'HTTP_X_FORWARDED_PROTO' => 'http',
203 'HTTP_X_FORWARDED_PORT' => '443'
204 )
205 )
206 );
207
208 $this->assertEquals(
209 'https://host.tld',
210 server_url(
211 array(
212 'HTTPS' => 'Off',
213 'SERVER_NAME' => 'host.tld',
214 'SERVER_PORT' => '80',
215 'HTTP_X_FORWARDED_PROTO' => 'https, http',
216 'HTTP_X_FORWARDED_PORT' => '443, 80'
217 )
218 )
219 );
220 }
221}