|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + * Licensed under the MIT License. See License.txt in the project root for license information. |
| 4 | + *--------------------------------------------------------------------------------------------*/ |
| 5 | + |
| 6 | +import assert from 'assert'; |
| 7 | +import { URI } from '../../../../base/common/uri.js'; |
| 8 | +import { testUrlMatchesGlob } from '../../common/urlGlob.js'; |
| 9 | +import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../base/test/common/utils.js'; |
| 10 | + |
| 11 | +suite('urlGlob', () => { |
| 12 | + |
| 13 | + ensureNoDisposablesAreLeakedInTestSuite(); |
| 14 | + |
| 15 | + suite('testUrlMatchesGlob', () => { |
| 16 | + |
| 17 | + test('exact match', () => { |
| 18 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://example.com'), true); |
| 19 | + assert.strictEqual(testUrlMatchesGlob('http://example.com', 'http://example.com'), true); |
| 20 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/path', 'https://example.com/path'), true); |
| 21 | + }); |
| 22 | + |
| 23 | + test('trailing slashes are ignored', () => { |
| 24 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/', 'https://example.com'), true); |
| 25 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://example.com/'), true); |
| 26 | + assert.strictEqual(testUrlMatchesGlob('https://example.com//', 'https://example.com'), true); |
| 27 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/path/', 'https://example.com/path'), true); |
| 28 | + }); |
| 29 | + |
| 30 | + test('query and fragment are ignored', () => { |
| 31 | + assert.strictEqual(testUrlMatchesGlob('https://example.com?query=value', 'https://example.com'), true); |
| 32 | + assert.strictEqual(testUrlMatchesGlob('https://example.com#fragment', 'https://example.com'), true); |
| 33 | + assert.strictEqual(testUrlMatchesGlob('https://example.com?query=value#fragment', 'https://example.com'), true); |
| 34 | + }); |
| 35 | + |
| 36 | + test('scheme matching', () => { |
| 37 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://example.com'), true); |
| 38 | + assert.strictEqual(testUrlMatchesGlob('http://example.com', 'https://example.com'), false); |
| 39 | + assert.strictEqual(testUrlMatchesGlob('ftp://example.com', 'https://example.com'), false); |
| 40 | + }); |
| 41 | + |
| 42 | + test('glob without scheme assumes http/https', () => { |
| 43 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'example.com'), true); |
| 44 | + assert.strictEqual(testUrlMatchesGlob('http://example.com', 'example.com'), true); |
| 45 | + assert.strictEqual(testUrlMatchesGlob('ftp://example.com', 'example.com'), false); |
| 46 | + }); |
| 47 | + |
| 48 | + test('wildcard matching in path', () => { |
| 49 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/anything', 'https://example.com/*'), true); |
| 50 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/path/to/resource', 'https://example.com/*'), true); |
| 51 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/path/to/resource', 'https://example.com/path/*'), true); |
| 52 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/path/to/resource', 'https://example.com/path/*/resource'), true); |
| 53 | + }); |
| 54 | + |
| 55 | + test('subdomain wildcard matching', () => { |
| 56 | + assert.strictEqual(testUrlMatchesGlob('https://sub.example.com', 'https://*.example.com'), true); |
| 57 | + assert.strictEqual(testUrlMatchesGlob('https://sub.domain.example.com', 'https://*.example.com'), true); |
| 58 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://*.example.com'), true); |
| 59 | + // *. matches any number of characters before the domain, including other domains |
| 60 | + assert.strictEqual(testUrlMatchesGlob('https://notexample.com', 'https://*.example.com'), true); |
| 61 | + }); |
| 62 | + |
| 63 | + test('port matching', () => { |
| 64 | + assert.strictEqual(testUrlMatchesGlob('https://example.com:8080', 'https://example.com:8080'), true); |
| 65 | + assert.strictEqual(testUrlMatchesGlob('https://example.com:8080', 'https://example.com:9090'), false); |
| 66 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://example.com:8080'), false); |
| 67 | + }); |
| 68 | + |
| 69 | + test('wildcard port matching', () => { |
| 70 | + assert.strictEqual(testUrlMatchesGlob('https://example.com:8080', 'https://example.com:*'), true); |
| 71 | + assert.strictEqual(testUrlMatchesGlob('https://example.com:9090', 'https://example.com:*'), true); |
| 72 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://example.com:*'), true); |
| 73 | + assert.strictEqual(testUrlMatchesGlob('https://example.com:8080/path', 'https://example.com:*/path'), true); |
| 74 | + }); |
| 75 | + |
| 76 | + test('root path glob', () => { |
| 77 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://example.com/'), true); |
| 78 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/', 'https://example.com/'), true); |
| 79 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/path', 'https://example.com/'), true); |
| 80 | + }); |
| 81 | + |
| 82 | + test('mismatch cases', () => { |
| 83 | + assert.strictEqual(testUrlMatchesGlob('https://example.com/path', 'https://example.com/other'), false); |
| 84 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://other.com'), false); |
| 85 | + assert.strictEqual(testUrlMatchesGlob('https://sub.example.com', 'https://example.com'), false); |
| 86 | + }); |
| 87 | + |
| 88 | + test('URI object input', () => { |
| 89 | + const uri = URI.parse('https://example.com/path'); |
| 90 | + assert.strictEqual(testUrlMatchesGlob(uri, 'https://example.com/path'), true); |
| 91 | + assert.strictEqual(testUrlMatchesGlob(uri, 'https://example.com/*'), true); |
| 92 | + }); |
| 93 | + |
| 94 | + test('complex patterns', () => { |
| 95 | + assert.strictEqual(testUrlMatchesGlob('https://api.github.com/repos/microsoft/vscode', 'https://*.github.com/repos/*/*'), true); |
| 96 | + assert.strictEqual(testUrlMatchesGlob('https://github.com/microsoft/vscode', 'https://*.github.com/repos/*/*'), false); |
| 97 | + assert.strictEqual(testUrlMatchesGlob('https://api.github.com:443/repos/microsoft/vscode', 'https://*.github.com:*/repos/*/*'), true); |
| 98 | + }); |
| 99 | + |
| 100 | + test('edge cases', () => { |
| 101 | + // Wildcard after authority doesn't match without additional path |
| 102 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', 'https://example.com*'), false); |
| 103 | + assert.strictEqual(testUrlMatchesGlob('https://example.com.extra', 'https://example.com*'), true); |
| 104 | + assert.strictEqual(testUrlMatchesGlob('https://example.com', '*'), true); |
| 105 | + }); |
| 106 | + }); |
| 107 | +}); |
0 commit comments