mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-19 00:39:55 +03:00
Merge pull request #653 from acelaya-forks/feature/remove-default-domain-from-body
Feature/remove default domain from body
This commit is contained in:
commit
67a66cefa6
6 changed files with 49 additions and 25 deletions
|
@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com), and this
|
||||||
#### Fixed
|
#### Fixed
|
||||||
|
|
||||||
* [#648](https://github.com/shlinkio/shlink/issues/648) Ensured any user can write in log files, in case shlink is run by several system users.
|
* [#648](https://github.com/shlinkio/shlink/issues/648) Ensured any user can write in log files, in case shlink is run by several system users.
|
||||||
|
* [#650](https://github.com/shlinkio/shlink/issues/650) Ensured default domain is ignored when trying to create a short URL.
|
||||||
|
|
||||||
|
|
||||||
## 2.0.4 - 2020-02-02
|
## 2.0.4 - 2020-02-02
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -228,6 +228,22 @@ class CreateShortUrlActionTest extends ApiTestCase
|
||||||
$this->assertEquals($url, $payload['url']);
|
$this->assertEquals($url, $payload['url']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function defaultDomainIsDroppedIfProvided(): void
|
||||||
|
{
|
||||||
|
[$createStatusCode, ['shortCode' => $shortCode]] = $this->createShortUrl([
|
||||||
|
'longUrl' => 'https://www.alejandrocelaya.com',
|
||||||
|
'domain' => 'doma.in',
|
||||||
|
]);
|
||||||
|
$getResp = $this->callApiWithKey(self::METHOD_GET, '/short-urls/' . $shortCode);
|
||||||
|
$payload = $this->getJsonResponsePayload($getResp);
|
||||||
|
|
||||||
|
$this->assertEquals(self::STATUS_OK, $createStatusCode);
|
||||||
|
$this->assertEquals(self::STATUS_OK, $getResp->getStatusCode());
|
||||||
|
$this->assertArrayHasKey('domain', $payload);
|
||||||
|
$this->assertNull($payload['domain']);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array {
|
* @return array {
|
||||||
* @var int $statusCode
|
* @var int $statusCode
|
||||||
|
|
|
@ -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…
Add table
Reference in a new issue