mirror of
https://github.com/shlinkio/shlink.git
synced 2025-03-27 12:01:49 +03:00
commit
f60c217fae
5 changed files with 75 additions and 45 deletions
|
@ -1,5 +1,11 @@
|
||||||
## CHANGELOG
|
## CHANGELOG
|
||||||
|
|
||||||
|
### 1.7.2
|
||||||
|
|
||||||
|
**Bugs:**
|
||||||
|
|
||||||
|
* [135: Fix PathVersionMiddleware being ignored when using expressive 2.2](https://github.com/shlinkio/shlink/issues/135)
|
||||||
|
|
||||||
### 1.7.1
|
### 1.7.1
|
||||||
|
|
||||||
**Enhancements:**
|
**Enhancements:**
|
||||||
|
|
|
@ -8,6 +8,7 @@ use Zend\Expressive\Helper;
|
||||||
use Zend\Expressive\Middleware;
|
use Zend\Expressive\Middleware;
|
||||||
use Zend\Expressive\Plates;
|
use Zend\Expressive\Plates;
|
||||||
use Zend\Expressive\Router;
|
use Zend\Expressive\Router;
|
||||||
|
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
|
||||||
use Zend\Expressive\Template;
|
use Zend\Expressive\Template;
|
||||||
use Zend\ServiceManager\Factory\InvokableFactory;
|
use Zend\ServiceManager\Factory\InvokableFactory;
|
||||||
use Zend\Stratigility\Middleware\ErrorHandler;
|
use Zend\Stratigility\Middleware\ErrorHandler;
|
||||||
|
@ -20,11 +21,15 @@ return [
|
||||||
Template\TemplateRendererInterface::class => Plates\PlatesRendererFactory::class,
|
Template\TemplateRendererInterface::class => Plates\PlatesRendererFactory::class,
|
||||||
Router\RouterInterface::class => Router\FastRouteRouterFactory::class,
|
Router\RouterInterface::class => Router\FastRouteRouterFactory::class,
|
||||||
ErrorHandler::class => Container\ErrorHandlerFactory::class,
|
ErrorHandler::class => Container\ErrorHandlerFactory::class,
|
||||||
Middleware\ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class,
|
ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class,
|
||||||
|
|
||||||
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
||||||
Helper\ServerUrlHelper::class => InvokableFactory::class,
|
Helper\ServerUrlHelper::class => InvokableFactory::class,
|
||||||
],
|
],
|
||||||
|
|
||||||
|
'aliases' => [
|
||||||
|
Middleware\ImplicitOptionsMiddleware::class => ImplicitOptionsMiddleware::class,
|
||||||
|
],
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
|
|
@ -20,7 +20,7 @@ return [
|
||||||
'priority' => 11,
|
'priority' => 11,
|
||||||
],
|
],
|
||||||
'pre-routing-rest' => [
|
'pre-routing-rest' => [
|
||||||
'path' => '/rest',
|
// 'path' => '/rest',
|
||||||
'middleware' => [
|
'middleware' => [
|
||||||
PathVersionMiddleware::class,
|
PathVersionMiddleware::class,
|
||||||
],
|
],
|
||||||
|
|
|
@ -3,53 +3,44 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest\Middleware;
|
namespace Shlinkio\Shlink\Rest\Middleware;
|
||||||
|
|
||||||
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||||
|
use Interop\Http\ServerMiddleware\MiddlewareInterface;
|
||||||
use Psr\Http\Message\ResponseInterface as Response;
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Zend\Stratigility\MiddlewareInterface;
|
|
||||||
|
|
||||||
class PathVersionMiddleware implements MiddlewareInterface
|
class PathVersionMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Process an incoming request and/or response.
|
* Process an incoming server request and return a response, optionally delegating
|
||||||
*
|
* to the next middleware component to create the response.
|
||||||
* Accepts a server-side request and a response instance, and does
|
|
||||||
* something with them.
|
|
||||||
*
|
|
||||||
* If the response is not complete and/or further processing would not
|
|
||||||
* interfere with the work done in the middleware, or if the middleware
|
|
||||||
* wants to delegate to another process, it can use the `$out` callable
|
|
||||||
* if present.
|
|
||||||
*
|
|
||||||
* If the middleware does not return a value, execution of the current
|
|
||||||
* request is considered complete, and the response instance provided will
|
|
||||||
* be considered the response to return.
|
|
||||||
*
|
|
||||||
* Alternately, the middleware may return a response instance.
|
|
||||||
*
|
|
||||||
* Often, middleware will `return $out();`, with the assumption that a
|
|
||||||
* later middleware will return a response.
|
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param Response $response
|
* @param DelegateInterface $delegate
|
||||||
* @param null|callable $out
|
*
|
||||||
* @return null|Response
|
* @return Response
|
||||||
*/
|
*/
|
||||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
public function process(Request $request, DelegateInterface $delegate)
|
||||||
{
|
{
|
||||||
$uri = $request->getUri();
|
$uri = $request->getUri();
|
||||||
$path = $uri->getPath();
|
$path = $uri->getPath();
|
||||||
|
|
||||||
// If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes
|
// TODO Workaround... Do not process the request if it does not start with rest
|
||||||
if (strpos($path, '/v') !== 0) {
|
if (\strpos($path, '/rest') !== 0) {
|
||||||
$parts = explode('/', $path);
|
return $delegate->process($request);
|
||||||
// Remove the first empty part and the
|
|
||||||
array_shift($parts);
|
|
||||||
// Prepend the version prefix
|
|
||||||
array_unshift($parts, '/v1');
|
|
||||||
|
|
||||||
$request = $request->withUri($uri->withPath(implode('/', $parts)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $out($request, $response);
|
// 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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $delegate->process($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,10 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
namespace ShlinkioTest\Shlink\Rest\Middleware;
|
||||||
|
|
||||||
|
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||||
|
use PHPUnit\Framework\Assert;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Prophecy\Argument;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
|
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
|
||||||
use Zend\Diactoros\Response;
|
use Zend\Diactoros\Response;
|
||||||
|
@ -27,11 +30,29 @@ class PathVersionMiddlewareTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function whenVersionIsProvidedRequestRemainsUnchanged()
|
public function whenVersionIsProvidedRequestRemainsUnchanged()
|
||||||
{
|
{
|
||||||
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo'));
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo'));
|
||||||
$test = $this;
|
|
||||||
$this->middleware->__invoke($request, new Response(), function ($req) use ($request, $test) {
|
$delegate = $this->prophesize(DelegateInterface::class);
|
||||||
$test->assertSame($request, $req);
|
$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());
|
||||||
|
|
||||||
|
$this->middleware->process($request, $delegate->reveal());
|
||||||
|
|
||||||
|
$process->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,11 +60,18 @@ class PathVersionMiddlewareTest extends TestCase
|
||||||
*/
|
*/
|
||||||
public function versionOneIsPrependedWhenNoVersionIsDefined()
|
public function versionOneIsPrependedWhenNoVersionIsDefined()
|
||||||
{
|
{
|
||||||
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz'));
|
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz'));
|
||||||
$test = $this;
|
|
||||||
$this->middleware->__invoke($request, new Response(), function (Request $req) use ($request, $test) {
|
$delegate = $this->prophesize(DelegateInterface::class);
|
||||||
$test->assertNotSame($request, $req);
|
$delegate->process(Argument::type(Request::class))->will(function (array $args) use ($request) {
|
||||||
$this->assertEquals('/v1/bar/baz', $req->getUri()->getPath());
|
$req = \array_shift($args);
|
||||||
|
|
||||||
|
Assert::assertNotSame($request, $req);
|
||||||
|
Assert::assertEquals('/rest/v1/bar/baz', $req->getUri()->getPath());
|
||||||
|
return new Response();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
$this->middleware->process($request, $delegate->reveal());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue