|
| 1 | +<?php |
| 2 | + |
| 3 | +use Tester\Assert; |
| 4 | + |
| 5 | +require __DIR__ . '/../bootstrap.php'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Test of Nette\Http\RequestFactory port detection |
| 9 | + */ |
| 10 | +class RequestFactoryPortTest extends Tester\TestCase |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * @dataProvider providerCreateHttpRequest |
| 15 | + */ |
| 16 | + public function testCreateHttpRequest($expectedPort, array $server) |
| 17 | + { |
| 18 | + $_SERVER = $server; |
| 19 | + |
| 20 | + $factory = new Nette\Http\RequestFactory; |
| 21 | + Assert::same($expectedPort, $factory->createHttpRequest()->getUrl()->getPort()); |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * @return array |
| 26 | + */ |
| 27 | + public function providerCreateHttpRequest() |
| 28 | + { |
| 29 | + return [ |
| 30 | + [80, []], |
| 31 | + [8080, ['HTTP_HOST' => 'localhost:8080']], |
| 32 | + [8080, ['SERVER_NAME' => 'localhost:8080']], |
| 33 | + [8080, ['HTTP_HOST' => 'localhost:8080', 'SERVER_PORT' => '666']], |
| 34 | + [8080, ['SERVER_NAME' => 'localhost:8080', 'SERVER_PORT' => '666']], |
| 35 | + [8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '8080']], |
| 36 | + [8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '8080']], |
| 37 | + |
| 38 | + [80, ['HTTP_X_FORWARDED_PORT' => '8080']], |
| 39 | + [8080, ['HTTP_HOST' => 'localhost:8080', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 40 | + [8080, ['SERVER_NAME' => 'localhost:8080', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 41 | + [8080, ['HTTP_HOST' => 'localhost:8080', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 42 | + [8080, ['SERVER_NAME' => 'localhost:8080', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 43 | + [80, ['HTTP_HOST' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 44 | + [80, ['SERVER_NAME' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 45 | + [8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '8080', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 46 | + [8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '8080', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 47 | + [44443, ['HTTPS' => 'on', 'SERVER_NAME' => 'localhost:44443', 'HTTP_X_FORWARDED_PORT' => '666']], |
| 48 | + ]; |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dataProvider providerCreateHttpRequestWithTrustedProxy |
| 53 | + */ |
| 54 | + public function testCreateHttpRequestWithTrustedProxy($expectedPort, array $server) |
| 55 | + { |
| 56 | + $_SERVER = array_merge(['REMOTE_ADDR' => '10.0.0.1'], $server); |
| 57 | + |
| 58 | + $factory = new Nette\Http\RequestFactory; |
| 59 | + $factory->setProxy(['10.0.0.1']); |
| 60 | + Assert::same($expectedPort, $factory->createHttpRequest()->getUrl()->getPort()); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @return array |
| 65 | + */ |
| 66 | + public function providerCreateHttpRequestWithTrustedProxy() |
| 67 | + { |
| 68 | + return [ |
| 69 | + [8080, ['HTTP_X_FORWARDED_PORT' => '8080']], |
| 70 | + [8080, ['HTTP_HOST' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 71 | + [8080, ['SERVER_NAME' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 72 | + [8080, ['HTTP_HOST' => 'localhost:666', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 73 | + [8080, ['SERVER_NAME' => 'localhost:666', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 74 | + [8080, ['HTTP_HOST' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 75 | + [8080, ['SERVER_NAME' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 76 | + [8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 77 | + [8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']], |
| 78 | + [44443, ['HTTPS' => 'on', 'SERVER_NAME' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '44443']], |
| 79 | + ]; |
| 80 | + } |
| 81 | + |
| 82 | +} |
| 83 | + |
| 84 | +$test = new RequestFactoryPortTest(); |
| 85 | +$test->run(); |
0 commit comments