mirror of
https://github.com/shlinkio/shlink.git
synced 2025-01-03 12:46:59 -06:00
Merge pull request #105 from gogus/feature/url-validation-option
Added option for enable/disable URL Validation by response status code.
This commit is contained in:
commit
ccb9d5e83a
@ -34,7 +34,8 @@
|
||||
"theorchard/monolog-cascade": "^0.4",
|
||||
"endroid/qrcode": "^1.7",
|
||||
"mikehaertl/phpwkhtmltopdf": "^2.2",
|
||||
"doctrine/migrations": "^1.4"
|
||||
"doctrine/migrations": "^1.4",
|
||||
"http-interop/http-middleware": "^0.4"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^5.7 || ^6.0",
|
||||
|
@ -10,6 +10,7 @@ return [
|
||||
'hostname' => Common\env('SHORTENED_URL_HOSTNAME'),
|
||||
],
|
||||
'shortcode_chars' => Common\env('SHORTCODE_CHARS', UrlShortener::DEFAULT_CHARS),
|
||||
'validate_url' => true,
|
||||
],
|
||||
|
||||
];
|
||||
|
@ -42,7 +42,14 @@ class UrlShortenerConfigCustomizerPlugin extends AbstractConfigCustomizerPlugin
|
||||
'Character set for generated short codes (leave empty to autogenerate one)',
|
||||
null,
|
||||
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>'
|
||||
)
|
||||
)
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
@ -189,6 +189,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
|
||||
'SCHEMA' => $urlShortener['domain']['schema'],
|
||||
'HOSTNAME' => $urlShortener['domain']['hostname'],
|
||||
'CHARS' => $urlShortener['shortcode_chars'],
|
||||
'VALIDATE_URL' => $urlShortener['validate_url'],
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -240,6 +241,7 @@ final class CustomizableAppConfig implements ArraySerializableInterface
|
||||
'hostname' => $this->urlShortener['HOSTNAME'],
|
||||
],
|
||||
'shortcode_chars' => $this->urlShortener['CHARS'],
|
||||
'validate_url' => $this->urlShortener['VALIDATE_URL']
|
||||
],
|
||||
];
|
||||
|
||||
|
@ -45,8 +45,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
|
||||
'SCHEMA' => 'something',
|
||||
'HOSTNAME' => 'something',
|
||||
'CHARS' => 'something',
|
||||
'VALIDATE_URL' => 'something',
|
||||
], $config->getUrlShortener());
|
||||
$askSecret->shouldHaveBeenCalledTimes(3);
|
||||
$askSecret->shouldHaveBeenCalledTimes(4);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -64,6 +65,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
|
||||
'SCHEMA' => 'bar',
|
||||
'HOSTNAME' => 'bar',
|
||||
'CHARS' => 'bar',
|
||||
'VALIDATE_URL' => 'bar',
|
||||
]);
|
||||
|
||||
$this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
|
||||
@ -72,8 +74,9 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
|
||||
'SCHEMA' => 'foo',
|
||||
'HOSTNAME' => 'foo',
|
||||
'CHARS' => 'foo',
|
||||
'VALIDATE_URL' => false,
|
||||
], $config->getUrlShortener());
|
||||
$ask->shouldHaveBeenCalledTimes(4);
|
||||
$ask->shouldHaveBeenCalledTimes(5);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,6 +92,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
|
||||
'SCHEMA' => 'foo',
|
||||
'HOSTNAME' => 'foo',
|
||||
'CHARS' => 'foo',
|
||||
'VALIDATE_URL' => 'foo',
|
||||
]);
|
||||
|
||||
$this->plugin->process(new ArrayInput([]), new NullOutput(), $config);
|
||||
@ -97,6 +101,7 @@ class UrlShortenerConfigCustomizerPluginTest extends TestCase
|
||||
'SCHEMA' => 'foo',
|
||||
'HOSTNAME' => 'foo',
|
||||
'CHARS' => 'foo',
|
||||
'VALIDATE_URL' => 'foo',
|
||||
], $config->getUrlShortener());
|
||||
$ask->shouldHaveBeenCalledTimes(1);
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ class CacheFactoryTest extends TestCase
|
||||
*/
|
||||
public function filesystemCacheAdaptersReadDirOption()
|
||||
{
|
||||
$dir = sys_get_temp_dir();
|
||||
$dir = realpath(sys_get_temp_dir());
|
||||
/** @var FilesystemCache $instance */
|
||||
$instance = $this->factory->__invoke($this->createSM(FilesystemCache::class, ['dir' => $dir]), '');
|
||||
$this->assertInstanceOf(FilesystemCache::class, $instance);
|
||||
|
@ -36,26 +36,39 @@ class UrlShortener implements UrlShortenerInterface
|
||||
* @var Cache
|
||||
*/
|
||||
private $cache;
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $urlValidationEnabled;
|
||||
|
||||
/**
|
||||
* UrlShortener constructor.
|
||||
* @param ClientInterface $httpClient
|
||||
* @param EntityManagerInterface $em
|
||||
* @param Cache $cache
|
||||
* @param bool $urlValidationEnabled
|
||||
* @param string $chars
|
||||
*
|
||||
* @Inject({"httpClient", "em", Cache::class, "config.url_shortener.shortcode_chars"})
|
||||
* @Inject({
|
||||
* "httpClient",
|
||||
* "em",
|
||||
* Cache::class,
|
||||
* "config.url_shortener.validate_url",
|
||||
* "config.url_shortener.shortcode_chars"
|
||||
* })
|
||||
*/
|
||||
public function __construct(
|
||||
ClientInterface $httpClient,
|
||||
EntityManagerInterface $em,
|
||||
Cache $cache,
|
||||
$urlValidationEnabled,
|
||||
$chars = self::DEFAULT_CHARS
|
||||
) {
|
||||
$this->httpClient = $httpClient;
|
||||
$this->em = $em;
|
||||
$this->chars = empty($chars) ? self::DEFAULT_CHARS : $chars;
|
||||
$this->cache = $cache;
|
||||
$this->urlValidationEnabled = $urlValidationEnabled;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -77,8 +90,11 @@ class UrlShortener implements UrlShortenerInterface
|
||||
return $shortUrl->getShortCode();
|
||||
}
|
||||
|
||||
// Check that the URL exists
|
||||
$this->checkUrlExists($url);
|
||||
// Check if the validation of url is enabled in the config
|
||||
if (true === $this->urlValidationEnabled) {
|
||||
// Check that the URL exists
|
||||
$this->checkUrlExists($url);
|
||||
}
|
||||
|
||||
// Transactionally insert the short url, then generate the short code and finally update the short code
|
||||
try {
|
||||
|
@ -58,7 +58,21 @@ class UrlShortenerTest extends TestCase
|
||||
|
||||
$this->cache = new ArrayCache();
|
||||
|
||||
$this->urlShortener = new UrlShortener($this->httpClient->reveal(), $this->em->reveal(), $this->cache);
|
||||
$this->setUrlShortener(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param bool $urlValidationEnabled
|
||||
*/
|
||||
public function setUrlShortener($urlValidationEnabled)
|
||||
{
|
||||
$this->urlShortener = new UrlShortener(
|
||||
$this->httpClient->reveal(),
|
||||
$this->em->reveal(),
|
||||
$this->cache,
|
||||
$urlValidationEnabled,
|
||||
UrlShortener::DEFAULT_CHARS
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -93,6 +107,8 @@ class UrlShortenerTest extends TestCase
|
||||
*/
|
||||
public function exceptionIsThrownWhenUrlDoesNotExist()
|
||||
{
|
||||
$this->setUrlShortener(true);
|
||||
|
||||
$this->httpClient->request(Argument::cetera())->willThrow(
|
||||
new ClientException('', $this->prophesize(Request::class)->reveal())
|
||||
);
|
||||
|
Loading…
Reference in New Issue
Block a user