import { Url } from './Url.js';
describe('Url', () => {
describe('Should_CreateNormalizedUrl_When_ValidInput', () => {
it('normalizes https URL', () => {
const url = Url.create('https://example.com/path');
expect(url.toString()).toBe('https://example.com/path');
});
it('converts http to https', () => {
const url = Url.create('http://example.com/path');
expect(url.toString()).toBe('https://example.com/path');
});
it('removes trailing slash', () => {
const url = Url.create('https://example.com/path/');
expect(url.toString()).toBe('https://example.com/path');
});
it('preserves query parameters', () => {
const url = Url.create('https://example.com/path?query=value&foo=bar');
expect(url.toString()).toBe('https://example.com/path?query=value&foo=bar');
});
it('preserves fragment', () => {
const url = Url.create('https://example.com/path#section');
expect(url.toString()).toBe('https://example.com/path#section');
});
});
describe('Should_ThrowError_When_InvalidUrl', () => {
it('throws error for non-URL string', () => {
expect(() => Url.create('not-a-url')).toThrow('Invalid URL format');
});
it('throws error for empty string', () => {
expect(() => Url.create('')).toThrow('Invalid URL format');
});
it('throws error for ftp protocol', () => {
expect(() => Url.create('ftp://example.com')).toThrow('Only HTTP and HTTPS protocols are supported');
});
it('throws error for file protocol', () => {
expect(() => Url.create('file:///path/to/file')).toThrow('Only HTTP and HTTPS protocols are supported');
});
});
describe('Should_HandleEquality_When_ComparingUrls', () => {
it('returns true for identical URLs', () => {
const url1 = Url.create('https://example.com/path');
const url2 = Url.create('https://example.com/path');
expect(url1.equals(url2)).toBe(true);
});
it('returns true for normalized equivalent URLs', () => {
const url1 = Url.create('http://example.com/path/');
const url2 = Url.create('https://example.com/path');
expect(url1.equals(url2)).toBe(true);
});
it('returns false for different URLs', () => {
const url1 = Url.create('https://example.com/path1');
const url2 = Url.create('https://example.com/path2');
expect(url1.equals(url2)).toBe(false);
});
});
describe('Should_ExtractDomain_When_Valid', () => {
it('extracts domain from simple URL', () => {
const url = Url.create('https://example.com/path');
expect(url.domain).toBe('example.com');
});
it('extracts domain with subdomain', () => {
const url = Url.create('https://blog.example.com/path');
expect(url.domain).toBe('blog.example.com');
});
it('extracts domain with port', () => {
const url = Url.create('https://example.com:8080/path');
expect(url.domain).toBe('example.com:8080');
});
});
});