mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 05:13:13 +03:00
Updated EditShortUrlAction so that it returns the parsed short URL instead of an empty response
This commit is contained in:
parent
85bc5ce595
commit
c61e1e1c0e
6 changed files with 43 additions and 14 deletions
|
@ -150,8 +150,33 @@
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"responses": {
|
"responses": {
|
||||||
"204": {
|
"200": {
|
||||||
"description": "The short code has been properly updated."
|
"description": "The short URL has been properly updated.",
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "../definitions/ShortUrl.json"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"examples": {
|
||||||
|
"application/json": {
|
||||||
|
"shortCode": "12Kb3",
|
||||||
|
"shortUrl": "https://doma.in/12Kb3",
|
||||||
|
"longUrl": "https://shlink.io",
|
||||||
|
"dateCreated": "2016-05-01T20:34:16+02:00",
|
||||||
|
"visitsCount": 1029,
|
||||||
|
"tags": [
|
||||||
|
"shlink"
|
||||||
|
],
|
||||||
|
"meta": {
|
||||||
|
"validSince": "2017-01-21T00:00:00+02:00",
|
||||||
|
"validUntil": null,
|
||||||
|
"maxVisits": 100
|
||||||
|
},
|
||||||
|
"domain": null
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"400": {
|
"400": {
|
||||||
"description": "Provided meta arguments are invalid.",
|
"description": "Provided meta arguments are invalid.",
|
||||||
|
|
|
@ -59,7 +59,7 @@ return [
|
||||||
Service\UrlShortener::class,
|
Service\UrlShortener::class,
|
||||||
'config.url_shortener.domain',
|
'config.url_shortener.domain',
|
||||||
],
|
],
|
||||||
Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class],
|
Action\ShortUrl\EditShortUrlAction::class => [Service\ShortUrlService::class, 'config.url_shortener.domain'],
|
||||||
Action\ShortUrl\DeleteShortUrlAction::class => [Service\ShortUrl\DeleteShortUrlService::class],
|
Action\ShortUrl\DeleteShortUrlAction::class => [Service\ShortUrl\DeleteShortUrlService::class],
|
||||||
Action\ShortUrl\ResolveShortUrlAction::class => [
|
Action\ShortUrl\ResolveShortUrlAction::class => [
|
||||||
Service\ShortUrl\ShortUrlResolver::class,
|
Service\ShortUrl\ShortUrlResolver::class,
|
||||||
|
|
|
@ -4,12 +4,13 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
namespace Shlinkio\Shlink\Rest\Action\ShortUrl;
|
||||||
|
|
||||||
use Laminas\Diactoros\Response\EmptyResponse;
|
use Laminas\Diactoros\Response\JsonResponse;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use Psr\Http\Message\ServerRequestInterface;
|
use Psr\Http\Message\ServerRequestInterface;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
|
use Shlinkio\Shlink\Core\Model\ShortUrlEdit;
|
||||||
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
use Shlinkio\Shlink\Core\Model\ShortUrlIdentifier;
|
||||||
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
|
use Shlinkio\Shlink\Core\Service\ShortUrlServiceInterface;
|
||||||
|
use Shlinkio\Shlink\Core\Transformer\ShortUrlDataTransformer;
|
||||||
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
use Shlinkio\Shlink\Rest\Action\AbstractRestAction;
|
||||||
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
use Shlinkio\Shlink\Rest\Middleware\AuthenticationMiddleware;
|
||||||
|
|
||||||
|
@ -19,10 +20,12 @@ class EditShortUrlAction extends AbstractRestAction
|
||||||
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH, self::METHOD_PUT];
|
protected const ROUTE_ALLOWED_METHODS = [self::METHOD_PATCH, self::METHOD_PUT];
|
||||||
|
|
||||||
private ShortUrlServiceInterface $shortUrlService;
|
private ShortUrlServiceInterface $shortUrlService;
|
||||||
|
private ShortUrlDataTransformer $transformer;
|
||||||
|
|
||||||
public function __construct(ShortUrlServiceInterface $shortUrlService)
|
public function __construct(ShortUrlServiceInterface $shortUrlService, array $domainConfig)
|
||||||
{
|
{
|
||||||
$this->shortUrlService = $shortUrlService;
|
$this->shortUrlService = $shortUrlService;
|
||||||
|
$this->transformer = new ShortUrlDataTransformer($domainConfig);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handle(ServerRequestInterface $request): ResponseInterface
|
public function handle(ServerRequestInterface $request): ResponseInterface
|
||||||
|
@ -31,7 +34,8 @@ class EditShortUrlAction extends AbstractRestAction
|
||||||
$identifier = ShortUrlIdentifier::fromApiRequest($request);
|
$identifier = ShortUrlIdentifier::fromApiRequest($request);
|
||||||
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
|
$apiKey = AuthenticationMiddleware::apiKeyFromRequest($request);
|
||||||
|
|
||||||
$this->shortUrlService->updateShortUrl($identifier, $shortUrlEdit, $apiKey);
|
$shortUrl = $this->shortUrlService->updateShortUrl($identifier, $shortUrlEdit, $apiKey);
|
||||||
return new EmptyResponse();
|
|
||||||
|
return new JsonResponse($this->transformer->transform($shortUrl));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,8 +41,8 @@ class EditShortUrlTest extends ApiTestCase
|
||||||
]);
|
]);
|
||||||
$metaAfterResetting = $this->findShortUrlMetaByShortCode($shortCode);
|
$metaAfterResetting = $this->findShortUrlMetaByShortCode($shortCode);
|
||||||
|
|
||||||
self::assertEquals(self::STATUS_NO_CONTENT, $editWithProvidedMeta->getStatusCode());
|
self::assertEquals(self::STATUS_OK, $editWithProvidedMeta->getStatusCode());
|
||||||
self::assertEquals(self::STATUS_NO_CONTENT, $editWithResetMeta->getStatusCode());
|
self::assertEquals(self::STATUS_OK, $editWithResetMeta->getStatusCode());
|
||||||
self::assertEquals($resetMeta, $metaAfterResetting);
|
self::assertEquals($resetMeta, $metaAfterResetting);
|
||||||
self::assertArraySubset($meta, $metaAfterEditing);
|
self::assertArraySubset($meta, $metaAfterEditing);
|
||||||
}
|
}
|
||||||
|
@ -93,7 +93,7 @@ class EditShortUrlTest extends ApiTestCase
|
||||||
|
|
||||||
public function provideLongUrls(): iterable
|
public function provideLongUrls(): iterable
|
||||||
{
|
{
|
||||||
yield 'valid URL' => ['https://shlink.io', self::STATUS_NO_CONTENT, null];
|
yield 'valid URL' => ['https://shlink.io', self::STATUS_OK, null];
|
||||||
yield 'invalid URL' => ['htt:foo', self::STATUS_BAD_REQUEST, 'INVALID_URL'];
|
yield 'invalid URL' => ['htt:foo', self::STATUS_BAD_REQUEST, 'INVALID_URL'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ class EditShortUrlTest extends ApiTestCase
|
||||||
]]);
|
]]);
|
||||||
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url));
|
$editedShortUrl = $this->getJsonResponsePayload($this->callApiWithKey(self::METHOD_GET, (string) $url));
|
||||||
|
|
||||||
self::assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
|
self::assertEquals(self::STATUS_OK, $editResp->getStatusCode());
|
||||||
self::assertEquals($domain, $editedShortUrl['domain']);
|
self::assertEquals($domain, $editedShortUrl['domain']);
|
||||||
self::assertEquals($expectedUrl, $editedShortUrl['longUrl']);
|
self::assertEquals($expectedUrl, $editedShortUrl['longUrl']);
|
||||||
self::assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null);
|
self::assertEquals(100, $editedShortUrl['meta']['maxVisits'] ?? null);
|
||||||
|
|
|
@ -29,7 +29,7 @@ class ResolveShortUrlTest extends ApiTestCase
|
||||||
$visitResp = $this->callShortUrl($shortCode);
|
$visitResp = $this->callShortUrl($shortCode);
|
||||||
$fetchResp = $this->callApiWithKey(self::METHOD_GET, $url);
|
$fetchResp = $this->callApiWithKey(self::METHOD_GET, $url);
|
||||||
|
|
||||||
self::assertEquals(self::STATUS_NO_CONTENT, $editResp->getStatusCode());
|
self::assertEquals(self::STATUS_OK, $editResp->getStatusCode());
|
||||||
self::assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode());
|
self::assertEquals(self::STATUS_NOT_FOUND, $visitResp->getStatusCode());
|
||||||
self::assertEquals(self::STATUS_OK, $fetchResp->getStatusCode());
|
self::assertEquals(self::STATUS_OK, $fetchResp->getStatusCode());
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,7 +25,7 @@ class EditShortUrlActionTest extends TestCase
|
||||||
public function setUp(): void
|
public function setUp(): void
|
||||||
{
|
{
|
||||||
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
|
$this->shortUrlService = $this->prophesize(ShortUrlServiceInterface::class);
|
||||||
$this->action = new EditShortUrlAction($this->shortUrlService->reveal());
|
$this->action = new EditShortUrlAction($this->shortUrlService->reveal(), []);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @test */
|
/** @test */
|
||||||
|
@ -54,7 +54,7 @@ class EditShortUrlActionTest extends TestCase
|
||||||
|
|
||||||
$resp = $this->action->handle($request);
|
$resp = $this->action->handle($request);
|
||||||
|
|
||||||
self::assertEquals(204, $resp->getStatusCode());
|
self::assertEquals(200, $resp->getStatusCode());
|
||||||
$updateMeta->shouldHaveBeenCalled();
|
$updateMeta->shouldHaveBeenCalled();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue