|
| 1 | +import {test} from 'node:test'; |
| 2 | +import assert from 'node:assert/strict'; |
| 3 | + |
| 4 | +// Test the URL parsing logic |
| 5 | +test('open option URL parsing', () => { |
| 6 | + // Test absolute URL detection |
| 7 | + const absoluteUrls = [ |
| 8 | + 'http://localhost:3000', |
| 9 | + 'https://example.com', |
| 10 | + 'http://127.0.0.1:8080/path', |
| 11 | + ]; |
| 12 | + |
| 13 | + for (const url of absoluteUrls) { |
| 14 | + try { |
| 15 | + // eslint-disable-next-line no-new |
| 16 | + new URL(url); |
| 17 | + assert.ok(true, `${url} is correctly identified as absolute`); |
| 18 | + } catch { |
| 19 | + assert.fail(`${url} should be parsed as absolute URL`); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + // Test relative path detection |
| 24 | + const relativePaths = [ |
| 25 | + '/about', |
| 26 | + 'page.php', |
| 27 | + 'admin/dashboard', |
| 28 | + 'index.html', |
| 29 | + ]; |
| 30 | + |
| 31 | + for (const path of relativePaths) { |
| 32 | + try { |
| 33 | + // eslint-disable-next-line no-new |
| 34 | + new URL(path); |
| 35 | + assert.fail(`${path} should not be parsed as absolute URL`); |
| 36 | + } catch { |
| 37 | + assert.ok(true, `${path} is correctly identified as relative`); |
| 38 | + } |
| 39 | + } |
| 40 | +}); |
| 41 | + |
| 42 | +test('pathname extraction from absolute URLs', () => { |
| 43 | + const testCases = [ |
| 44 | + {url: 'http://localhost:3000', expectedPath: '/'}, |
| 45 | + {url: 'http://localhost:3000/', expectedPath: '/'}, |
| 46 | + {url: 'http://localhost:3000/about', expectedPath: '/about'}, |
| 47 | + {url: 'https://example.com/path/to/page', expectedPath: '/path/to/page'}, |
| 48 | + ]; |
| 49 | + |
| 50 | + for (const {url, expectedPath} of testCases) { |
| 51 | + const parsedUrl = new URL(url); |
| 52 | + assert.equal(parsedUrl.pathname, expectedPath, `Pathname for ${url} should be ${expectedPath}`); |
| 53 | + } |
| 54 | +}); |
0 commit comments