Skip to content

Commit

Permalink
move is console or not enabled check to trait
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 11, 2018
1 parent 404b083 commit af232cf
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 29 deletions.
39 changes: 18 additions & 21 deletions spec/Listener/ForceHttpsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,22 @@

});

it('attach on route event on non-console and enable', function () {
it('not attach when not enabled', function () {

Console::overrideIsConsole(false);
$listener = new ForceHttps([
'enable' => false,
'force_all_routes' => true,
'force_specific_routes' => [],
]);

$eventManager = Double::instance(['implements' => EventManagerInterface::class]);
$listener->attach($this->eventManager);

expect($this->eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']);

});

it('attach on route event on non-console and enable', function () {

Console::overrideIsConsole(false);
$listener = new ForceHttps([
Expand All @@ -46,9 +59,10 @@
'force_specific_routes' => [],
]);

$listener->attach($eventManager);
allow($this->eventManager)->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']);
$listener->attach($this->eventManager);

expect($eventManager)->not->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']);
expect($this->eventManager)->toReceive('attach')->with(MvcEvent::EVENT_ROUTE, [$listener, 'forceHttpsScheme']);

});

Expand All @@ -64,23 +78,6 @@
$this->routeMatch = Double::instance(['extends' => RouteMatch::class, 'methods' => '__construct']);
});

context('not enabled', function () {

it('returns early', function () {

$listener = new ForceHttps([
'enable' => false,
'force_all_routes' => true,
'force_specific_routes' => [],
]);
$actual = $listener->forceHttpsScheme($this->mvcEvent);

expect($actual)->toBe(null);

});

});

context('on current scheme is https', function () {

it('not redirect if uri already has https scheme and without strict_transport_security', function () {
Expand Down
6 changes: 6 additions & 0 deletions src/HttpsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,18 @@
namespace ForceHttpsModule;

use Psr\Http\Message\ResponseInterface;
use Zend\Console\Console;
use Zend\Expressive\Router\RouteResult;
use Zend\Http\PhpEnvironment\Response;
use Zend\Router\RouteMatch;

trait HttpsTrait
{
private function isConsoleOrNotEnabled() : bool
{
return Console::isConsole() || ! $this->config['enable'];
}

private function isSchemeHttps(string $uriScheme) : bool
{
return $uriScheme === 'https';
Expand Down
7 changes: 1 addition & 6 deletions src/Listener/ForceHttps.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace ForceHttpsModule\Listener;

use ForceHttpsModule\HttpsTrait;
use Zend\Console\Console;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Http\PhpEnvironment\Response;
Expand All @@ -28,7 +27,7 @@ public function __construct(array $config)

public function attach(EventManagerInterface $events, $priority = 1) : void
{
if (Console::isConsole()) {
if ($this->isConsoleOrNotEnabled()) {
return;
}

Expand Down Expand Up @@ -58,10 +57,6 @@ private function setHttpStrictTransportSecurity(string $uriScheme, RouteMatch $m
*/
public function forceHttpsScheme(MvcEvent $e) : void
{
if (! $this->config['enable']) {
return;
}

/** @var \Zend\Http\PhpEnvironment\Request $request */
$request = $e->getRequest();
/** @var \Zend\Http\PhpEnvironment\Response $response */
Expand Down
3 changes: 1 addition & 2 deletions src/Middleware/ForceHttps.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Console\Console;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Router\RouterInterface;

Expand Down Expand Up @@ -45,7 +44,7 @@ private function setHttpStrictTransportSecurity($uriScheme, RouteResult $match,
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$response = $handler->handle($request);
if (Console::isConsole() || ! $this->config['enable']) {
if ($this->isConsoleOrNotEnabled()) {
return $response;
}

Expand Down

0 comments on commit af232cf

Please sign in to comment.