Updated PathVersionMiddleware to single-pass middleware

This commit is contained in:
Alejandro Celaya 2018-03-26 17:36:58 +02:00
parent d9d599eab4
commit ee2233c6dd
2 changed files with 35 additions and 37 deletions

View file

@ -3,53 +3,38 @@ 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 // If the path does not begin with the version number, prepend v1 by default for BC compatibility purposes
if (strpos($path, '/v') !== 0) { if (\strpos($path, '/v') !== 0) {
$parts = explode('/', $path); $parts = \explode('/', $path);
// Remove the first empty part and the // Remove the first empty part and the
array_shift($parts); \array_shift($parts);
// Prepend the version prefix // 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);
} }
} }

View file

@ -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;
@ -28,10 +31,13 @@ class PathVersionMiddlewareTest extends TestCase
public function whenVersionIsProvidedRequestRemainsUnchanged() public function whenVersionIsProvidedRequestRemainsUnchanged()
{ {
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/v2/foo')); $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/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();
} }
/** /**
@ -40,10 +46,17 @@ class PathVersionMiddlewareTest extends TestCase
public function versionOneIsPrependedWhenNoVersionIsDefined() public function versionOneIsPrependedWhenNoVersionIsDefined()
{ {
$request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/bar/baz')); $request = ServerRequestFactory::fromGlobals()->withUri(new Uri('/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('/v1/bar/baz', $req->getUri()->getPath());
return new Response();
}); });
$this->middleware->process($request, $delegate->reveal());
} }
} }