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