diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index dad75c13..afdd78ef 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -21,7 +21,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 f8fc0113..40de9af1 100644 --- a/module/Rest/src/Middleware/PathVersionMiddleware.php +++ b/module/Rest/src/Middleware/PathVersionMiddleware.php @@ -18,27 +18,16 @@ class PathVersionMiddleware implements MiddlewareInterface * @param RequestHandlerInterface $handler * * @return Response + * @throws \InvalidArgumentException */ public function process(Request $request, RequestHandlerInterface $handler): Response { $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 $handler->handle($request); - } - // If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes - if (\strpos($path, '/rest/v') !== 0) { - $parts = \explode('/', $path); - // Remove the first empty part and the rest part - \array_shift($parts); - \array_shift($parts); - // Prepend the version prefix - \array_unshift($parts, '/rest/v1'); - - $request = $request->withUri($uri->withPath(\implode('/', $parts))); + if (\strpos($path, '/v') !== 0) { + $request = $request->withUri($uri->withPath('/v1' . $uri->getPath())); } return $handler->handle($request); diff --git a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php index e024c128..ae483abf 100644 --- a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php +++ b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php @@ -30,22 +30,7 @@ class PathVersionMiddlewareTest extends TestCase */ public function whenVersionIsProvidedRequestRemainsUnchanged() { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo')); - - $delegate = $this->prophesize(RequestHandlerInterface::class); - $process = $delegate->handle($request)->willReturn(new Response()); - - $this->middleware->process($request, $delegate->reveal()); - - $process->shouldHaveBeenCalled(); - } - - /** - * @test - */ - public function whenPathDoesNotStartWithRestRemainsUnchanged() - { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/foo')); + $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo')); $delegate = $this->prophesize(RequestHandlerInterface::class); $process = $delegate->handle($request)->willReturn(new Response()); @@ -60,14 +45,14 @@ class PathVersionMiddlewareTest extends TestCase */ public function versionOneIsPrependedWhenNoVersionIsDefined() { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz')); + $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz')); $delegate = $this->prophesize(RequestHandlerInterface::class); $delegate->handle(Argument::type(Request::class))->will(function (array $args) use ($request) { $req = \array_shift($args); Assert::assertNotSame($request, $req); - Assert::assertEquals('/rest/v1/bar/baz', $req->getUri()->getPath()); + Assert::assertEquals('/v1/bar/baz', $req->getUri()->getPath()); return new Response(); });