mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 21:27:44 +03:00
Updated PathVersionMiddleware to single-pass middleware
This commit is contained in:
parent
d9d599eab4
commit
ee2233c6dd
2 changed files with 35 additions and 37 deletions
|
@ -3,53 +3,38 @@ 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);
|
||||
if (\strpos($path, '/v') !== 0) {
|
||||
$parts = \explode('/', $path);
|
||||
// Remove the first empty part and the
|
||||
array_shift($parts);
|
||||
\array_shift($parts);
|
||||
// Prepend the version prefix
|
||||
array_unshift($parts, '/v1');
|
||||
\array_unshift($parts, '/v1');
|
||||
|
||||
$request = $request->withUri($uri->withPath(implode('/', $parts)));
|
||||
$request = $request->withUri($uri->withPath(\implode('/', $parts)));
|
||||
}
|
||||
|
||||
return $out($request, $response);
|
||||
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;
|
||||
|
@ -28,10 +31,13 @@ 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);
|
||||
});
|
||||
|
||||
$delegate = $this->prophesize(DelegateInterface::class);
|
||||
$process = $delegate->process($request)->willReturn(new Response());
|
||||
|
||||
$this->middleware->process($request, $delegate->reveal());
|
||||
|
||||
$process->shouldHaveBeenCalled();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -40,10 +46,17 @@ 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());
|
||||
|
||||
$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('/v1/bar/baz', $req->getUri()->getPath());
|
||||
return new Response();
|
||||
});
|
||||
|
||||
|
||||
$this->middleware->process($request, $delegate->reveal());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue