From de9d9d866735157294d84c3733f8e66f1e8ebaa9 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Sat, 21 Jan 2017 20:12:12 +0100 Subject: [PATCH] Updated PathVersionMiddleware so that it is only applied to rest routes --- docker-compose.yml | 1 + .../Rest/config/middleware-pipeline.config.php | 1 + .../src/Middleware/PathVersionMiddleware.php | 14 ++++---------- .../Middleware/PathVersionMiddlewareTest.php | 18 +++--------------- 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index b829d472..217ae629 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,6 +10,7 @@ services: - "8000:80" volumes: - ./:/home/shlink/www + - ./docs:/home/shlink/www/public/docs - ./data/infra/vhost.conf:/etc/nginx/conf.d/shlink-vhost.conf links: - shlink_php diff --git a/module/Rest/config/middleware-pipeline.config.php b/module/Rest/config/middleware-pipeline.config.php index 8adea240..19f20198 100644 --- a/module/Rest/config/middleware-pipeline.config.php +++ b/module/Rest/config/middleware-pipeline.config.php @@ -5,6 +5,7 @@ return [ 'middleware_pipeline' => [ 'pre-routing' => [ + 'path' => '/rest', 'middleware' => [ Middleware\PathVersionMiddleware::class, ], diff --git a/module/Rest/src/Middleware/PathVersionMiddleware.php b/module/Rest/src/Middleware/PathVersionMiddleware.php index ac29d972..3220bfaf 100644 --- a/module/Rest/src/Middleware/PathVersionMiddleware.php +++ b/module/Rest/src/Middleware/PathVersionMiddleware.php @@ -37,19 +37,13 @@ class PathVersionMiddleware implements MiddlewareInterface $uri = $request->getUri(); $path = $uri->getPath(); - // Exclude non-rest route - if (strpos($path, '/rest') !== 0) { - return $out($request, $response); - } - // If the path does not begin with the version number, prepend v1 by default for retrocompatibility purposes - if (strpos($path, '/rest/v') !== 0) { + if (strpos($path, '/v') !== 0) { $parts = explode('/', $path); - // Remove the first empty part and the "/rest" prefix + // Remove the first empty part and the array_shift($parts); - array_shift($parts); - // Prepend the prefix with version - array_unshift($parts, '/rest/v1'); + // Prepend the version prefix + array_unshift($parts, '/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 6eba4feb..25dc629f 100644 --- a/module/Rest/test/Middleware/PathVersionMiddlewareTest.php +++ b/module/Rest/test/Middleware/PathVersionMiddlewareTest.php @@ -25,7 +25,7 @@ class PathVersionMiddlewareTest extends TestCase */ public function whenVersionIsProvidedRequestRemainsUnchanged() { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo')); + $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo')); $test = $this; $this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) { $test->assertSame($request, $req); @@ -37,23 +37,11 @@ class PathVersionMiddlewareTest extends TestCase */ public function versionOneIsPrependedWhenNoVersionIsDefined() { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz')); + $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz')); $test = $this; $this->middleware->__invoke($request, new Response(), function (Request $req) use ($request, $test) { $test->assertNotSame($request, $req); - $this->assertEquals('/rest/v1/bar/baz', $req->getUri()->getPath()); - }); - } - - /** - * @test - */ - public function nonRestPathsAreNotProcessed() - { - $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/non-rest')); - $test = $this; - $this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) { - $test->assertSame($request, $req); + $this->assertEquals('/v1/bar/baz', $req->getUri()->getPath()); }); } }