mirror of
https://github.com/shlinkio/shlink.git
synced 2025-02-25 18:45:27 -06:00
Migrated PreviewAction to psr-15 middleware
This commit is contained in:
parent
7530048fbd
commit
46db736af8
@ -2,16 +2,16 @@
|
|||||||
namespace Shlinkio\Shlink\Core\Action;
|
namespace Shlinkio\Shlink\Core\Action;
|
||||||
|
|
||||||
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
use Acelaya\ZsmAnnotatedServices\Annotation\Inject;
|
||||||
|
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 Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
|
|
||||||
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
||||||
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface;
|
use Shlinkio\Shlink\Common\Service\PreviewGeneratorInterface;
|
||||||
use Shlinkio\Shlink\Common\Util\ResponseUtilsTrait;
|
use Shlinkio\Shlink\Common\Util\ResponseUtilsTrait;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
use Shlinkio\Shlink\Core\Service\UrlShortenerInterface;
|
||||||
use Zend\Stratigility\MiddlewareInterface;
|
|
||||||
|
|
||||||
class PreviewAction implements MiddlewareInterface
|
class PreviewAction implements MiddlewareInterface
|
||||||
{
|
{
|
||||||
@ -40,44 +40,28 @@ class PreviewAction 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)
|
||||||
{
|
{
|
||||||
$shortCode = $request->getAttribute('shortCode');
|
$shortCode = $request->getAttribute('shortCode');
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$url = $this->urlShortener->shortCodeToUrl($shortCode);
|
$url = $this->urlShortener->shortCodeToUrl($shortCode);
|
||||||
if (! isset($url)) {
|
if (! isset($url)) {
|
||||||
return $out($request, $response->withStatus(404), 'Not found');
|
return $delegate->process($request);
|
||||||
}
|
}
|
||||||
|
|
||||||
$imagePath = $this->previewGenerator->generatePreview($url);
|
$imagePath = $this->previewGenerator->generatePreview($url);
|
||||||
return $this->generateImageResponse($imagePath);
|
return $this->generateImageResponse($imagePath);
|
||||||
} catch (InvalidShortCodeException $e) {
|
} catch (InvalidShortCodeException $e) {
|
||||||
return $out($request, $response->withStatus(404), 'Not found');
|
return $delegate->process($request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
namespace ShlinkioTest\Shlink\Core\Action;
|
namespace ShlinkioTest\Shlink\Core\Action;
|
||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use Prophecy\Argument;
|
||||||
use Prophecy\Prophecy\ObjectProphecy;
|
use Prophecy\Prophecy\ObjectProphecy;
|
||||||
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
|
use Shlinkio\Shlink\Common\Exception\PreviewGenerationException;
|
||||||
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
use Shlinkio\Shlink\Common\Service\PreviewGenerator;
|
||||||
use Shlinkio\Shlink\Core\Action\PreviewAction;
|
use Shlinkio\Shlink\Core\Action\PreviewAction;
|
||||||
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
use Shlinkio\Shlink\Core\Exception\InvalidShortCodeException;
|
||||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
||||||
|
use ShlinkioTest\Shlink\Common\Util\TestUtils;
|
||||||
use Zend\Diactoros\Response;
|
use Zend\Diactoros\Response;
|
||||||
use Zend\Diactoros\ServerRequestFactory;
|
use Zend\Diactoros\ServerRequestFactory;
|
||||||
|
|
||||||
@ -36,20 +38,18 @@ class PreviewActionTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function invalidShortCodeFallbacksToNextMiddlewareWithStatusNotFound()
|
public function invalidShortCodeFallsBackToNextMiddleware()
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)->shouldBeCalledTimes(1);
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)->shouldBeCalledTimes(1);
|
||||||
|
$delegate = TestUtils::createDelegateMock();
|
||||||
|
|
||||||
$resp = $this->action->__invoke(
|
$this->action->process(
|
||||||
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
||||||
new Response(),
|
$delegate->reveal()
|
||||||
function ($req, $resp) {
|
|
||||||
return $resp;
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(404, $resp->getStatusCode());
|
$delegate->process(Argument::cetera())->shouldHaveBeenCalledTimes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -63,9 +63,9 @@ class PreviewActionTest extends TestCase
|
|||||||
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($url)->shouldBeCalledTimes(1);
|
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($url)->shouldBeCalledTimes(1);
|
||||||
$this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledTimes(1);
|
$this->previewGenerator->generatePreview($url)->willReturn($path)->shouldBeCalledTimes(1);
|
||||||
|
|
||||||
$resp = $this->action->__invoke(
|
$resp = $this->action->process(
|
||||||
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
||||||
new Response()
|
TestUtils::createDelegateMock()->reveal()
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
|
$this->assertEquals(filesize($path), $resp->getHeaderLine('Content-length'));
|
||||||
@ -75,20 +75,18 @@ class PreviewActionTest extends TestCase
|
|||||||
/**
|
/**
|
||||||
* @test
|
* @test
|
||||||
*/
|
*/
|
||||||
public function invalidShortcodeExceptionReturnsNotFound()
|
public function invalidShortcodeExceptionFallsBackToNextMiddleware()
|
||||||
{
|
{
|
||||||
$shortCode = 'abc123';
|
$shortCode = 'abc123';
|
||||||
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
|
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(InvalidShortCodeException::class)
|
||||||
->shouldBeCalledTimes(1);
|
->shouldBeCalledTimes(1);
|
||||||
|
$delegate = TestUtils::createDelegateMock();
|
||||||
|
|
||||||
$resp = $this->action->__invoke(
|
$this->action->process(
|
||||||
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode),
|
||||||
new Response(),
|
$delegate->reveal()
|
||||||
function ($req, $resp) {
|
|
||||||
return $resp;
|
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->assertEquals(404, $resp->getStatusCode());
|
$delegate->process(Argument::any())->shouldHaveBeenCalledTimes(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user