diff --git a/config/autoload/middleware-pipeline.global.php b/config/autoload/middleware-pipeline.global.php index 279417bb..0f24a7c9 100644 --- a/config/autoload/middleware-pipeline.global.php +++ b/config/autoload/middleware-pipeline.global.php @@ -30,12 +30,6 @@ return [ Common\Middleware\CloseDbConnectionMiddleware::class, ], ], - 'pre-routing-rest' => [ - 'path' => '/rest', - 'middleware' => [ - Rest\Middleware\PathVersionMiddleware::class, - ], - ], 'routing' => [ 'middleware' => [ diff --git a/module/Rest/config/dependencies.config.php b/module/Rest/config/dependencies.config.php index 11bfaed3..953d212a 100644 --- a/module/Rest/config/dependencies.config.php +++ b/module/Rest/config/dependencies.config.php @@ -36,7 +36,6 @@ return [ ImplicitOptionsMiddleware::class => Middleware\EmptyResponseImplicitOptionsMiddlewareFactory::class, Middleware\BodyParserMiddleware::class => InvokableFactory::class, Middleware\CrossDomainMiddleware::class => InvokableFactory::class, - Middleware\PathVersionMiddleware::class => InvokableFactory::class, Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class => InvokableFactory::class, ], ], diff --git a/module/Rest/src/ConfigProvider.php b/module/Rest/src/ConfigProvider.php index 8624ad66..6352e3c7 100644 --- a/module/Rest/src/ConfigProvider.php +++ b/module/Rest/src/ConfigProvider.php @@ -12,6 +12,7 @@ use function sprintf; class ConfigProvider { private const ROUTES_PREFIX = '/rest/v{version:1|2}'; + private const UNVERSIONED_ROUTES_PREFIX = '/rest'; private Closure $loadConfig; @@ -33,7 +34,11 @@ class ConfigProvider // Prepend the routes prefix to every path foreach ($routes as $key => $route) { ['path' => $path] = $route; - $routes[$key]['path'] = sprintf('%s%s', self::ROUTES_PREFIX, $path); + $routes[$key]['path'] = sprintf( + '%s%s', + $path === '/health' ? self::UNVERSIONED_ROUTES_PREFIX : self::ROUTES_PREFIX, + $path, + ); } return $config; diff --git a/module/Rest/src/Middleware/PathVersionMiddleware.php b/module/Rest/src/Middleware/PathVersionMiddleware.php deleted file mode 100644 index 84921710..00000000 --- a/module/Rest/src/Middleware/PathVersionMiddleware.php +++ /dev/null @@ -1,30 +0,0 @@ -getUri(); - $path = $uri->getPath(); - - // If the path does not begin with the version number, prepend v1 by default for BC purposes - if (strpos($path, '/v') !== 0) { - $request = $request->withUri($uri->withPath('/v1' . $uri->getPath())); - } - - return $handler->handle($request); - } -} diff --git a/module/Rest/test/ConfigProviderTest.php b/module/Rest/test/ConfigProviderTest.php index b8cebf93..80919c30 100644 --- a/module/Rest/test/ConfigProviderTest.php +++ b/module/Rest/test/ConfigProviderTest.php @@ -33,6 +33,7 @@ class ConfigProviderTest extends TestCase ['path' => '/foo'], ['path' => '/bar'], ['path' => '/baz/foo'], + ['path' => '/health'], ], ]); @@ -42,6 +43,7 @@ class ConfigProviderTest extends TestCase ['path' => '/rest/v{version:1|2}/foo'], ['path' => '/rest/v{version:1|2}/bar'], ['path' => '/rest/v{version:1|2}/baz/foo'], + ['path' => '/rest/health'], ], $config['routes']); } } diff --git a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php deleted file mode 100644 index 98e6698d..00000000 --- a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php +++ /dev/null @@ -1,58 +0,0 @@ -middleware = new PathVersionMiddleware(); - } - - /** @test */ - public function whenVersionIsProvidedRequestRemainsUnchanged(): void - { - $request = (new ServerRequest())->withUri(new Uri('/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 versionOneIsPrependedWhenNoVersionIsDefined(): void - { - $request = (new ServerRequest())->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('/v1/bar/baz', $req->getUri()->getPath()); - return new Response(); - }); - - - $this->middleware->process($request, $delegate->reveal()); - } -}