-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 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,27 @@ | ||
<?php | ||
namespace Packaged\Routing\Routes; | ||
|
||
use Packaged\Context\Context; | ||
use Packaged\Routing\FuncCondition; | ||
use Packaged\Routing\Handler\FuncHandler; | ||
use Packaged\Routing\Route; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
||
class InsecureRequestUpgradeRoute extends Route | ||
{ | ||
public function __construct() | ||
{ | ||
$this->add(FuncCondition::i(function (Context $c) { return !$c->request()->isSecure(true); })); | ||
} | ||
|
||
public function getHandler() | ||
{ | ||
return new FuncHandler( | ||
function (Context $c) { | ||
return RedirectResponse::create( | ||
str_replace('http:', 'https:', $c->request()->getUri()) | ||
); | ||
} | ||
); | ||
} | ||
} |
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,38 @@ | ||
<?php | ||
|
||
namespace Packaged\Tests\Routing; | ||
|
||
use Packaged\Context\Context; | ||
use Packaged\Http\Request; | ||
use Packaged\Routing\HealthCheckCondition; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class HealthCheckConditionTest extends TestCase | ||
{ | ||
|
||
public function testMatch() | ||
{ | ||
$this->assertFalse(HealthCheckCondition::i()->match(new Context(Request::create('http://www.test.com:8080/')))); | ||
$this->assertTrue( | ||
HealthCheckCondition::i()->match(new Context(Request::create('http://www.test.com:8080/_ah/health'))) | ||
); | ||
|
||
$ctx = new Context(Request::create('http://www.test.com:8080/')); | ||
$this->assertFalse(HealthCheckCondition::i()->match($ctx)); | ||
|
||
$ctx = new Context( | ||
Request::create('http://www.test.com:8080/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'GoogleHC/1']) | ||
); | ||
$this->assertTrue(HealthCheckCondition::i()->match($ctx)); | ||
|
||
$ctx = new Context( | ||
Request::create('http://www.test.com:8080/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'kube-probe']) | ||
); | ||
$this->assertTrue(HealthCheckCondition::i()->match($ctx)); | ||
|
||
$ctx = new Context( | ||
Request::create('http://www.test.com:8080/', 'GET', [], [], [], ['HTTP_USER_AGENT' => 'elb-healthchecker']) | ||
); | ||
$this->assertTrue(HealthCheckCondition::i()->match($ctx)); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Packaged\Tests\Routing\Routes; | ||
|
||
use Packaged\Context\Context; | ||
use Packaged\Http\Request; | ||
use Packaged\Routing\Routes\InsecureRequestUpgradeRoute; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
||
class InsecureRequestUpgradeRouteTest extends TestCase | ||
{ | ||
public function testHttpUpgrade() | ||
{ | ||
$ctx = new Context(Request::create('http://www.google.com/a/b/c/?d=e&f=g')); | ||
$route = InsecureRequestUpgradeRoute::i(); | ||
$this->assertTrue($route->match($ctx)); | ||
/** @var RedirectResponse|null $resp */ | ||
$resp = $route->getHandler()->handle($ctx); | ||
$this->assertInstanceOf(RedirectResponse::class, $resp); | ||
$this->assertEquals('https://www.google.com/a/b/c/?d=e&f=g', $resp->getTargetUrl()); | ||
} | ||
|
||
public function testHttpsIgnore() | ||
{ | ||
$ctx = new Context(Request::create('https://www.google.com/')); | ||
$route = InsecureRequestUpgradeRoute::i(); | ||
$this->assertFalse($route->match($ctx)); | ||
} | ||
} |