mirror of
git://f0xx.org/ac/ac-ms-url-shortener
synced 2026-07-29 02:58:30 +03:00
36 lines
1.0 KiB
PHP
36 lines
1.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
require_once dirname(__DIR__) . '/src/UrlNormalizer.php';
|
|
require_once dirname(__DIR__) . '/src/SlugGenerator.php';
|
|
|
|
function assert_true(bool $cond, string $msg): void {
|
|
if (!$cond) {
|
|
fwrite(STDERR, "FAIL: $msg\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$n = UrlNormalizer::normalize('HTTPS://Example.COM/path?q=1#frag');
|
|
assert_true($n === 'https://example.com/path?q=1#frag', 'normalize host case');
|
|
|
|
$h = UrlNormalizer::originHash($n);
|
|
assert_true(strlen($h) === 64, 'origin hash length');
|
|
|
|
try {
|
|
UrlNormalizer::normalize('http://127.0.0.1/');
|
|
fwrite(STDERR, "FAIL: should block localhost\n");
|
|
exit(1);
|
|
} catch (InvalidArgumentException $e) {
|
|
// ok
|
|
}
|
|
|
|
$slug = SlugGenerator::baseSlug(42, 'https://example.com/a');
|
|
assert_true(strlen($slug) === 12, 'slug length 12');
|
|
assert_true(ctype_xdigit($slug), 'slug hex');
|
|
|
|
$candidates = SlugGenerator::candidates(1, 'https://a.test/');
|
|
assert_true(count($candidates) >= 2, 'collision candidates');
|
|
|
|
echo "OK url-shortener php tests\n";
|