Added option for enable/disable URL Validation by response status code.

This commit is contained in:
Mikolaj Gogula 2017-10-17 11:03:12 +02:00
parent 1009f9e7c6
commit 18d9815e88
6 changed files with 49 additions and 9 deletions

View File

@ -42,7 +42,14 @@ class UrlShortenerConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin
'Character set for generated short codes (leave empty to autogenerate one)', 'Character set for generated short codes (leave empty to autogenerate one)',
null, null,
true true
) ?: str_shuffle(UrlShortener::DEFAULT_CHARS) ) ?: str_shuffle(UrlShortener::DEFAULT_CHARS),
'VALIDATE_URL' => $this->questionHelper->ask(
$input,
$output,
new ConfirmationQuestion(
'<question>Do you want to validate long urls by 200 HTTP status code on response (Y/n):</question>'
)
)
]); ]);
} }
} }

View File

@ -189,6 +189,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
'SCHEMA' => $urlShortener['domain']['schema'], 'SCHEMA' => $urlShortener['domain']['schema'],
'HOSTNAME' => $urlShortener['domain']['hostname'], 'HOSTNAME' => $urlShortener['domain']['hostname'],
'CHARS' => $urlShortener['shortcode_chars'], 'CHARS' => $urlShortener['shortcode_chars'],
'VALIDATE_URL' => $urlShortener['validate_url'],
]); ]);
} }
} }
@ -240,6 +241,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
'hostname' => $this->urlShortener['HOSTNAME'], 'hostname' => $this->urlShortener['HOSTNAME'],
], ],
'shortcode_chars' => $this->urlShortener['CHARS'], 'shortcode_chars' => $this->urlShortener['CHARS'],
'validate_url' => $this->urlShortener['VALIDATE_URL']
], ],
]; ];

View File

@ -45,8 +45,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
'SCHEMA' => 'something', 'SCHEMA' => 'something',
'HOSTNAME' => 'something', 'HOSTNAME' => 'something',
'CHARS' => 'something', 'CHARS' => 'something',
'VALIDATE_URL' => 'something',
], $config->getUrlShortener()); ], $config->getUrlShortener());
$askSecret->shouldHaveBeenCalledTimes(3); $askSecret->shouldHaveBeenCalledTimes(4);
} }
/** /**
@ -64,6 +65,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
'SCHEMA' => 'bar', 'SCHEMA' => 'bar',
'HOSTNAME' => 'bar', 'HOSTNAME' => 'bar',
'CHARS' => 'bar', 'CHARS' => 'bar',
'VALIDATE_URL' => 'bar',
]); ]);
$this->plugin->process(new ArrayInput([]), new NullOutput(), $config); $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
@ -72,8 +74,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
'SCHEMA' => 'foo', 'SCHEMA' => 'foo',
'HOSTNAME' => 'foo', 'HOSTNAME' => 'foo',
'CHARS' => 'foo', 'CHARS' => 'foo',
'VALIDATE_URL' => false,
], $config->getUrlShortener()); ], $config->getUrlShortener());
$ask->shouldHaveBeenCalledTimes(4); $ask->shouldHaveBeenCalledTimes(5);
} }
/** /**
@ -89,6 +92,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
'SCHEMA' => 'foo', 'SCHEMA' => 'foo',
'HOSTNAME' => 'foo', 'HOSTNAME' => 'foo',
'CHARS' => 'foo', 'CHARS' => 'foo',
'VALIDATE_URL' => 'foo',
]); ]);
$this->plugin->process(new ArrayInput([]), new NullOutput(), $config); $this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
@ -97,6 +101,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
'SCHEMA' => 'foo', 'SCHEMA' => 'foo',
'HOSTNAME' => 'foo', 'HOSTNAME' => 'foo',
'CHARS' => 'foo', 'CHARS' => 'foo',
'VALIDATE_URL' => 'foo',
], $config->getUrlShortener()); ], $config->getUrlShortener());
$ask->shouldHaveBeenCalledTimes(1); $ask->shouldHaveBeenCalledTimes(1);
} }

View File

@ -73,7 +73,7 @@ class CacheFactoryTest extends TestCase
*/ */
public function filesystemCacheAdaptersReadDirOption() public function filesystemCacheAdaptersReadDirOption()
{ {
$dir = sys_get_temp_dir(); $dir = realpath(sys_get_temp_dir());
/** @var FilesystemCache $instance */ /** @var FilesystemCache $instance */
$instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), ''); $instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), '');
$this->assertInstanceOf(FilesystemCache::class, $instance); $this->assertInstanceOf(FilesystemCache::class, $instance);

View File

@ -36,6 +36,10 @@ class UrlShortener implements UrlShortenerInterface
* @var Cache * @var Cache
*/ */
private $cache; private $cache;
/**
* @var bool
*/
private $isUrlExistsValidation;
/** /**
* UrlShortener constructor. * UrlShortener constructor.
@ -43,19 +47,22 @@ class UrlShortener implements UrlShortenerInterface
* @param EntityManagerInterface $em * @param EntityManagerInterface $em
* @param Cache $cache * @param Cache $cache
* @param string $chars * @param string $chars
* @param bool $isUrlExistsValidation
* *
* @Inject({"httpClient", "em", Cache::class, "config.url_shortener.shortcode_chars"}) * @Inject({"httpClient", "em", Cache::class, "config.url_shortener.shortcode_chars", "config.url_shortener.validate_url"})
*/ */
public function __construct( public function __construct(
ClientInterface $httpClient, ClientInterface $httpClient,
EntityManagerInterface $em, EntityManagerInterface $em,
Cache $cache, Cache $cache,
$chars = self::DEFAULT_CHARS $chars = self::DEFAULT_CHARS,
$isUrlExistsValidation
) { ) {
$this->httpClient = $httpClient; $this->httpClient = $httpClient;
$this->em = $em; $this->em = $em;
$this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars; $this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars;
$this->cache = $cache; $this->cache = $cache;
$this->isUrlExistsValidation = $isUrlExistsValidation;
} }
/** /**
@ -77,8 +84,11 @@ class UrlShortener implements UrlShortenerInterface
return $shortUrl->getShortCode(); return $shortUrl->getShortCode();
} }
// Check that the URL exists // Check if the validation of url is enabled in the config
$this->checkUrlExists($url); if (true === $this->isUrlExistsValidation) {
// Check that the URL exists
$this->checkUrlExists($url);
}
// Transactionally insert the short url, then generate the short code and finally update the short code // Transactionally insert the short url, then generate the short code and finally update the short code
try { try {

View File

@ -58,7 +58,21 @@ class UrlShortenerTest extends TestCase
$this->cache = new ArrayCache(); $this->cache = new ArrayCache();
$this->urlShortener = new UrlShortener($this->httpClient->reveal(), $this->em->reveal(), $this->cache); $this->setUrlShortener(false);
}
/**
* @param bool $isUrlValidationExists
*/
public function setUrlShortener($isUrlValidationExists)
{
$this->urlShortener = new UrlShortener(
$this->httpClient->reveal(),
$this->em->reveal(),
$this->cache,
UrlShortener::DEFAULT_CHARS,
$isUrlValidationExists
);
} }
/** /**
@ -93,6 +107,8 @@ class UrlShortenerTest extends TestCase
*/ */
public function exceptionIsThrownWhenUrlDoesNotExist() public function exceptionIsThrownWhenUrlDoesNotExist()
{ {
$this->setUrlShortener(true);
$this->httpClient->request(Argument::cetera())->willThrow( $this->httpClient->request(Argument::cetera())->willThrow(
new ClientException('', $this->prophesize(Request::class)->reveal()) new ClientException('', $this->prophesize(Request::class)->reveal())
); );