Skip to content

Commit

Permalink
Merge pull request #128 from weirdan/upgrade-to-expressive-2.2
Browse files Browse the repository at this point in the history
Upgrade to expressive 2.2
  • Loading branch information
acelaya authored Mar 21, 2018
2 parents 86ed83d + dff2ad3 commit d1ba44e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
6 changes: 3 additions & 3 deletions config/autoload/middleware-pipeline.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

'routing' => [
'middleware' => [
Expressive\Application::ROUTING_MIDDLEWARE,
Expressive\Router\Middleware\RouteMiddleware::class,
],
'priority' => 10,
],
Expand All @@ -38,7 +38,7 @@
'path' => '/rest',
'middleware' => [
CrossDomainMiddleware::class,
Expressive\Middleware\ImplicitOptionsMiddleware::class,
Expressive\Router\Middleware\ImplicitOptionsMiddleware::class,
BodyParserMiddleware::class,
CheckAuthenticationMiddleware::class,
],
Expand All @@ -47,7 +47,7 @@

'post-routing' => [
'middleware' => [
Expressive\Application::DISPATCH_MIDDLEWARE,
Expressive\Router\Middleware\DispatchMiddleware::class,
],
'priority' => 1,
],
Expand Down
2 changes: 2 additions & 0 deletions config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/

return (new ConfigAggregator\ConfigAggregator([
Zend\Expressive\ConfigProvider::class,
Zend\Expressive\Router\ConfigProvider::class,
ExpressiveErrorHandler\ConfigProvider::class,
Common\ConfigProvider::class,
Core\ConfigProvider::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Expressive\Middleware\ImplicitOptionsMiddleware;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use PHPUnit\Framework\TestCase;
use Shlinkio\Shlink\Common\Factory\EmptyResponseImplicitOptionsMiddlewareFactory;
use Zend\Diactoros\Response\EmptyResponse;
use Zend\Expressive\Middleware\ImplicitOptionsMiddleware;
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
use Zend\ServiceManager\ServiceManager;

class EmptyResponseImplicitOptionsMiddlewareFactoryTest extends TestCase
Expand Down
29 changes: 22 additions & 7 deletions module/Rest/test/Middleware/CheckAuthenticationMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Zend\Expressive\Router\RouteResult;
use Zend\I18n\Translator\Translator;

use function Zend\Stratigility\middleware;

class CheckAuthenticationMiddlewareTest extends TestCase
{
/**
Expand All @@ -28,10 +30,18 @@ class CheckAuthenticationMiddlewareTest extends TestCase
*/
protected $jwtService;

/**
* @var callable
*/
protected $dummyMiddleware;

public function setUp()
{
$this->jwtService = $this->prophesize(JWTService::class);
$this->middleware = new CheckAuthenticationMiddleware($this->jwtService->reveal(), Translator::factory([]));
$this->dummyMiddleware = middleware(function ($request, $handler) {
return new Response\EmptyResponse;
});
}

/**
Expand Down Expand Up @@ -59,7 +69,12 @@ public function someWhiteListedSituationsFallbackToNextMiddleware()

$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('foo', '', Route::HTTP_METHOD_ANY, AuthenticateAction::class))
RouteResult::fromRoute(new Route(
'foo',
$this->dummyMiddleware,
Route::HTTP_METHOD_ANY,
AuthenticateAction::class
))
);
$delegate = $this->prophesize(DelegateInterface::class);
/** @var MethodProphecy $process */
Expand All @@ -69,7 +84,7 @@ public function someWhiteListedSituationsFallbackToNextMiddleware()

$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', 'foo'), [])
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withMethod('OPTIONS');
$delegate = $this->prophesize(DelegateInterface::class);
/** @var MethodProphecy $process */
Expand All @@ -85,7 +100,7 @@ public function noHeaderReturnsError()
{
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', 'foo'), [])
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
);
$response = $this->middleware->process($request, TestUtils::createDelegateMock()->reveal());
$this->assertEquals(401, $response->getStatusCode());
Expand All @@ -99,7 +114,7 @@ public function provideAnAuthorizationWithoutTypeReturnsError()
$authToken = 'ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', 'foo'), [])
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, $authToken);

$response = $this->middleware->process($request, TestUtils::createDelegateMock()->reveal());
Expand All @@ -116,7 +131,7 @@ public function provideAnAuthorizationWithWrongTypeReturnsError()
$authToken = 'ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', 'foo'), [])
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Basic ' . $authToken);

$response = $this->middleware->process($request, TestUtils::createDelegateMock()->reveal());
Expand All @@ -135,7 +150,7 @@ public function provideAnExpiredTokenReturnsError()
$authToken = 'ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', 'foo'), [])
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'Bearer ' . $authToken);
$this->jwtService->verify($authToken)->willReturn(false)->shouldBeCalledTimes(1);

Expand All @@ -151,7 +166,7 @@ public function provideCorrectTokenUpdatesExpirationAndFallsBackToNextMiddleware
$authToken = 'ABC-abc';
$request = ServerRequestFactory::fromGlobals()->withAttribute(
RouteResult::class,
RouteResult::fromRoute(new Route('bar', 'foo'), [])
RouteResult::fromRoute(new Route('bar', $this->dummyMiddleware), [])
)->withHeader(CheckAuthenticationMiddleware::AUTHORIZATION_HEADER, 'bearer ' . $authToken);
$this->jwtService->verify($authToken)->willReturn(true)->shouldBeCalledTimes(1);
$this->jwtService->refresh($authToken)->willReturn($authToken)->shouldBeCalledTimes(1);
Expand Down

0 comments on commit d1ba44e

Please sign in to comment.