coolify/tests/Unit/ValidHostnameTest.php
Andras Bacsai 988dd57cf4 feat(validation): make hostname validation case-insensitive and expand allowed characters
- Normalize hostnames to lowercase for RFC 1123 compliance while accepting uppercase input
- Expand NAME_PATTERN to allow parentheses, hash, comma, colon, and plus characters
- Add fallback to random name generation when application name doesn't meet minimum requirements
- Add comprehensive test coverage for validation patterns and edge cases
2026-03-24 08:03:08 +01:00

75 lines
2.8 KiB
PHP

<?php
use App\Rules\ValidHostname;
it('accepts valid RFC 1123 hostnames', function (string $hostname) {
$rule = new ValidHostname;
$failCalled = false;
$rule->validate('server_name', $hostname, function () use (&$failCalled) {
$failCalled = true;
});
expect($failCalled)->toBeFalse();
})->with([
'simple hostname' => 'myserver',
'hostname with hyphen' => 'my-server',
'hostname with numbers' => 'server123',
'hostname starting with number' => '123server',
'all numeric hostname' => '12345',
'fqdn' => 'server.example.com',
'subdomain' => 'web.app.example.com',
'max label length' => str_repeat('a', 63),
'max total length' => str_repeat('a', 63).'.'.str_repeat('b', 63).'.'.str_repeat('c', 63).'.'.str_repeat('d', 59),
'uppercase hostname' => 'MyServer',
'mixed case fqdn' => 'MyServer.Example.COM',
]);
it('rejects invalid RFC 1123 hostnames', function (string $hostname, string $expectedError) {
$rule = new ValidHostname;
$failCalled = false;
$errorMessage = '';
$rule->validate('server_name', $hostname, function ($message) use (&$failCalled, &$errorMessage) {
$failCalled = true;
$errorMessage = $message;
});
expect($failCalled)->toBeTrue();
expect($errorMessage)->toContain($expectedError);
})->with([
'underscore' => ['my_server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'starts with hyphen' => ['-myserver', 'cannot start or end with a hyphen'],
'ends with hyphen' => ['myserver-', 'cannot start or end with a hyphen'],
'starts with dot' => ['.myserver', 'cannot start or end with a dot'],
'ends with dot' => ['myserver.', 'cannot start or end with a dot'],
'consecutive dots' => ['my..server', 'consecutive dots'],
'too long total' => [str_repeat('a', 254), 'must not exceed 253 characters'],
'label too long' => [str_repeat('a', 64), 'must be 1-63 characters'],
'empty label' => ['my..server', 'consecutive dots'],
'special characters' => ['my@server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'space' => ['my server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
'shell metacharacters' => ['my;server', 'letters (a-z, A-Z), numbers (0-9), hyphens (-), and dots (.)'],
]);
it('accepts empty hostname', function () {
$rule = new ValidHostname;
$failCalled = false;
$rule->validate('server_name', '', function () use (&$failCalled) {
$failCalled = true;
});
expect($failCalled)->toBeFalse();
});
it('trims whitespace before validation', function () {
$rule = new ValidHostname;
$failCalled = false;
$rule->validate('server_name', ' myserver ', function () use (&$failCalled) {
$failCalled = true;
});
expect($failCalled)->toBeFalse();
});