Skip to content

Commit

Permalink
middleware never be in console environment, remove its check
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Apr 11, 2018
1 parent af232cf commit e168d11
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 31 deletions.
23 changes: 0 additions & 23 deletions spec/Middleware/ForceHttpsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Psr\Http\Message\UriInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Console\Console;
use Zend\Expressive\Router\Route;
use Zend\Expressive\Router\RouteResult;
use Zend\Expressive\Router\RouterInterface;
Expand All @@ -34,22 +33,8 @@
return Double::instance(['implements' => RouterInterface::class]);
});

it('not redirect on console', function () {

Console::overrideIsConsole(true);
$listener = new ForceHttps([], $this->router);

$handler = Double::instance(['implements' => RequestHandlerInterface::class]);
allow($handler)->toReceive('handle')->with($this->request)->andReturn($this->response);

$listener->process($this->request, $handler);

expect($this->response)->not->toReceive('withStatus');
});

it('not redirect on not-enable', function () {

Console::overrideIsConsole(false);
$listener = new ForceHttps(['enable' => false], $this->router);

$handler = Double::instance(['implements' => RequestHandlerInterface::class]);
Expand All @@ -62,7 +47,6 @@

it('not redirect on router not match', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRouteFailure(null);
allow($this->router)->toReceive('match')->andReturn($match);

Expand All @@ -79,7 +63,6 @@

it('not redirect on https and match but no strict_transport_security config', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRoute(new Route('/about', Double::instance(['implements' => MiddlewareInterface::class])));

allow($this->router)->toReceive('match')->andReturn($match);
Expand All @@ -99,7 +82,6 @@

it('not redirect on http and match, with force_all_routes is false and matched route name not in force_specific_routes config', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRoute(new Route('/about', Double::instance(['implements' => MiddlewareInterface::class])));
allow($this->router)->toReceive('match')->andReturn($match);

Expand Down Expand Up @@ -129,7 +111,6 @@

it('not redirect on https and match, with strict_transport_security config, but disabled', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRoute(new Route('/about', Double::instance(['implements' => MiddlewareInterface::class])));

allow($this->router)->toReceive('match')->andReturn($match);
Expand Down Expand Up @@ -160,7 +141,6 @@

it('not redirect on https and match, with strict_transport_security config, and enabled', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRoute(new Route('/about', Double::instance(['implements' => MiddlewareInterface::class])));

allow($this->router)->toReceive('match')->andReturn($match);
Expand Down Expand Up @@ -190,7 +170,6 @@

it('return Response with 308 status on http and match', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRoute(new Route('/about', Double::instance(['implements' => MiddlewareInterface::class])));

allow($this->router)->toReceive('match')->andReturn($match);
Expand Down Expand Up @@ -222,7 +201,6 @@

it('return Response with 308 status with include www prefix on http and match with configurable "add_www_prefix"', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRoute(new Route('/about', Double::instance(['implements' => MiddlewareInterface::class])));

allow($this->router)->toReceive('match')->andReturn($match);
Expand Down Expand Up @@ -255,7 +233,6 @@

it('return Response with 308 status with remove www prefix on http and match with configurable "remove_www_prefix"', function () {

Console::overrideIsConsole(false);
$match = RouteResult::fromRoute(new Route('/about', Double::instance(['implements' => MiddlewareInterface::class])));

allow($this->request)->toReceive('getUri', '__toString')->andReturn('http://www.example.com/about');
Expand Down
6 changes: 0 additions & 6 deletions src/HttpsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@
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
3 changes: 2 additions & 1 deletion src/Listener/ForceHttps.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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 @@ -27,7 +28,7 @@ public function __construct(array $config)

public function attach(EventManagerInterface $events, $priority = 1) : void
{
if ($this->isConsoleOrNotEnabled()) {
if (Console::isConsole() || ! $this->config['enable']) {
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Middleware/ForceHttps.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private function setHttpStrictTransportSecurity($uriScheme, RouteResult $match,
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
{
$response = $handler->handle($request);
if ($this->isConsoleOrNotEnabled()) {
if (! $this->config['enable']) {
return $response;
}

Expand Down

0 comments on commit e168d11

Please sign in to comment.