[FEATURE] Add normalized server parameters to PSR-7 requests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Http / NormalizedParamsTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Core\Tests\Unit\Http;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use Psr\Http\Message\ServerRequestInterface;
19 use TYPO3\CMS\Core\Http\NormalizedParams;
20 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
21
22 /**
23 * Test case
24 */
25 class NormalizedParamsTest extends UnitTestCase
26 {
27 /**
28 * @return array[]
29 */
30 public function getHttpHostReturnsSanitizedValueDataProvider(): array
31 {
32 return [
33 'simple HTTP_HOST' => [
34 [
35 'HTTP_HOST' => 'www.domain.com'
36 ],
37 [],
38 'www.domain.com'
39 ],
40 'first HTTP_X_FORWARDED_HOST from configured proxy' => [
41 [
42 'HTTP_HOST' => '',
43 'REMOTE_ADDR' => '123.123.123.123',
44 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com, www.domain2.com,'
45 ],
46 [
47 'SYS' => [
48 'reverseProxyIP' => ' 123.123.123.123',
49 'reverseProxyHeaderMultiValue' => 'first',
50 ]
51 ],
52 'www.domain1.com',
53 ],
54 'last HTTP_X_FORWARDED_HOST from configured proxy' => [
55 [
56 'HTTP_HOST' => '',
57 'REMOTE_ADDR' => '123.123.123.123',
58 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com, www.domain2.com,'
59 ],
60 [
61 'SYS' => [
62 'reverseProxyIP' => '123.123.123.123',
63 'reverseProxyHeaderMultiValue' => 'last',
64 ]
65 ],
66 'www.domain2.com',
67 ],
68 'simple HTTP_HOST if reverseProxyHeaderMultiValue is not configured' => [
69 [
70 'HTTP_HOST' => 'www.domain.com',
71 'REMOTE_ADDR' => '123.123.123.123',
72 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com'
73 ],
74 [
75 'SYS' => [
76 'reverseProxyIP' => '123.123.123.123',
77 ]
78 ],
79 'www.domain.com',
80 ],
81 'simple HTTP_HOST if proxy IP does not match' => [
82 [
83 'HTTP_HOST' => 'www.domain.com',
84 'REMOTE_ADDR' => '123.123.123.123',
85 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com'
86 ],
87 [
88 'SYS' => [
89 'reverseProxyIP' => '234.234.234.234',
90 'reverseProxyHeaderMultiValue' => 'last',
91 ]
92 ],
93 'www.domain.com',
94 ],
95 'simple HTTP_HOST if REMOTE_ADDR misses' => [
96 [
97 'HTTP_HOST' => 'www.domain.com',
98 'HTTP_X_FORWARDED_HOST' => 'www.domain1.com'
99 ],
100 [
101 'SYS' => [
102 'reverseProxyIP' => '234.234.234.234',
103 'reverseProxyHeaderMultiValue' => 'last',
104 ]
105 ],
106 'www.domain.com',
107 ],
108 'simple HTTP_HOST if HTTP_X_FORWARDED_HOST is empty' => [
109 [
110 'HTTP_HOST' => 'www.domain.com',
111 'REMOTE_ADDR' => '123.123.123.123',
112 'HTTP_X_FORWARDED_HOST' => ''
113 ],
114 [
115 'SYS' => [
116 'reverseProxyIP' => '123.123.123.123',
117 'reverseProxyHeaderMultiValue' => 'last',
118 ]
119 ],
120 'www.domain.com',
121 ],
122 ];
123 }
124
125 /**
126 * @test
127 * @dataProvider getHttpHostReturnsSanitizedValueDataProvider
128 * @param array $serverParams
129 * @param array $typo3ConfVars
130 * @param string $expected
131 */
132 public function getHttpHostReturnsSanitizedValue(array $serverParams, array $typo3ConfVars, string $expected)
133 {
134 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
135 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
136 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
137 $this->assertSame($expected, $serverRequestParameters->getHttpHost());
138 }
139
140 /**
141 * @return array[]
142 */
143 public function isHttpsReturnSanitizedValueDataProvider(): array
144 {
145 return [
146 'false if nothing special is set' => [
147 [
148 'HTTP_HOST' => 'www.domain.com',
149 ],
150 [],
151 false
152 ],
153 'true if SSL_SESSION_ID is set' => [
154 [
155 'HTTP_HOST' => 'www.domain.com',
156 'SSL_SESSION_ID' => 'foo',
157 ],
158 [],
159 true
160 ],
161 'false if SSL_SESSION_ID is empty' => [
162 [
163 'HTTP_HOST' => 'www.domain.com',
164 'SSL_SESSION_ID' => '',
165 ],
166 [],
167 false
168 ],
169 'true if HTTPS is "ON"' => [
170 [
171 'HTTP_HOST' => 'www.domain.com',
172 'HTTPS' => 'ON',
173 ],
174 [],
175 true,
176 ],
177 'true if HTTPS is "on"' => [
178 [
179 'HTTP_HOST' => 'www.domain.com',
180 'HTTPS' => 'on',
181 ],
182 [],
183 true,
184 ],
185 'true if HTTPS is "1"' => [
186 [
187 'HTTP_HOST' => 'www.domain.com',
188 'HTTPS' => '1',
189 ],
190 [],
191 true,
192 ],
193 'false if HTTPS is "0"' => [
194 [
195 'HTTP_HOST' => 'www.domain.com',
196 'HTTPS' => '0',
197 ],
198 [],
199 false,
200 ],
201 'false if HTTPS is not on' => [
202 [
203 'HTTP_HOST' => 'www.domain.com',
204 'HTTPS' => 'off',
205 ],
206 [],
207 false,
208 ],
209 'false if HTTPS is empty' => [
210 [
211 'HTTP_HOST' => 'www.domain.com',
212 'HTTPS' => '',
213 ],
214 [],
215 false,
216 ],
217 'true if ssl proxy IP matches REMOTE_ADDR' => [
218 [
219 'HTTP_HOST' => 'www.domain.com',
220 'REMOTE_ADDR' => '123.123.123.123 ',
221 ],
222 [
223 'SYS' => [
224 'reverseProxySSL' => ' 123.123.123.123',
225 ],
226 ],
227 true
228 ],
229 'false if ssl proxy IP does not match REMOTE_ADDR' => [
230 [
231 'HTTP_HOST' => 'www.domain.com',
232 'REMOTE_ADDR' => '123.123.123.123',
233 ],
234 [
235 'SYS' => [
236 'reverseProxySSL' => '234.234.234.234',
237 ],
238 ],
239 false
240 ],
241 'true if SSL proxy is * and reverse proxy IP matches REMOTE_ADDR' => [
242 [
243 'HTTP_HOST' => 'www.domain.com',
244 'REMOTE_ADDR' => '123.123.123.123',
245 ],
246 [
247 'SYS' => [
248 'reverseProxySSL' => '*',
249 'reverseProxyIP' => '123.123.123.123',
250 ],
251 ],
252 true
253 ],
254 'false if SSL proxy is * and reverse proxy IP does not match REMOTE_ADDR' => [
255 [
256 'HTTP_HOST' => 'www.domain.com',
257 'REMOTE_ADDR' => '123.123.123.123',
258 ],
259 [
260 'SYS' => [
261 'reverseProxySSL' => '*',
262 'reverseProxyIP' => '234.234.234.234',
263 ],
264 ],
265 false
266 ]
267 ];
268 }
269
270 /**
271 * @test
272 * @dataProvider isHttpsReturnSanitizedValueDataProvider
273 * @param array $serverParams
274 * @param array $typo3ConfVars
275 * @param bool $expected
276 */
277 public function isHttpsReturnSanitizedValue(array $serverParams, array $typo3ConfVars, bool $expected)
278 {
279 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
280 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
281 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
282 $this->assertSame($expected, $serverRequestParameters->isHttps());
283 }
284
285 /**
286 * @test
287 */
288 public function getRequestHostReturnsRequestHost()
289 {
290 $serverParams = [
291 'HTTP_HOST' => 'www.domain.com',
292 'HTTPS' => 'on',
293 ];
294 $expected = 'https://www.domain.com';
295 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
296 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
297 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
298 $this->assertSame($expected, $serverRequestParameters->getRequestHost());
299 }
300
301 /**
302 * @return array[]
303 */
304 public function getScriptNameReturnsExpectedValueDataProvider(): array
305 {
306 return [
307 'empty string if nothing is set' => [
308 [
309 'HTTP_HOST' => 'www.domain.com',
310 ],
311 [],
312 ''
313 ],
314 'use ORIG_PATH_INFO' => [
315 [
316 'ORIG_PATH_INFO' => '/orig/path/info.php',
317 'PATH_INFO' => '/path/info.php',
318 'ORIG_SCRIPT_NAME' => '/orig/script/name.php',
319 'SCRIPT_NAME' => '/script/name.php',
320 ],
321 [],
322 '/orig/path/info.php',
323 ],
324 'use PATH_INFO' => [
325 [
326 'PATH_INFO' => '/path/info.php',
327 'ORIG_SCRIPT_NAME' => '/orig/script/name.php',
328 'SCRIPT_NAME' => '/script/name.php',
329 ],
330 [],
331 '/path/info.php',
332 ],
333 'use ORIG_SCRIPT_NAME' => [
334 [
335 'ORIG_SCRIPT_NAME' => '/orig/script/name.php',
336 'SCRIPT_NAME' => '/script/name.php',
337 ],
338 [],
339 '/orig/script/name.php',
340 ],
341 'use SCRIPT_NAME' => [
342 [
343 'SCRIPT_NAME' => '/script/name.php',
344 ],
345 [],
346 '/script/name.php',
347 ],
348 'add proxy ssl prefix' => [
349 [
350 'REMOTE_ADDR' => '123.123.123.123',
351 'HTTPS' => 'on',
352 'PATH_INFO' => '/path/info.php',
353 ],
354 [
355 'SYS' => [
356 'reverseProxyIP' => '123.123.123.123',
357 'reverseProxyPrefixSSL' => '/proxyPrefixSSL',
358 ],
359 ],
360 '/proxyPrefixSSL/path/info.php',
361 ],
362 'add proxy prefix' => [
363 [
364 'REMOTE_ADDR' => '123.123.123.123',
365 'PATH_INFO' => '/path/info.php',
366 ],
367 [
368 'SYS' => [
369 'reverseProxyIP' => '123.123.123.123',
370 'reverseProxyPrefix' => '/proxyPrefix',
371 ],
372 ],
373 '/proxyPrefix/path/info.php',
374 ],
375 ];
376 }
377
378 /**
379 * @test
380 * @dataProvider getScriptNameReturnsExpectedValueDataProvider
381 * @param array $serverParams
382 * @param array $typo3ConfVars
383 * @param string $expected
384 */
385 public function getScriptNameReturnsExpectedValue(array $serverParams, array $typo3ConfVars, string $expected)
386 {
387 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
388 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
389 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
390 $this->assertSame($expected, $serverRequestParameters->getScriptName());
391 }
392
393 /**
394 * @return array[]
395 */
396 public function getRequestUriReturnsExpectedValueDataProvider(): array
397 {
398 return [
399 'slash if nothing is set' => [
400 [
401 'HTTP_HOST' => 'www.domain.com',
402 ],
403 [],
404 '/'
405 ],
406 'use REQUEST_URI' => [
407 [
408 'HTTP_HOST' => 'www.domain.com',
409 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
410 ],
411 [],
412 '/typo3/index.php?route=foo/bar&id=42',
413 ],
414 'use query string and script name if REQUEST_URI is not set' => [
415 [
416 'QUERY_STRING' => 'route=foo/bar&id=42',
417 'SCRIPT_NAME' => '/typo3/index.php',
418 ],
419 [],
420 '/typo3/index.php?route=foo/bar&id=42',
421 ],
422 'prefix with proxy prefix with ssl if using REQUEST_URI' => [
423 [
424 'HTTP_HOST' => 'www.domain.com',
425 'REMOTE_ADDR' => '123.123.123.123',
426 'HTTPS' => 'on',
427 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
428 ],
429 [
430 'SYS' => [
431 'reverseProxyIP' => '123.123.123.123',
432 'reverseProxyPrefixSSL' => '/proxyPrefixSSL',
433 ],
434 ],
435 '/proxyPrefixSSL/typo3/index.php?route=foo/bar&id=42',
436 ],
437 'prefix with proxy prefix if using REQUEST_URI' => [
438 [
439 'HTTP_HOST' => 'www.domain.com',
440 'REMOTE_ADDR' => '123.123.123.123',
441 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
442 ],
443 [
444 'SYS' => [
445 'reverseProxyIP' => '123.123.123.123',
446 'reverseProxyPrefix' => '/proxyPrefix',
447 ],
448 ],
449 '/proxyPrefix/typo3/index.php?route=foo/bar&id=42',
450 ],
451 'prefix with proxy prefix with ssl if using query string and script name' => [
452 [
453 'REMOTE_ADDR' => '123.123.123.123',
454 'HTTPS' => 'on',
455 'QUERY_STRING' => 'route=foo/bar&id=42',
456 'SCRIPT_NAME' => '/typo3/index.php',
457 ],
458 [
459 'SYS' => [
460 'reverseProxyIP' => '123.123.123.123',
461 'reverseProxyPrefixSSL' => '/proxyPrefixSSL',
462 ],
463 ],
464 '/proxyPrefixSSL/typo3/index.php?route=foo/bar&id=42',
465 ],
466 'prefix with proxy prefix if using query string and script name' => [
467 [
468 'REMOTE_ADDR' => '123.123.123.123',
469 'HTTPS' => 'on',
470 'QUERY_STRING' => 'route=foo/bar&id=42',
471 'SCRIPT_NAME' => '/typo3/index.php',
472 ],
473 [
474 'SYS' => [
475 'reverseProxyIP' => '123.123.123.123',
476 'reverseProxyPrefix' => '/proxyPrefix',
477 ],
478 ],
479 '/proxyPrefix/typo3/index.php?route=foo/bar&id=42',
480 ],
481 ];
482 }
483
484 /**
485 * @test
486 * @dataProvider getRequestUriReturnsExpectedValueDataProvider
487 * @param array $serverParams
488 * @param array $typo3ConfVars
489 * @param string $expected
490 */
491 public function getRequestUriReturnsExpectedValue(array $serverParams, array $typo3ConfVars, string $expected)
492 {
493 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
494 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
495 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
496 $this->assertSame($expected, $serverRequestParameters->getRequestUri());
497 }
498
499 /**
500 * @test
501 */
502 public function getRequestUriFetchesFromConfiguredRequestUriVar()
503 {
504 $GLOBALS['foo']['bar'] = '/foo/bar.php';
505 $serverParams = [
506 'HTTP_HOST' => 'www.domain.com',
507 ];
508 $typo3ConfVars = [
509 'SYS' => [
510 'requestURIvar' => 'foo|bar',
511 ],
512 ];
513 $expected = '/foo/bar.php';
514 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
515 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
516 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
517 $this->assertSame($expected, $serverRequestParameters->getRequestUri());
518 }
519
520 /**
521 * @test
522 */
523 public function getRequestUrlReturnsExpectedValue()
524 {
525 $serverParams = [
526 'HTTP_HOST' => 'www.domain.com',
527 'REQUEST_URI' => 'typo3/index.php?route=foo/bar&id=42',
528 ];
529 $expected = 'http://www.domain.com/typo3/index.php?route=foo/bar&id=42';
530 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
531 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
532 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
533 $this->assertSame($expected, $serverRequestParameters->getRequestUrl());
534 }
535
536 /**
537 * @test
538 */
539 public function getRequestScriptReturnsExpectedValue()
540 {
541 $serverParams = [
542 'HTTP_HOST' => 'www.domain.com',
543 'PATH_INFO' => '/typo3/index.php',
544 ];
545 $expected = 'http://www.domain.com/typo3/index.php';
546 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
547 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
548 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
549 $this->assertSame($expected, $serverRequestParameters->getRequestScript());
550 }
551
552 /**
553 * @test
554 */
555 public function getRequestDirReturnsExpectedValue()
556 {
557 $serverParams = [
558 'HTTP_HOST' => 'www.domain.com',
559 'PATH_INFO' => '/typo3/index.php',
560 ];
561 $expected = 'http://www.domain.com/typo3/';
562 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
563 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
564 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
565 $this->assertSame($expected, $serverRequestParameters->getRequestDir());
566 }
567
568 /**
569 * @return array[]
570 */
571 public function isBehindReverseProxyReturnsExpectedValueDataProvider(): array
572 {
573 return [
574 'false with empty data' => [
575 [
576 'HTTP_HOST' => 'www.domain.com',
577 ],
578 [],
579 false
580 ],
581 'false if REMOTE_ADDR and reverseProxyIP do not match' => [
582 [
583 'HTTP_HOST' => 'www.domain.com',
584 'REMOTE_ADDR' => '100.100.100.100',
585 ],
586 [
587 'SYS' => [
588 'reverseProxyIP' => '200.200.200.200',
589 ],
590 ],
591 false
592 ],
593 'true if REMOTE_ADDR matches configured reverseProxyIP' => [
594 [
595 'HTTP_HOST' => 'www.domain.com',
596 'REMOTE_ADDR' => '100.100.100.100',
597 ],
598 [
599 'SYS' => [
600 'reverseProxyIP' => '100.100.100.100',
601 ],
602 ],
603 true
604 ],
605 'true if trimmed REMOTE_ADDR matches configured trimmed reverseProxyIP' => [
606 [
607 'HTTP_HOST' => 'www.domain.com',
608 'REMOTE_ADDR' => ' 100.100.100.100 ',
609 ],
610 [
611 'SYS' => [
612 'reverseProxyIP' => ' 100.100.100.100 ',
613 ],
614 ],
615 true
616 ]
617 ];
618 }
619
620 /**
621 * @test
622 * @dataProvider isBehindReverseProxyReturnsExpectedValueDataProvider
623 * @param array $serverParams
624 * @param array $typo3ConfVars
625 * @param bool $expected
626 */
627 public function isBehindReverseProxyReturnsExpectedValue(array $serverParams, array $typo3ConfVars, bool $expected)
628 {
629 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
630 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
631 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
632 $this->assertSame($expected, $serverRequestParameters->isBehindReverseProxy());
633 }
634
635 /**
636 * @return array[]
637 */
638 public function getRemoteAddressReturnsExpectedValueDataProvider(): array
639 {
640 return [
641 'simple REMOTE_ADDR' => [
642 [
643 'HTTP_HOST' => 'www.domain.com',
644 'REMOTE_ADDR' => ' 123.123.123.123 ',
645 ],
646 [],
647 '123.123.123.123'
648 ],
649 'reverse proxy with last HTTP_X_FORWARDED_FOR' => [
650 [
651 'HTTP_HOST' => 'www.domain.com',
652 'REMOTE_ADDR' => ' 123.123.123.123 ',
653 'HTTP_X_FORWARDED_FOR' => ' 234.234.234.234, 235.235.235.235,',
654 ],
655 [
656 'SYS' => [
657 'reverseProxyIP' => '123.123.123.123',
658 'reverseProxyHeaderMultiValue' => ' last ',
659 ]
660 ],
661 '235.235.235.235'
662 ],
663 'reverse proxy with first HTTP_X_FORWARDED_FOR' => [
664 [
665 'HTTP_HOST' => 'www.domain.com',
666 'REMOTE_ADDR' => ' 123.123.123.123 ',
667 'HTTP_X_FORWARDED_FOR' => ' 234.234.234.234, 235.235.235.235,',
668 ],
669 [
670 'SYS' => [
671 'reverseProxyIP' => '123.123.123.123 ',
672 'reverseProxyHeaderMultiValue' => ' first ',
673 ]
674 ],
675 '234.234.234.234'
676 ],
677 'reverse proxy with broken reverseProxyHeaderMultiValue returns REMOTE_ADDR' => [
678 [
679 'HTTP_HOST' => 'www.domain.com',
680 'REMOTE_ADDR' => ' 123.123.123.123 ',
681 'HTTP_X_FORWARDED_FOR' => ' 234.234.234.234, 235.235.235.235,',
682 ],
683 [
684 'SYS' => [
685 'reverseProxyIP' => '123.123.123.123 ',
686 'reverseProxyHeaderMultiValue' => ' foo ',
687 ]
688 ],
689 '123.123.123.123'
690 ],
691 ];
692 }
693
694 /**
695 * @test
696 * @dataProvider getRemoteAddressReturnsExpectedValueDataProvider
697 * @param array $serverParams
698 * @param array $typo3ConfVars
699 * @param string $expected
700 */
701 public function getRemoteAddressReturnsExpectedValue(array $serverParams, array $typo3ConfVars, string $expected)
702 {
703 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
704 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
705 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), $typo3ConfVars, '', '');
706 $this->assertSame($expected, $serverRequestParameters->getRemoteAddress());
707 }
708
709 /**
710 * @return array
711 */
712 public static function getRequestHostOnlyReturnsExpectedValueDataProvider(): array
713 {
714 return [
715 'localhost ipv4 without port' => [
716 [
717 'HTTP_HOST' => '127.0.0.1',
718 ],
719 '127.0.0.1'
720 ],
721 'localhost ipv4 with port' => [
722 [
723 'HTTP_HOST' => '127.0.0.1:81',
724 ],
725 '127.0.0.1'
726 ],
727 'localhost ipv6 without port' => [
728 [
729 'HTTP_HOST' => '[::1]',
730 ],
731 '[::1]'
732 ],
733 'localhost ipv6 with port' => [
734 [
735 'HTTP_HOST' => '[::1]:81',
736 ],
737 '[::1]'
738 ],
739 'ipv6 without port' => [
740 [
741 'HTTP_HOST' => '[2001:DB8::1]',
742 ],
743 '[2001:DB8::1]'
744 ],
745 'ipv6 with port' => [
746 [
747 'HTTP_HOST' => '[2001:DB8::1]:81',
748 ],
749 '[2001:DB8::1]'
750 ],
751 'hostname without port' => [
752 [
753 'HTTP_HOST' => 'lolli.did.this',
754 ],
755 'lolli.did.this'
756 ],
757 'hostname with port' => [
758 [
759 'HTTP_HOST' => 'lolli.did.this:42',
760 ],
761 'lolli.did.this'
762 ],
763 ];
764 }
765
766 /**
767 * @test
768 * @dataProvider getRequestHostOnlyReturnsExpectedValueDataProvider
769 * @param array $serverParams
770 * @param string $expected
771 */
772 public function getRequestHostOnlyReturnsExpectedValue(array $serverParams, string $expected)
773 {
774 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
775 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
776 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
777 $this->assertSame($expected, $serverRequestParameters->getRequestHostOnly());
778 }
779
780 /**
781 * @return array
782 */
783 public static function getRequestPortOnlyReturnsExpectedValueDataProvider(): array
784 {
785 return [
786 'localhost ipv4 without port' => [
787 [
788 'HTTP_HOST' => '127.0.0.1',
789 ],
790 0
791 ],
792 'localhost ipv4 with port' => [
793 [
794 'HTTP_HOST' => '127.0.0.1:81',
795 ],
796 81
797 ],
798 'localhost ipv6 without port' => [
799 [
800 'HTTP_HOST' => '[::1]',
801 ],
802 0
803 ],
804 'localhost ipv6 with port' => [
805 [
806 'HTTP_HOST' => '[::1]:81',
807 ],
808 81
809 ],
810 'ipv6 without port' => [
811 [
812 'HTTP_HOST' => '[2001:DB8::1]',
813 ],
814 0
815 ],
816 'ipv6 with port' => [
817 [
818 'HTTP_HOST' => '[2001:DB8::1]:81',
819 ],
820 81
821 ],
822 'hostname without port' => [
823 [
824 'HTTP_HOST' => 'lolli.did.this',
825 ],
826 0
827 ],
828 'hostname with port' => [
829 [
830 'HTTP_HOST' => 'lolli.did.this:42',
831 ],
832 42
833 ],
834 ];
835 }
836
837 /**
838 * @test
839 * @dataProvider getRequestPortOnlyReturnsExpectedValueDataProvider
840 * @param array $serverParams
841 * @param int $expected
842 */
843 public function getRequestPortReturnsExpectedValue(array $serverParams, int $expected)
844 {
845 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
846 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
847 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
848 $this->assertSame($expected, $serverRequestParameters->getRequestPort());
849 }
850
851 /**
852 * @test
853 */
854 public function getScriptFilenameReturnsThirdConstructorArgument()
855 {
856 $serverParams = [
857 'HTTP_HOST' => 'www.domain.com',
858 'SCRIPT_NAME' => '/typo3/index.php',
859 ];
860 $pathSite = '/var/www/';
861 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
862 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
863 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '/var/www/typo3/index.php', $pathSite);
864 $this->assertSame('/var/www/typo3/index.php', $serverRequestParameters->getScriptFilename());
865 }
866
867 /**
868 * @test
869 */
870 public function getDocumentRootReturnsExpectedPath()
871 {
872 $serverParams = [
873 'HTTP_HOST' => 'www.domain.com',
874 'SCRIPT_NAME' => '/typo3/index.php',
875 ];
876 $pathThisScript = '/var/www/myInstance/Web/typo3/index.php';
877 $pathSite = '/var/www/myInstance/Web/';
878 $expected = '/var/www/myInstance/Web';
879 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
880 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
881 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
882 $this->assertSame($expected, $serverRequestParameters->getDocumentRoot());
883 }
884
885 /**
886 * @test
887 */
888 public function getSiteUrlReturnsExpectedUrl()
889 {
890 $serverParams = [
891 'SCRIPT_NAME' => '/typo3/index.php',
892 'HTTP_HOST' => 'www.domain.com',
893 'PATH_INFO' => '/typo3/index.php',
894 ];
895 $pathThisScript = '/var/www/myInstance/Web/typo3/index.php';
896 $pathSite = '/var/www/myInstance/Web/';
897 $expected = 'http://www.domain.com/';
898 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
899 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
900 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
901 $this->assertSame($expected, $serverRequestParameters->getSiteUrl());
902 }
903
904 /**
905 * @return array[]
906 */
907 public function getSitePathReturnsExpectedPathDataProvider()
908 {
909 return [
910 'empty config' => [
911 [],
912 '',
913 '',
914 ''
915 ],
916 'not in a sub directory' => [
917 [
918 'SCRIPT_NAME' => '/typo3/index.php',
919 'HTTP_HOST' => 'www.domain.com',
920 ],
921 '/var/www/myInstance/Web/typo3/index.php',
922 '/var/www/myInstance/Web/',
923 '/'
924 ],
925 'in a sub directory' => [
926 [
927 'SCRIPT_NAME' => '/some/sub/dir/typo3/index.php',
928 'HTTP_HOST' => 'www.domain.com',
929 ],
930 '/var/www/myInstance/Web/typo3/index.php',
931 '/var/www/myInstance/Web/',
932 '/some/sub/dir/'
933 ],
934 ];
935 }
936
937 /**
938 * @test
939 * @dataProvider getSitePathReturnsExpectedPathDataProvider
940 * @param array $serverParams
941 * @param string $pathThisScript
942 * @param string $pathSite
943 * @param string $expected
944 */
945 public function getSitePathReturnsExpectedPath(array $serverParams, string $pathThisScript, string $pathSite, string $expected)
946 {
947 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
948 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
949 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
950 $this->assertSame($expected, $serverRequestParameters->getSitePath());
951 }
952
953 /**
954 * @return array[]
955 */
956 public function getSiteScriptReturnsExpectedPathDataProvider()
957 {
958 return [
959 'not in a sub directory' => [
960 [
961 'SCRIPT_NAME' => '/typo3/index.php?id=42&foo=bar',
962 'HTTP_HOST' => 'www.domain.com',
963 ],
964 '/var/www/myInstance/Web/typo3/index.php',
965 '/var/www/myInstance/Web/',
966 'typo3/index.php?id=42&foo=bar'
967 ],
968 'in a sub directory' => [
969 [
970 'SCRIPT_NAME' => '/some/sub/dir/typo3/index.php?id=42&foo=bar',
971 'HTTP_HOST' => 'www.domain.com',
972 ],
973 '/var/www/myInstance/Web/typo3/index.php',
974 '/var/www/myInstance/Web/',
975 'typo3/index.php?id=42&foo=bar'
976 ],
977 ];
978 }
979
980 /**
981 * @test
982 * @dataProvider getSiteScriptReturnsExpectedPathDataProvider
983 * @param array $serverParams
984 * @param string $pathThisScript
985 * @param string $pathSite
986 * @param string $expected
987 */
988 public function getSiteScriptReturnsExpectedPath(array $serverParams, string $pathThisScript, string $pathSite, string $expected)
989 {
990 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
991 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
992 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], $pathThisScript, $pathSite);
993 $this->assertSame($expected, $serverRequestParameters->getSiteScript());
994 }
995
996 /**
997 * @test
998 */
999 public function getPathInfoReturnsExpectedValue()
1000 {
1001 $serverParams = [
1002 'PATH_INFO' => '/typo3/index.php',
1003 ];
1004 $expected = '/typo3/index.php';
1005 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1006 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1007 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1008 $this->assertSame($expected, $serverRequestParameters->getPathInfo());
1009 }
1010
1011 /**
1012 * @test
1013 */
1014 public function getHttpRefererReturnsExpectedValue()
1015 {
1016 $serverParams = [
1017 'HTTP_REFERER' => 'https://www.domain.com/typo3/index.php?id=42',
1018 ];
1019 $expected = 'https://www.domain.com/typo3/index.php?id=42';
1020 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1021 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1022 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1023 $this->assertSame($expected, $serverRequestParameters->getHttpReferer());
1024 }
1025
1026 /**
1027 * @test
1028 */
1029 public function getHttpUserAgentReturnsExpectedValue()
1030 {
1031 $serverParams = [
1032 'HTTP_USER_AGENT' => 'the client browser',
1033 ];
1034 $expected = 'the client browser';
1035 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1036 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1037 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1038 $this->assertSame($expected, $serverRequestParameters->getHttpUserAgent());
1039 }
1040
1041 /**
1042 * @test
1043 */
1044 public function getHttpAcceptEncodingReturnsExpectedValue()
1045 {
1046 $serverParams = [
1047 'HTTP_ACCEPT_ENCODING' => 'gzip, deflate',
1048 ];
1049 $expected = 'gzip, deflate';
1050 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1051 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1052 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1053 $this->assertSame($expected, $serverRequestParameters->getHttpAcceptEncoding());
1054 }
1055
1056 /**
1057 * @test
1058 */
1059 public function getHttpAcceptLanguageReturnsExpectedValue()
1060 {
1061 $serverParams = [
1062 'HTTP_ACCEPT_LANGUAGE' => 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7',
1063 ];
1064 $expected = 'de-DE,de;q=0.9,en-US;q=0.8,en;q=0.7';
1065 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1066 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1067 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1068 $this->assertSame($expected, $serverRequestParameters->getHttpAcceptLanguage());
1069 }
1070
1071 /**
1072 * @test
1073 */
1074 public function getRemoteHostReturnsExpectedValue()
1075 {
1076 $serverParams = [
1077 'REMOTE_HOST' => 'www.clientDomain.com',
1078 ];
1079 $expected = 'www.clientDomain.com';
1080 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1081 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1082 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1083 $this->assertSame($expected, $serverRequestParameters->getRemoteHost());
1084 }
1085
1086 /**
1087 * @test
1088 */
1089 public function getQueryStringReturnsExpectedValue()
1090 {
1091 $serverParams = [
1092 'QUERY_STRING' => 'id=42&foo=bar',
1093 ];
1094 $expected = 'id=42&foo=bar';
1095 $serverRequestProphecy = $this->prophesize(ServerRequestInterface::class);
1096 $serverRequestProphecy->getServerParams()->willReturn($serverParams);
1097 $serverRequestParameters = new NormalizedParams($serverRequestProphecy->reveal(), [], '', '');
1098 $this->assertSame($expected, $serverRequestParameters->getQueryString());
1099 }
1100 }