Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368import { URL, InvalidURLError } from '../../../domain/value-objects/URL';
describe('URL Value Object', () => { describe('create', () => { describe('valid URLs', () => { it('should create URL with valid http URL', () => { const result = URL.create('http://example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('http://example.com/'); });
it('should create URL with valid https URL', () => { const result = URL.create('https://example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/'); });
it('should create URL with subdomain', () => { const result = URL.create('https://www.example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://www.example.com/'); });
it('should create URL with port', () => { const result = URL.create('https://example.com:8080');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com:8080/'); }); });
describe('trailing slash normalization', () => { describe('should add trailing slash to bare root domains', () => { it('should add trailing slash to http://example.com', () => { const result = URL.create('http://example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('http://example.com/'); });
it('should add trailing slash to https://example.com', () => { const result = URL.create('https://example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/'); });
it('should add trailing slash to https://www.example.com', () => { const result = URL.create('https://www.example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://www.example.com/'); });
it('should add trailing slash to https://example.com:8080', () => { const result = URL.create('https://example.com:8080');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com:8080/'); });
it('should add trailing slash to https://sub.domain.example.com', () => { const result = URL.create('https://sub.domain.example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://sub.domain.example.com/'); }); });
describe('should NOT add trailing slash when already present', () => { it('should not add trailing slash to https://example.com/', () => { const result = URL.create('https://example.com/');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/'); });
it('should not add trailing slash to https://www.example.com/', () => { const result = URL.create('https://www.example.com/');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://www.example.com/'); });
it('should not add trailing slash to https://example.com:8080/', () => { const result = URL.create('https://example.com:8080/');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com:8080/'); }); });
describe('should NOT add trailing slash to URLs with paths', () => { it('should not add trailing slash to https://example.com/path', () => { const result = URL.create('https://example.com/path');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/path'); });
it('should not add trailing slash to https://example.com/path/subpath', () => { const result = URL.create('https://example.com/path/subpath');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com/path/subpath', ); });
it('should not add trailing slash to https://example.com/path/', () => { const result = URL.create('https://example.com/path/');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/path/'); });
it('should not add trailing slash to https://example.com/path.html', () => { const result = URL.create('https://example.com/path.html');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/path.html'); });
it('should not add trailing slash to https://example.com/api/v1/users', () => { const result = URL.create('https://example.com/api/v1/users');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com/api/v1/users', ); }); });
describe('should NOT add trailing slash to URLs with query parameters', () => { it('should not add trailing slash to https://example.com?param=value', () => { const result = URL.create('https://example.com?param=value');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com?param=value'); });
it('should not add trailing slash to https://example.com?param1=value1¶m2=value2', () => { const result = URL.create( 'https://example.com?param1=value1¶m2=value2', );
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com?param1=value1¶m2=value2', ); });
it('should not add trailing slash to https://example.com/?param=value', () => { const result = URL.create('https://example.com/?param=value');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com/?param=value', ); });
it('should not add trailing slash to https://example.com/path?param=value', () => { const result = URL.create('https://example.com/path?param=value');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com/path?param=value', ); }); });
describe('should NOT add trailing slash to URLs with fragments', () => { it('should not add trailing slash to https://example.com#section', () => { const result = URL.create('https://example.com#section');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com#section'); });
it('should not add trailing slash to https://example.com/#section', () => { const result = URL.create('https://example.com/#section');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/#section'); });
it('should not add trailing slash to https://example.com/path#section', () => { const result = URL.create('https://example.com/path#section');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com/path#section', ); });
it('should not add trailing slash to https://example.com?param=value#section', () => { const result = URL.create('https://example.com?param=value#section');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com?param=value#section', ); }); });
describe('should NOT add trailing slash to URLs with query parameters AND fragments', () => { it('should not add trailing slash to https://example.com?param=value#section', () => { const result = URL.create('https://example.com?param=value#section');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com?param=value#section', ); });
it('should not add trailing slash to https://example.com/path?param=value#section', () => { const result = URL.create( 'https://example.com/path?param=value#section', );
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe( 'https://example.com/path?param=value#section', ); }); }); });
describe('edge cases', () => { it('should handle URLs with IP addresses', () => { const result = URL.create('http://192.168.1.1');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('http://192.168.1.1/'); });
it('should handle URLs with IP addresses and ports', () => { const result = URL.create('http://192.168.1.1:8080');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('http://192.168.1.1:8080/'); });
it('should handle localhost', () => { const result = URL.create('http://localhost');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('http://localhost/'); });
it('should handle localhost with port', () => { const result = URL.create('http://localhost:3000');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('http://localhost:3000/'); });
it('should trim whitespace before processing', () => { const result = URL.create(' https://example.com ');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com/'); });
it('should handle URLs with authentication', () => { const result = URL.create('https://user:pass@example.com');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://user:pass@example.com/'); }); it('should handle URLs with multiple slashes', () => { const result = URL.create('https://example.com//');
expect(result.isOk()).toBe(true); expect(result.unwrap().value).toBe('https://example.com//'); }); });
describe('invalid URLs', () => { it('should fail for empty string', () => { const result = URL.create('');
if (!result.isErr()) { throw new Error('Expected result to be an error'); } expect(result.isErr()).toBe(true); expect(result.error).toBeInstanceOf(InvalidURLError); expect(result.error.message).toBe('URL cannot be empty'); });
it('should fail for whitespace only', () => { const result = URL.create(' ');
if (!result.isErr()) { throw new Error('Expected result to be an error'); } expect(result.isErr()).toBe(true); expect(result.error).toBeInstanceOf(InvalidURLError); expect(result.error.message).toBe('URL cannot be empty'); });
it('should fail for invalid URL format', () => { const result = URL.create('not-a-url');
if (!result.isErr()) { throw new Error('Expected result to be an error'); } expect(result.isErr()).toBe(true); expect(result.error).toBeInstanceOf(InvalidURLError); expect(result.error.message).toBe('Invalid URL format'); });
it('should fail for URL without protocol', () => { const result = URL.create('example.com');
if (!result.isErr()) { throw new Error('Expected result to be an error'); } expect(result.isErr()).toBe(true); expect(result.error).toBeInstanceOf(InvalidURLError); expect(result.error.message).toBe('Invalid URL format'); });
it('should fail for malformed URL', () => { const result = URL.create('https://');
if (!result.isErr()) { throw new Error('Expected result to be an error'); } expect(result.isErr()).toBe(true); expect(result.error).toBeInstanceOf(InvalidURLError); expect(result.error.message).toBe('Invalid URL format'); }); }); });
describe('toString', () => { it('should return the URL value as string', () => { const url = URL.create('https://example.com/path').unwrap();
expect(url.toString()).toBe('https://example.com/path'); });
it('should return normalized URL with trailing slash for bare domains', () => { const url = URL.create('https://example.com').unwrap();
expect(url.toString()).toBe('https://example.com/'); }); });
describe('value getter', () => { it('should return the URL value', () => { const url = URL.create('https://example.com/path').unwrap();
expect(url.value).toBe('https://example.com/path'); });
it('should return normalized URL with trailing slash for bare domains', () => { const url = URL.create('https://example.com').unwrap();
expect(url.value).toBe('https://example.com/'); }); });});