|
1 | 1 | import { describe, expect, it } from 'bun:test' |
2 | 2 | import { |
| 3 | + getHostName, |
3 | 4 | isDefined, |
4 | 5 | isFunction, |
5 | 6 | isNumber, |
@@ -92,3 +93,36 @@ describe('isString', () => { |
92 | 93 | expect(isString({})).toBe(false) |
93 | 94 | }) |
94 | 95 | }) |
| 96 | +describe('getHostName', () => { |
| 97 | + it('should return the host for a standard URL', () => { |
| 98 | + expect(getHostName('https://www.example.com/path')).toBe('example.com') |
| 99 | + }) |
| 100 | + |
| 101 | + it('should return the host for a URL with a subdomain', () => { |
| 102 | + expect(getHostName('http://sub.domain.co.uk/page?q=1')).toBe( |
| 103 | + 'sub.domain.co.uk', |
| 104 | + ) |
| 105 | + }) |
| 106 | + |
| 107 | + it('should return the host for a URL without a path', () => { |
| 108 | + expect(getHostName('https://anothersite.org')).toBe('anothersite.org') |
| 109 | + }) |
| 110 | + |
| 111 | + it('should throw an error for an invalid URL string', () => { |
| 112 | + const invalidUrl = 'not a url' |
| 113 | + expect(() => getHostName(invalidUrl)).toThrow( |
| 114 | + new Error(`Invalid URL: ${invalidUrl}`), |
| 115 | + ) |
| 116 | + }) |
| 117 | + |
| 118 | + it('should throw an error for an empty string', () => { |
| 119 | + expect(() => getHostName('')).toThrow(new Error('Invalid URL: ')) |
| 120 | + }) |
| 121 | + |
| 122 | + it('should throw an error for a string that looks like a host but lacks a protocol', () => { |
| 123 | + const urlWithoutProtocol = 'example.com' |
| 124 | + expect(() => getHostName(urlWithoutProtocol)).toThrow( |
| 125 | + new Error(`Invalid URL: ${urlWithoutProtocol}`), |
| 126 | + ) |
| 127 | + }) |
| 128 | +}) |
0 commit comments