Removed PathVersionMiddleware as routes without version are not longer supported

This commit is contained in:
Alejandro Celaya 2019-12-31 16:26:00 +01:00
parent f99ca464de
commit 339121fbb1
6 changed files with 8 additions and 96 deletions

View file

@ -30,12 +30,6 @@ return [
Common\Middleware\CloseDbConnectionMiddleware::class,
],
],
'pre-routing-rest' => [
'path' => '/rest',
'middleware' => [
Rest\Middleware\PathVersionMiddleware::class,
],
],
'routing' => [
'middleware' => [

View file

@ -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,
],
],

View file

@ -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;

View file

@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
namespace Shlinkio\Shlink\Rest\Middleware;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function strpos;
class PathVersionMiddleware implements MiddlewareInterface
{
// TODO The /health endpoint needs this middleware in order to work without the version.
// Take it into account if this middleware is ever removed.
public function process(Request $request, RequestHandlerInterface $handler): Response
{
$uri = $request->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);
}
}

View file

@ -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']);
}
}

View file

@ -1,58 +0,0 @@
<?php
declare(strict_types=1);
namespace ShlinkioTest\Shlink\Rest\Middleware;
use PHPUnit\Framework\Assert;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
use Zend\Diactoros\Response;
use Zend\Diactoros\ServerRequest;
use Zend\Diactoros\Uri;
use function array_shift;
class PathVersionMiddlewareTest extends TestCase
{
private PathVersionMiddleware $middleware;
public function setUp(): void
{
$this->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());
}
}