-
-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RequestFactory: correctly detects scheme and port if the server is be…
- Loading branch information
Showing
3 changed files
with
186 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
/** | ||
* Test of Nette\Http\RequestFactory port detection | ||
*/ | ||
class RequestFactoryPortTest extends Tester\TestCase | ||
{ | ||
|
||
/** | ||
* @dataProvider providerCreateHttpRequest | ||
*/ | ||
public function testCreateHttpRequest($expectedPort, array $server) | ||
{ | ||
$_SERVER = $server; | ||
|
||
$factory = new Nette\Http\RequestFactory; | ||
Assert::same($expectedPort, $factory->createHttpRequest()->getUrl()->getPort()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function providerCreateHttpRequest() | ||
{ | ||
return [ | ||
[80, []], | ||
[8080, ['HTTP_HOST' => 'localhost:8080']], | ||
[8080, ['SERVER_NAME' => 'localhost:8080']], | ||
[8080, ['HTTP_HOST' => 'localhost:8080', 'SERVER_PORT' => '666']], | ||
[8080, ['SERVER_NAME' => 'localhost:8080', 'SERVER_PORT' => '666']], | ||
[8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '8080']], | ||
[8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '8080']], | ||
|
||
[80, ['HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['HTTP_HOST' => 'localhost:8080', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[8080, ['SERVER_NAME' => 'localhost:8080', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[8080, ['HTTP_HOST' => 'localhost:8080', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[8080, ['SERVER_NAME' => 'localhost:8080', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[80, ['HTTP_HOST' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[80, ['SERVER_NAME' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '8080', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '8080', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
[44443, ['HTTPS' => 'on', 'SERVER_NAME' => 'localhost:44443', 'HTTP_X_FORWARDED_PORT' => '666']], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider providerCreateHttpRequestWithTrustedProxy | ||
*/ | ||
public function testCreateHttpRequestWithTrustedProxy($expectedPort, array $server) | ||
{ | ||
$_SERVER = array_merge(['REMOTE_ADDR' => '10.0.0.1'], $server); | ||
|
||
$factory = new Nette\Http\RequestFactory; | ||
$factory->setProxy(['10.0.0.1']); | ||
Assert::same($expectedPort, $factory->createHttpRequest()->getUrl()->getPort()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function providerCreateHttpRequestWithTrustedProxy() | ||
{ | ||
return [ | ||
[8080, ['HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['HTTP_HOST' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['SERVER_NAME' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['HTTP_HOST' => 'localhost:666', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['SERVER_NAME' => 'localhost:666', 'SERVER_PORT' => '80', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['HTTP_HOST' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['SERVER_NAME' => 'localhost', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['HTTP_HOST' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[8080, ['SERVER_NAME' => 'localhost', 'SERVER_PORT' => '666', 'HTTP_X_FORWARDED_PORT' => '8080']], | ||
[44443, ['HTTPS' => 'on', 'SERVER_NAME' => 'localhost:666', 'HTTP_X_FORWARDED_PORT' => '44443']], | ||
]; | ||
} | ||
|
||
} | ||
|
||
$test = new RequestFactoryPortTest(); | ||
$test->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
/** | ||
* Test of Nette\Http\RequestFactory schema detection | ||
*/ | ||
class RequestFactorySchemeTest extends Tester\TestCase | ||
{ | ||
|
||
/** | ||
* @covers RequestFactory::getScheme | ||
* @dataProvider providerCreateHttpRequest | ||
*/ | ||
public function testCreateHttpRequest($expectedScheme, array $server) | ||
{ | ||
$_SERVER = $server; | ||
|
||
$factory = new Nette\Http\RequestFactory; | ||
Assert::same($expectedScheme, $factory->createHttpRequest()->getUrl()->getScheme()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function providerCreateHttpRequest() | ||
{ | ||
return [ | ||
['http', []], | ||
['http', ['HTTPS' => '']], | ||
['http', ['HTTPS' => 'off']], | ||
['http', ['HTTP_X_FORWARDED_PROTO' => 'https']], | ||
['http', ['HTTP_X_FORWARDED_PORT' => '443']], | ||
['http', ['HTTP_X_FORWARDED_PROTO' => 'https', 'HTTP_X_FORWARDED_PORT' => '443']], | ||
|
||
['https', ['HTTPS' => 'on']], | ||
['https', ['HTTPS' => 'anything']], | ||
['https', ['HTTPS' => 'on', 'HTTP_X_FORWARDED_PROTO' => 'http']], | ||
['https', ['HTTPS' => 'on', 'HTTP_X_FORWARDED_PORT' => '80']], | ||
['https', ['HTTPS' => 'on', 'HTTP_X_FORWARDED_PROTO' => 'http', 'HTTP_X_FORWARDED_PORT' => '80']], | ||
]; | ||
} | ||
|
||
/** | ||
* @covers RequestFactory::getScheme | ||
* @dataProvider providerCreateHttpRequestWithTrustedProxy | ||
*/ | ||
public function testCreateHttpRequestWithTrustedProxy($expectedScheme, array $server) | ||
{ | ||
$_SERVER = array_merge(['REMOTE_ADDR' => '10.0.0.1'], $server); | ||
|
||
$factory = new Nette\Http\RequestFactory; | ||
$factory->setProxy(['10.0.0.1']); | ||
Assert::same($expectedScheme, $factory->createHttpRequest()->getUrl()->getScheme()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function providerCreateHttpRequestWithTrustedProxy() | ||
{ | ||
return [ | ||
['http', ['HTTP_X_FORWARDED_PROTO' => 'http']], | ||
['http', ['HTTPS' => 'on', 'HTTP_X_FORWARDED_PROTO' => 'http']], | ||
['http', ['HTTPS' => 'on', 'HTTP_X_FORWARDED_PROTO' => 'something-unexpected']], | ||
['http', ['HTTPS' => 'on', 'HTTP_X_FORWARDED_PROTO' => 'http', 'HTTP_X_FORWARDED_PORT' => '443']], | ||
|
||
['https', ['HTTP_X_FORWARDED_PROTO' => 'https']], | ||
['https', ['HTTPS' => 'off', 'HTTP_X_FORWARDED_PROTO' => 'https']], | ||
['https', ['HTTPS' => 'off', 'HTTP_X_FORWARDED_PROTO' => 'https', 'HTTP_X_FORWARDED_PORT' => '80']], | ||
]; | ||
} | ||
|
||
} | ||
|
||
$test = new RequestFactorySchemeTest(); | ||
$test->run(); |