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