mirror of
https://github.com/shlinkio/shlink.git
synced 2024-11-23 21:27:44 +03:00
Created RedirectActionTest
This commit is contained in:
parent
fcdcfde04f
commit
ebeaa3c64a
2 changed files with 118 additions and 3 deletions
|
@ -70,10 +70,10 @@ class RedirectAction implements MiddlewareInterface
|
|||
// If provided shortCode does not belong to a valid long URL, dispatch next middleware, which will trigger
|
||||
// a not-found error
|
||||
if (! isset($longUrl)) {
|
||||
return $out($request, $response->withStatus(404), 'Not found');
|
||||
return $this->notFoundResponse($request, $response, $out);
|
||||
}
|
||||
|
||||
// Track visit to this shortcode
|
||||
// Track visit to this short code
|
||||
$this->visitTracker->track($shortCode);
|
||||
|
||||
// Return a redirect response to the long URL.
|
||||
|
@ -81,7 +81,18 @@ class RedirectAction implements MiddlewareInterface
|
|||
return new RedirectResponse($longUrl);
|
||||
} catch (\Exception $e) {
|
||||
// In case of error, dispatch 404 error
|
||||
return $out($request, $response);
|
||||
return $this->notFoundResponse($request, $response, $out);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @param callable $out
|
||||
* @return Response
|
||||
*/
|
||||
protected function notFoundResponse(Request $request, Response $response, callable $out)
|
||||
{
|
||||
return $out($request, $response->withStatus(404), 'Not Found');
|
||||
}
|
||||
}
|
||||
|
|
104
module/Core/test/Action/RedirectActionTest.php
Normal file
104
module/Core/test/Action/RedirectActionTest.php
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
namespace ShlinkioTest\Shlink\Core\Action;
|
||||
|
||||
use PHPUnit_Framework_TestCase as TestCase;
|
||||
use Prophecy\Argument;
|
||||
use Prophecy\Prophecy\ObjectProphecy;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Shlinkio\Shlink\Core\Action\RedirectAction;
|
||||
use Shlinkio\Shlink\Core\Service\UrlShortener;
|
||||
use Shlinkio\Shlink\Core\Service\VisitsTracker;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequestFactory;
|
||||
|
||||
class RedirectActionTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var RedirectAction
|
||||
*/
|
||||
protected $action;
|
||||
/**
|
||||
* @var ObjectProphecy
|
||||
*/
|
||||
protected $urlShortener;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
$this->urlShortener = $this->prophesize(UrlShortener::class);
|
||||
$visitTracker = $this->prophesize(VisitsTracker::class);
|
||||
$visitTracker->track(Argument::any());
|
||||
$this->action = new RedirectAction($this->urlShortener->reveal(), $visitTracker->reveal());
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function redirectionIsPerformedToLongUrl()
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$expectedUrl = 'http://domain.com/foo/bar';
|
||||
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn($expectedUrl)
|
||||
->shouldBeCalledTimes(1);
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
|
||||
$response = $this->action->__invoke($request, new Response());
|
||||
|
||||
$this->assertInstanceOf(Response\RedirectResponse::class, $response);
|
||||
$this->assertEquals(302, $response->getStatusCode());
|
||||
$this->assertTrue($response->hasHeader('Location'));
|
||||
$this->assertEquals($expectedUrl, $response->getHeaderLine('Location'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function nextErrorMiddlewareIsInvokedIfLongUrlIsNotFound()
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$this->urlShortener->shortCodeToUrl($shortCode)->willReturn(null)
|
||||
->shouldBeCalledTimes(1);
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
|
||||
$originalResponse = new Response();
|
||||
$test = $this;
|
||||
$this->action->__invoke($request, $originalResponse, function (
|
||||
ServerRequestInterface $req,
|
||||
ResponseInterface $resp,
|
||||
$error
|
||||
) use (
|
||||
$test,
|
||||
$request
|
||||
) {
|
||||
$test->assertSame($request, $req);
|
||||
$test->assertEquals(404, $resp->getStatusCode());
|
||||
$test->assertEquals('Not Found', $error);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function nextErrorMiddlewareIsInvokedIfAnExceptionIsThrown()
|
||||
{
|
||||
$shortCode = 'abc123';
|
||||
$this->urlShortener->shortCodeToUrl($shortCode)->willThrow(\Exception::class)
|
||||
->shouldBeCalledTimes(1);
|
||||
|
||||
$request = ServerRequestFactory::fromGlobals()->withAttribute('shortCode', $shortCode);
|
||||
$originalResponse = new Response();
|
||||
$test = $this;
|
||||
$this->action->__invoke($request, $originalResponse, function (
|
||||
ServerRequestInterface $req,
|
||||
ResponseInterface $resp,
|
||||
$error
|
||||
) use (
|
||||
$test,
|
||||
$request
|
||||
) {
|
||||
$test->assertSame($request, $req);
|
||||
$test->assertEquals(404, $resp->getStatusCode());
|
||||
$test->assertEquals('Not Found', $error);
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue