Updated middleware which drops default domain so that it is capable of doing it from parsed body too

This commit is contained in:
Alejandro Celaya 2020-02-08 12:22:07 +01:00
parent 3f08b38558
commit ccec6e03aa
4 changed files with 32 additions and 25 deletions

View File

@ -37,7 +37,7 @@ return [
Middleware\BodyParserMiddleware::class => InvokableFactory::class,
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class => InvokableFactory::class,
Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware::class => ConfigAbstractFactory::class,
Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ConfigAbstractFactory::class,
],
],
@ -74,7 +74,7 @@ return [
Action\Tag\CreateTagsAction::class => [Service\Tag\TagService::class, LoggerInterface::class],
Action\Tag\UpdateTagAction::class => [Service\Tag\TagService::class, LoggerInterface::class],
Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware::class => ['config.url_shortener.domain.hostname'],
Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class => ['config.url_shortener.domain.hostname'],
],
];

View File

@ -4,8 +4,8 @@ declare(strict_types=1);
namespace Shlinkio\Shlink\Rest;
$contentNegotiationMiddleware = [Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class];
$dropDomainMiddleware = [Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware::class];
$contentNegotiationMiddleware = Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class;
$dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class;
return [
@ -13,16 +13,16 @@ return [
Action\HealthAction::getRouteDef(),
// Short codes
Action\ShortUrl\CreateShortUrlAction::getRouteDef($contentNegotiationMiddleware),
Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef($contentNegotiationMiddleware),
Action\ShortUrl\EditShortUrlAction::getRouteDef($dropDomainMiddleware),
Action\ShortUrl\DeleteShortUrlAction::getRouteDef($dropDomainMiddleware),
Action\ShortUrl\ResolveShortUrlAction::getRouteDef($dropDomainMiddleware),
Action\ShortUrl\CreateShortUrlAction::getRouteDef([$contentNegotiationMiddleware, $dropDomainMiddleware]),
Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef([$contentNegotiationMiddleware]),
Action\ShortUrl\EditShortUrlAction::getRouteDef([$dropDomainMiddleware]),
Action\ShortUrl\DeleteShortUrlAction::getRouteDef([$dropDomainMiddleware]),
Action\ShortUrl\ResolveShortUrlAction::getRouteDef([$dropDomainMiddleware]),
Action\ShortUrl\ListShortUrlsAction::getRouteDef(),
Action\ShortUrl\EditShortUrlTagsAction::getRouteDef($dropDomainMiddleware),
Action\ShortUrl\EditShortUrlTagsAction::getRouteDef([$dropDomainMiddleware]),
// Visits
Action\Visit\GetVisitsAction::getRouteDef($dropDomainMiddleware),
Action\Visit\GetVisitsAction::getRouteDef([$dropDomainMiddleware]),
// Tags
Action\Tag\ListTagsAction::getRouteDef(),

View File

@ -9,7 +9,7 @@ use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
class DropDefaultDomainFromQueryMiddleware implements MiddlewareInterface
class DropDefaultDomainFromRequestMiddleware implements MiddlewareInterface
{
private string $defaultDomain;
@ -20,12 +20,18 @@ class DropDefaultDomainFromQueryMiddleware implements MiddlewareInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$query = $request->getQueryParams();
if (isset($query['domain']) && $query['domain'] === $this->defaultDomain) {
unset($query['domain']);
$request = $request->withQueryParams($query);
}
$request = $request->withQueryParams($this->sanitizeDomainFromPayload($request->getQueryParams()))
->withParsedBody($this->sanitizeDomainFromPayload($request->getParsedBody()));
return $handler->handle($request);
}
private function sanitizeDomainFromPayload(array $payload): array
{
if (isset($payload['domain']) && $payload['domain'] === $this->defaultDomain) {
unset($payload['domain']);
}
return $payload;
}
}

View File

@ -12,29 +12,30 @@ use Prophecy\Argument;
use Prophecy\Prophecy\ObjectProphecy;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware;
use Shlinkio\Shlink\Rest\Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware;
class DropDefaultDomainFromQueryMiddlewareTest extends TestCase
class DropDefaultDomainFromRequestMiddlewareTest extends TestCase
{
private DropDefaultDomainFromQueryMiddleware $middleware;
private DropDefaultDomainFromRequestMiddleware $middleware;
private ObjectProphecy $next;
public function setUp(): void
{
$this->next = $this->prophesize(RequestHandlerInterface::class);
$this->middleware = new DropDefaultDomainFromQueryMiddleware('doma.in');
$this->middleware = new DropDefaultDomainFromRequestMiddleware('doma.in');
}
/**
* @test
* @dataProvider provideQueryParams
*/
public function domainIsDroppedWhenDefaultOneIsProvided(array $providedQuery, array $expectedQuery): void
public function domainIsDroppedWhenDefaultOneIsProvided(array $providedPayload, array $expectedPayload): void
{
$req = ServerRequestFactory::fromGlobals()->withQueryParams($providedQuery);
$req = ServerRequestFactory::fromGlobals()->withQueryParams($providedPayload)->withParsedBody($providedPayload);
$handle = $this->next->handle(Argument::that(function (ServerRequestInterface $request) use ($expectedQuery) {
Assert::assertEquals($expectedQuery, $request->getQueryParams());
$handle = $this->next->handle(Argument::that(function (ServerRequestInterface $request) use ($expectedPayload) {
Assert::assertEquals($expectedPayload, $request->getQueryParams());
Assert::assertEquals($expectedPayload, $request->getParsedBody());
return $request;
}))->willReturn(new Response());