diff options
author | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:07:13 +0200 |
---|---|---|
committer | ArthurHoaro <arthur@hoa.ro> | 2020-10-13 12:07:13 +0200 |
commit | d9f6275ebca035fec8331652c677981056793ccc (patch) | |
tree | 37a64baf4f0eba6b781040605965383d8aded2cc /tests/http/HttpUtils/ServerUrlTest.php | |
parent | 38672ba0d1c722e5d6d33a58255ceb55e9410e46 (diff) | |
parent | d63ff87a009313141ae684ec447b902562ff6ee7 (diff) | |
download | Shaarli-d9f6275ebca035fec8331652c677981056793ccc.tar.gz Shaarli-d9f6275ebca035fec8331652c677981056793ccc.tar.zst Shaarli-d9f6275ebca035fec8331652c677981056793ccc.zip |
Merge branch 'v0.11' into stablestable
Diffstat (limited to 'tests/http/HttpUtils/ServerUrlTest.php')
-rw-r--r-- | tests/http/HttpUtils/ServerUrlTest.php | 223 |
1 files changed, 223 insertions, 0 deletions
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 | } | ||