From 4d0fc1da0743fb28efa36eaafd8a807b63e7edcc Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Mon, 26 Mar 2018 17:53:22 +0200 Subject: [PATCH] Fixed PathVersionMiddleware not being properly propagated --- config/autoload/dependencies.global.php | 7 ++++++- .../autoload/middleware-pipeline.global.php | 2 +- .../src/Middleware/PathVersionMiddleware.php | 12 ++++++++--- .../Middleware/PathVersionMiddlewareTest.php | 21 ++++++++++++++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index ad7ff750..8afccea0 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -8,6 +8,7 @@ use Zend\Expressive\Helper; use Zend\Expressive\Middleware; use Zend\Expressive\Plates; use Zend\Expressive\Router; +use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware; use Zend\Expressive\Template; use Zend\ServiceManager\Factory\InvokableFactory; use Zend\Stratigility\Middleware\ErrorHandler; @@ -20,11 +21,15 @@ return [ Template\TemplateRendererInterface::class => Plates\PlatesRendererFactory::class, Router\RouterInterface::class => Router\FastRouteRouterFactory::class, ErrorHandler::class => Container\ErrorHandlerFactory::class, - Middleware\ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class, + ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class, Helper\UrlHelper::class => Helper\UrlHelperFactory::class, Helper\ServerUrlHelper::class => InvokableFactory::class, ], + + 'aliases' => [ + Middleware\ImplicitOptionsMiddleware::class => ImplicitOptionsMiddleware::class, + ], ], ]; diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index 3157f6ee..2d4c0c18 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -20,7 +20,7 @@ return [ 'priority' => 11, ], 'pre-routing-rest' => [ - 'path' => '/rest', +// 'path' => '/rest', 'middleware' => [ PathVersionMiddleware::class, ], diff --git a/module/Rest/src/Middleware/PathVersionMiddleware.php b/module/Rest/src/Middleware/PathVersionMiddleware.php index 928d2b84..18cfc394 100644 --- a/module/Rest/src/Middleware/PathVersionMiddleware.php +++ b/module/Rest/src/Middleware/PathVersionMiddleware.php @@ -24,13 +24,19 @@ class PathVersionMiddleware implements MiddlewareInterface $uri = $request->getUri(); $path = $uri->getPath(); + // TODO Workaround... Do not process the request if it does not start with rest + if (\strpos($path, '/rest') !== 0) { + return $delegate->process($request); + } + // If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes - if (\strpos($path, '/v') !== 0) { + if (\strpos($path, '/rest/v') !== 0) { $parts = \explode('/', $path); - // Remove the first empty part and the + // Remove the first empty part and the rest part + \array_shift($parts); \array_shift($parts); // Prepend the version prefix - \array_unshift($parts, '/v1'); + \array_unshift($parts, '/rest/v1'); $request = $request->withUri($uri->withPath(\implode('/', $parts))); } diff --git a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php index 1e2a7a69..4c98d4f2 100644 --- a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php +++ b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php @@ -30,7 +30,22 @@ class PathVersionMiddlewareTest extends TestCase */ public function whenVersionIsProvidedRequestRemainsUnchanged() { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo')); + $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo')); + + $delegate = $this->prophesize(DelegateInterface::class); + $process = $delegate->process($request)->willReturn(new Response()); + + $this->middleware->process($request, $delegate->reveal()); + + $process->shouldHaveBeenCalled(); + } + + /** + * @test + */ + public function whenPathDoesNotStartWithRestRemainsUnchanged() + { + $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo')); $delegate = $this->prophesize(DelegateInterface::class); $process = $delegate->process($request)->willReturn(new Response()); @@ -45,14 +60,14 @@ class PathVersionMiddlewareTest extends TestCase */ public function versionOneIsPrependedWhenNoVersionIsDefined() { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz')); + $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz')); $delegate = $this->prophesize(DelegateInterface::class); $delegate->process(Argument::type(Request::class))->will(function (array $args) use ($request) { $req = \array_shift($args); Assert::assertNotSame($request, $req); - Assert::assertEquals('/v1/bar/baz', $req->getUri()->getPath()); + Assert::assertEquals('/rest/v1/bar/baz', $req->getUri()->getPath()); return new Response(); });