mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-27 16:26:37 +03:00
commit
f60c217fae
5 changed files with 75 additions and 45 deletions
|
@ -1,5 +1,11 @@
|
|||
## 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
|
||||
|
||||
**Enhancements:**
|
||||
|
|
|
@ -8,6 +8,7 @@ use Zend\Expressive\Helper;
|
|||
use Zend\Expressive\Middleware;
|
||||
use Zend\Expressive\Plates;
|
||||
use Zend\Expressive\Router;
|
||||
use Zend\Expressive\Router\Middleware\ImplicitOptionsMiddleware;
|
||||
use Zend\Expressive\Template;
|
||||
use Zend\ServiceManager\Factory\InvokableFactory;
|
||||
use Zend\Stratigility\Middleware\ErrorHandler;
|
||||
|
@ -20,11 +21,15 @@ return [
|
|||
Template\TemplateRendererInterface::class => Plates\PlatesRendererFactory::class,
|
||||
Router\RouterInterface::class => Router\FastRouteRouterFactory::class,
|
||||
ErrorHandler::class => Container\ErrorHandlerFactory::class,
|
||||
Middleware\ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class,
|
||||
ImplicitOptionsMiddleware::class => EmptyResponseImplicitOptionsMiddlewareFactory::class,
|
||||
|
||||
Helper\UrlHelper::class => Helper\UrlHelperFactory::class,
|
||||
Helper\ServerUrlHelper::class => InvokableFactory::class,
|
||||
],
|
||||
|
||||
'aliases' => [
|
||||
Middleware\ImplicitOptionsMiddleware::class => ImplicitOptionsMiddleware::class,
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
|
|
|
@ -20,7 +20,7 @@ return [
|
|||
'priority' => 11,
|
||||
],
|
||||
'pre-routing-rest' => [
|
||||
'path' => '/rest',
|
||||
// 'path' => '/rest',
|
||||
'middleware' => [
|
||||
PathVersionMiddleware::class,
|
||||
],
|
||||
|
|
|
@ -3,53 +3,44 @@ declare(strict_types=1);
|
|||
|
||||
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\ServerRequestInterface as Request;
|
||||
use Zend\Stratigility\MiddlewareInterface;
|
||||
|
||||
class PathVersionMiddleware implements MiddlewareInterface
|
||||
{
|
||||
/**
|
||||
* Process an incoming request and/or 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.
|
||||
* Process an incoming server request and return a response, optionally delegating
|
||||
* to the next middleware component to create the response.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param null|callable $out
|
||||
* @return null|Response
|
||||
* @param DelegateInterface $delegate
|
||||
*
|
||||
* @return Response
|
||||
*/
|
||||
public function __invoke(Request $request, Response $response, callable $out = null)
|
||||
public function process(Request $request, DelegateInterface $delegate)
|
||||
{
|
||||
$uri = $request->getUri();
|
||||
$path = $uri->getPath();
|
||||
|
||||
// If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes
|
||||
if (strpos($path, '/v') !== 0) {
|
||||
$parts = explode('/', $path);
|
||||
// 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)));
|
||||
// TODO Workaround... Do not process the request if it does not start with rest
|
||||
if (\strpos($path, '/rest') !== 0) {
|
||||
return $delegate->process($request);
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
use Interop\Http\ServerMiddleware\DelegateInterface;
|
||||
use PHPUnit\Framework\Assert;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Shlinkio\Shlink\Rest\Middleware\PathVersionMiddleware;
|
||||
use Zend\Diactoros\Response;
|
||||
|
@ -27,11 +30,29 @@ class PathVersionMiddlewareTest extends TestCase
|
|||
*/
|
||||
public function whenVersionIsProvidedRequestRemainsUnchanged()
|
||||
{
|
||||
$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);
|
||||
});
|
||||
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/v2/foo'));
|
||||
|
||||
$delegate = $this->prophesize(DelegateInterface::class);
|
||||
$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()
|
||||
{
|
||||
$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('/v1/bar/baz', $req->getUri()->getPath());
|
||||
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/rest/bar/baz'));
|
||||
|
||||
$delegate = $this->prophesize(DelegateInterface::class);
|
||||
$delegate->process(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());
|
||||
return new Response();
|
||||
});
|
||||
|
||||
|
||||
$this->middleware->process($request, $delegate->reveal());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue