aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/HttpUtils
diff options
context:
space:
mode:
authorVirtualTam <virtualtam@flibidi.net>2015-09-06 21:31:37 +0200
committerVirtualTam <virtualtam@flibidi.net>2015-09-14 20:27:16 +0200
commit482d67bd523bf12f36508a0131d09fe299ee02f4 (patch)
treeb4e7c6ddaa1d88cc49bc96c2524f12431d8fcce0 /tests/HttpUtils
parent7b114771d337af3bfd51d8fda1e8f2fd5b39535d (diff)
downloadShaarli-482d67bd523bf12f36508a0131d09fe299ee02f4.tar.gz
Shaarli-482d67bd523bf12f36508a0131d09fe299ee02f4.tar.zst
Shaarli-482d67bd523bf12f36508a0131d09fe299ee02f4.zip
HTTP: move server URL functions to `HttpUtils.php`
Relates to #333 Modifications: - refactor server URL utility functions - do not access global `$_SERVER` variables - add test coverage - improve readability - apply coding conventions Signed-off-by: VirtualTam <virtualtam@flibidi.net>
Diffstat (limited to 'tests/HttpUtils')
-rw-r--r--tests/HttpUtils/GetHttpUrlTest.php38
-rw-r--r--tests/HttpUtils/IndexUrlTest.php72
-rw-r--r--tests/HttpUtils/PageUrlTest.php76
-rw-r--r--tests/HttpUtils/ServerUrlTest.php135
4 files changed, 321 insertions, 0 deletions
diff --git a/tests/HttpUtils/GetHttpUrlTest.php b/tests/HttpUtils/GetHttpUrlTest.php
new file mode 100644
index 00000000..76092b80
--- /dev/null
+++ b/tests/HttpUtils/GetHttpUrlTest.php
@@ -0,0 +1,38 @@
1<?php
2/**
3 * HttpUtils' tests
4 */
5
6require_once 'application/HttpUtils.php';
7
8/**
9 * Unitary tests for get_http_url()
10 */
11class GetHttpUrlTest extends PHPUnit_Framework_TestCase
12{
13 /**
14 * Get an invalid local URL
15 */
16 public function testGetInvalidLocalUrl()
17 {
18 list($headers, $content) = get_http_url('/non/existent', 1);
19 $this->assertEquals('HTTP Error', $headers[0]);
20 $this->assertRegexp(
21 '/failed to open stream: No such file or directory/',
22 $content
23 );
24 }
25
26 /**
27 * Get an invalid remote URL
28 */
29 public function testGetInvalidRemoteUrl()
30 {
31 list($headers, $content) = get_http_url('http://non.existent', 1);
32 $this->assertEquals('HTTP Error', $headers[0]);
33 $this->assertRegexp(
34 '/Name or service not known/',
35 $content
36 );
37 }
38}
diff --git a/tests/HttpUtils/IndexUrlTest.php b/tests/HttpUtils/IndexUrlTest.php
new file mode 100644
index 00000000..337dcab0
--- /dev/null
+++ b/tests/HttpUtils/IndexUrlTest.php
@@ -0,0 +1,72 @@
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/PageUrlTest.php b/tests/HttpUtils/PageUrlTest.php
new file mode 100644
index 00000000..4dbbe9cf
--- /dev/null
+++ b/tests/HttpUtils/PageUrlTest.php
@@ -0,0 +1,76 @@
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
new file mode 100644
index 00000000..5096db65
--- /dev/null
+++ b/tests/HttpUtils/ServerUrlTest.php
@@ -0,0 +1,135 @@
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 with SSL enabled
43 */
44 public function testHttpsProxyForward()
45 {
46 $this->assertEquals(
47 'https://host.tld:8080',
48 server_url(
49 array(
50 'HTTPS' => 'Off',
51 'SERVER_NAME' => 'host.tld',
52 'SERVER_PORT' => '80',
53 'HTTP_X_FORWARDED_PROTO' => 'https',
54 'HTTP_X_FORWARDED_PORT' => '8080'
55 )
56 )
57 );
58
59 $this->assertEquals(
60 'https://host.tld',
61 server_url(
62 array(
63 'HTTPS' => 'Off',
64 'SERVER_NAME' => 'host.tld',
65 'SERVER_PORT' => '80',
66 'HTTP_X_FORWARDED_PROTO' => 'https'
67 )
68 )
69 );
70 }
71
72 /**
73 * Detect if the server uses a specific port (!= 80)
74 */
75 public function testPort()
76 {
77 // HTTP
78 $this->assertEquals(
79 'http://host.tld:8080',
80 server_url(
81 array(
82 'HTTPS' => 'OFF',
83 'SERVER_NAME' => 'host.tld',
84 'SERVER_PORT' => '8080'
85 )
86 )
87 );
88
89 // HTTPS
90 $this->assertEquals(
91 'https://host.tld:8080',
92 server_url(
93 array(
94 'HTTPS' => 'ON',
95 'SERVER_NAME' => 'host.tld',
96 'SERVER_PORT' => '8080'
97 )
98 )
99 );
100 }
101
102 /**
103 * HTTP server on port 80
104 */
105 public function testStandardHttpPort()
106 {
107 $this->assertEquals(
108 'http://host.tld',
109 server_url(
110 array(
111 'HTTPS' => 'OFF',
112 'SERVER_NAME' => 'host.tld',
113 'SERVER_PORT' => '80'
114 )
115 )
116 );
117 }
118
119 /**
120 * HTTPS server on port 443
121 */
122 public function testStandardHttpsPort()
123 {
124 $this->assertEquals(
125 'https://host.tld',
126 server_url(
127 array(
128 'HTTPS' => 'ON',
129 'SERVER_NAME' => 'host.tld',
130 'SERVER_PORT' => '443'
131 )
132 )
133 );
134 }
135}