Commit 413573c8 authored by Igor Wiedler's avatar Igor Wiedler

Fix redirecting to https when requireHttps is set, fixes #308

parent 5f7b6505
......@@ -27,9 +27,24 @@ class RedirectableUrlMatcher extends BaseRedirectableUrlMatcher
*/
public function redirect($path, $route, $scheme = null)
{
$url = $this->context->getBaseUrl().$path;
if ($this->context->getHost()) {
if ($scheme) {
$port = '';
if ('http' === $scheme && 80 != $this->context->getHttpPort()) {
$port = ':'.$this->context->getHttpPort();
} elseif ('https' === $scheme && 443 != $this->context->getHttpsPort()) {
$port = ':'.$this->context->getHttpsPort();
}
$url = $scheme.'://'.$this->context->getHost().$port.$url;
}
}
return array(
'_controller' => function ($url) { return new RedirectResponse($url, 301); },
'url' => $this->context->getBaseUrl().$path,
'url' => $url,
);
}
}
......@@ -102,6 +102,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testMissingRoute()
{
$app = new Application();
unset($app['exception_handler']);
$request = Request::create('/baz');
......@@ -147,13 +148,16 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testRequestShouldBeStoredRegardlessOfRouting() {
$app = new Application();
$app->get('/foo', function () use ($app) {
return new Response($app['request']->getRequestUri());
});
$app->error(function ($e) use ($app) {
return new Response($app['request']->getRequestUri());
});
foreach(array('/foo', '/bar') as $path) {
foreach (array('/foo', '/bar') as $path) {
$request = Request::create($path);
$response = $app->handle($request);
$this->assertContains($path, $response->getContent());
......@@ -163,6 +167,7 @@ class RouterTest extends \PHPUnit_Framework_TestCase
public function testTrailingSlashBehavior()
{
$app = new Application();
$app->get('/foo/', function () use ($app) {
return new Response('ok');
});
......@@ -174,6 +179,34 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals('/foo/', $response->headers->get('Location'));
}
public function testRequireHttpRedirect()
{
$app = new Application();
$app->match('/secured', function () {
return 'secured content';
})
->requireHttp();
$request = Request::create('https://example.com/secured');
$response = $app->handle($request);
$this->assertTrue($response->isRedirect('http://example.com/secured'));
}
public function testRequireHttpsRedirect()
{
$app = new Application();
$app->match('/secured', function () {
return 'secured content';
})
->requireHttps();
$request = Request::create('http://example.com/secured');
$response = $app->handle($request);
$this->assertTrue($response->isRedirect('https://example.com/secured'));
}
public function testClassNameControllerSyntax()
{
$app = new Application();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment