@mdfriday/foundry
Version:
The core engine of MDFriday. Convert Markdown and shortcodes into fully themed static sites – Hugo-style, powered by TypeScript.
138 lines • 7.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const baseurl_1 = require("./baseurl");
describe('BaseURL', () => {
describe('fromString', () => {
it('should handle absolute HTTPS URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com/');
expect(baseUrl.toString()).toBe('https://example.com/');
expect(baseUrl.isAbsoluteURL()).toBe(true);
expect(baseUrl.isRelativeURL()).toBe(false);
expect(baseUrl.protocol()).toBe('https:');
expect(baseUrl.hostname()).toBe('example.com');
expect(baseUrl.basePath).toBe('/');
});
it('should handle absolute HTTP URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('http://example.com/');
expect(baseUrl.toString()).toBe('http://example.com/');
expect(baseUrl.isAbsoluteURL()).toBe(true);
expect(baseUrl.protocol()).toBe('http:');
expect(baseUrl.hostname()).toBe('example.com');
});
it('should handle absolute URLs with paths', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com/public/');
expect(baseUrl.toString()).toBe('https://example.com/public/');
expect(baseUrl.basePath).toBe('/public/');
expect(baseUrl.basePathNoTrailingSlash).toBe('/public');
expect(baseUrl.withoutPath).toBe('https://example.com');
});
it('should handle absolute URLs without trailing slash', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com/public');
expect(baseUrl.toString()).toBe('https://example.com/public/');
expect(baseUrl.basePath).toBe('/public/');
});
it('should handle relative paths', () => {
const baseUrl = baseurl_1.BaseURL.fromString('/public/');
expect(baseUrl.toString()).toBe('/public/');
expect(baseUrl.isRelativeURL()).toBe(true);
expect(baseUrl.isAbsoluteURL()).toBe(false);
expect(baseUrl.basePath).toBe('/public/');
expect(baseUrl.hostURL()).toBe('/');
});
it('should handle relative paths without trailing slash', () => {
const baseUrl = baseurl_1.BaseURL.fromString('/public');
expect(baseUrl.toString()).toBe('/public/');
expect(baseUrl.basePath).toBe('/public/');
});
it('should handle root path', () => {
const baseUrl = baseurl_1.BaseURL.fromString('/');
expect(baseUrl.toString()).toBe('/');
expect(baseUrl.isRelativeURL()).toBe(true);
expect(baseUrl.basePath).toBe('/');
});
it('should handle empty string as root', () => {
const baseUrl = baseurl_1.BaseURL.fromString('');
expect(baseUrl.toString()).toBe('/');
expect(baseUrl.isRelativeURL()).toBe(true);
});
it('should handle protocol-relative URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('//example.com/public/');
expect(baseUrl.toString()).toBe('https://example.com/public/');
expect(baseUrl.isAbsoluteURL()).toBe(true);
expect(baseUrl.protocol()).toBe('https:');
});
it('should handle domain names without protocol', () => {
const baseUrl = baseurl_1.BaseURL.fromString('example.com');
expect(baseUrl.toString()).toBe('https://example.com/');
expect(baseUrl.isAbsoluteURL()).toBe(true);
expect(baseUrl.protocol()).toBe('https:');
});
it('should handle domain names with path but no protocol', () => {
const baseUrl = baseurl_1.BaseURL.fromString('example.com/public');
expect(baseUrl.toString()).toBe('https://example.com/public/');
expect(baseUrl.basePath).toBe('/public/');
});
it('should reject unsupported protocols', () => {
expect(() => baseurl_1.BaseURL.fromString('ftp://example.com/')).toThrow('Unsupported protocol: ftp:');
expect(() => baseurl_1.BaseURL.fromString('mailto:test@example.com')).toThrow('Unsupported protocol: mailto:');
});
it('should handle URLs with ports', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com:8080/');
expect(baseUrl.toString()).toBe('https://example.com:8080/');
expect(baseUrl.port()).toBe(8080);
expect(baseUrl.host()).toBe('example.com:8080');
});
});
describe('getRoot', () => {
it('should return correct root for absolute URLs with absolute paths', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com/public/');
expect(baseUrl.getRoot('/assets/style.css')).toBe('https://example.com');
expect(baseUrl.getRoot('style.css')).toBe('https://example.com/public/');
});
it('should return correct root for relative base URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('/public/');
expect(baseUrl.getRoot('/assets/style.css')).toBe('/public/');
expect(baseUrl.getRoot('style.css')).toBe('/public/');
});
it('should return correct root for root relative base URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('/');
expect(baseUrl.getRoot('/assets/style.css')).toBe('/');
expect(baseUrl.getRoot('style.css')).toBe('/');
});
});
describe('withProtocol', () => {
it('should change protocol for absolute URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com/');
const newUrl = baseUrl.withProtocol('http://');
expect(newUrl.toString()).toBe('http://example.com/');
expect(newUrl.protocol()).toBe('http:');
});
it('should throw error for relative URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('/public/');
expect(() => baseUrl.withProtocol('https://')).toThrow('Cannot change protocol of relative URL');
});
});
describe('withPort', () => {
it('should change port for absolute URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com/');
const newUrl = baseUrl.withPort(8080);
expect(newUrl.toString()).toBe('https://example.com:8080/');
expect(newUrl.port()).toBe(8080);
});
it('should throw error for relative URLs', () => {
const baseUrl = baseurl_1.BaseURL.fromString('/public/');
expect(() => baseUrl.withPort(8080)).toThrow('Cannot set port on relative URL');
});
});
describe('edge cases', () => {
it('should handle whitespace in input', () => {
const baseUrl = baseurl_1.BaseURL.fromString(' https://example.com/public/ ');
expect(baseUrl.toString()).toBe('https://example.com/public/');
});
it('should handle multiple slashes', () => {
const baseUrl = baseurl_1.BaseURL.fromString('https://example.com//public//');
expect(baseUrl.toString()).toBe('https://example.com//public//');
});
});
});
//# sourceMappingURL=baseurl.test.js.map