mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Updated middleware which drops default domain so that it is capable of doing it from parsed body too
This commit is contained in:
parent
3f08b38558
commit
ccec6e03aa
@ -37,7 +37,7 @@ return [
|
|||||||
Middleware\BodyParserMiddleware::class => InvokableFactory::class,
|
Middleware\BodyParserMiddleware::class => InvokableFactory::class,
|
||||||
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
|
Middleware\CrossDomainMiddleware::class => InvokableFactory::class,
|
||||||
Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::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\CreateTagsAction::class => [Service\Tag\TagService::class, LoggerInterface::class],
|
||||||
Action\Tag\UpdateTagAction::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'],
|
||||||
],
|
],
|
||||||
|
|
||||||
];
|
];
|
||||||
|
@ -4,8 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest;
|
namespace Shlinkio\Shlink\Rest;
|
||||||
|
|
||||||
$contentNegotiationMiddleware = [Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class];
|
$contentNegotiationMiddleware = Middleware\ShortUrl\CreateShortUrlContentNegotiationMiddleware::class;
|
||||||
$dropDomainMiddleware = [Middleware\ShortUrl\DropDefaultDomainFromQueryMiddleware::class];
|
$dropDomainMiddleware = Middleware\ShortUrl\DropDefaultDomainFromRequestMiddleware::class;
|
||||||
|
|
||||||
return [
|
return [
|
||||||
|
|
||||||
@ -13,16 +13,16 @@ return [
|
|||||||
Action\HealthAction::getRouteDef(),
|
Action\HealthAction::getRouteDef(),
|
||||||
|
|
||||||
// Short codes
|
// Short codes
|
||||||
Action\ShortUrl\CreateShortUrlAction::getRouteDef($contentNegotiationMiddleware),
|
Action\ShortUrl\CreateShortUrlAction::getRouteDef([$contentNegotiationMiddleware, $dropDomainMiddleware]),
|
||||||
Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef($contentNegotiationMiddleware),
|
Action\ShortUrl\SingleStepCreateShortUrlAction::getRouteDef([$contentNegotiationMiddleware]),
|
||||||
Action\ShortUrl\EditShortUrlAction::getRouteDef($dropDomainMiddleware),
|
Action\ShortUrl\EditShortUrlAction::getRouteDef([$dropDomainMiddleware]),
|
||||||
Action\ShortUrl\DeleteShortUrlAction::getRouteDef($dropDomainMiddleware),
|
Action\ShortUrl\DeleteShortUrlAction::getRouteDef([$dropDomainMiddleware]),
|
||||||
Action\ShortUrl\ResolveShortUrlAction::getRouteDef($dropDomainMiddleware),
|
Action\ShortUrl\ResolveShortUrlAction::getRouteDef([$dropDomainMiddleware]),
|
||||||
Action\ShortUrl\ListShortUrlsAction::getRouteDef(),
|
Action\ShortUrl\ListShortUrlsAction::getRouteDef(),
|
||||||
Action\ShortUrl\EditShortUrlTagsAction::getRouteDef($dropDomainMiddleware),
|
Action\ShortUrl\EditShortUrlTagsAction::getRouteDef([$dropDomainMiddleware]),
|
||||||
|
|
||||||
// Visits
|
// Visits
|
||||||
Action\Visit\GetVisitsAction::getRouteDef($dropDomainMiddleware),
|
Action\Visit\GetVisitsAction::getRouteDef([$dropDomainMiddleware]),
|
||||||
|
|
||||||
// Tags
|
// Tags
|
||||||
Action\Tag\ListTagsAction::getRouteDef(),
|
Action\Tag\ListTagsAction::getRouteDef(),
|
||||||
|
@ -9,7 +9,7 @@ use Psr\Http\Message\ServerRequestInterface;
|
|||||||
use Psr\Http\Server\MiddlewareInterface;
|
use Psr\Http\Server\MiddlewareInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
use Psr\Http\Server\RequestHandlerInterface;
|
||||||
|
|
||||||
class DropDefaultDomainFromQueryMiddleware implements MiddlewareInterface
|
class DropDefaultDomainFromRequestMiddleware implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
private string $defaultDomain;
|
private string $defaultDomain;
|
||||||
|
|
||||||
@ -20,12 +20,18 @@ class DropDefaultDomainFromQueryMiddleware implements MiddlewareInterface
|
|||||||
|
|
||||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||||
{
|
{
|
||||||
$query = $request->getQueryParams();
|
$request = $request->withQueryParams($this->sanitizeDomainFromPayload($request->getQueryParams()))
|
||||||
if (isset($query['domain']) && $query['domain'] === $this->defaultDomain) {
|
->withParsedBody($this->sanitizeDomainFromPayload($request->getParsedBody()));
|
||||||
unset($query['domain']);
|
|
||||||
$request = $request->withQueryParams($query);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $handler->handle($request);
|
return $handler->handle($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function sanitizeDomainFromPayload(array $payload): array
|
||||||
|
{
|
||||||
|
if (isset($payload['domain']) && $payload['domain'] === $this->defaultDomain) {
|
||||||
|
unset($payload['domain']);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
}
|
}
|
@ -12,29 +12,30 @@ use Prophecy\Argument;
|
|||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Psr\Http\Server\RequestHandlerInterface;
|
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;
|
private ObjectProphecy $next;
|
||||||
|
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->next = $this->prophesize(RequestHandlerInterface::class);
|
$this->next = $this->prophesize(RequestHandlerInterface::class);
|
||||||
$this->middleware = new DropDefaultDomainFromQueryMiddleware('doma.in');
|
$this->middleware = new DropDefaultDomainFromRequestMiddleware('doma.in');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
* @dataProvider provideQueryParams
|
* @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) {
|
$handle = $this->next->handle(Argument::that(function (ServerRequestInterface $request) use ($expectedPayload) {
|
||||||
Assert::assertEquals($expectedQuery, $request->getQueryParams());
|
Assert::assertEquals($expectedPayload, $request->getQueryParams());
|
||||||
|
Assert::assertEquals($expectedPayload, $request->getParsedBody());
|
||||||
return $request;
|
return $request;
|
||||||
}))->willReturn(new Response());
|
}))->willReturn(new Response());
|
||||||
|
|
Loading…
Reference in New Issue
Block a user