|
| 1 | +/** |
| 2 | + * Copyright 2026 GitProxy Contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import { describe, it, expect } from 'vitest'; |
| 18 | +import { hostMatchesNoProxy } from '../src/proxy/routes'; |
| 19 | + |
| 20 | +describe('hostMatchesNoProxy', () => { |
| 21 | + describe('null / undefined / empty host', () => { |
| 22 | + it('returns false for null host', () => { |
| 23 | + expect(hostMatchesNoProxy(null, ['example.com'])).toBe(false); |
| 24 | + }); |
| 25 | + |
| 26 | + it('returns false for undefined host', () => { |
| 27 | + expect(hostMatchesNoProxy(undefined, ['example.com'])).toBe(false); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns false for empty string host', () => { |
| 31 | + expect(hostMatchesNoProxy('', ['example.com'])).toBe(false); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('empty noProxyList', () => { |
| 36 | + it('returns false when list is empty', () => { |
| 37 | + expect(hostMatchesNoProxy('github.com', [])).toBe(false); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('exact match', () => { |
| 42 | + it('matches host exactly', () => { |
| 43 | + expect(hostMatchesNoProxy('github.com', ['github.com'])).toBe(true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('does not match a different host', () => { |
| 47 | + expect(hostMatchesNoProxy('gitlab.com', ['github.com'])).toBe(false); |
| 48 | + }); |
| 49 | + |
| 50 | + it('strips port before matching', () => { |
| 51 | + expect(hostMatchesNoProxy('github.com:443', ['github.com'])).toBe(true); |
| 52 | + }); |
| 53 | + |
| 54 | + it('does not match a subdomain as exact', () => { |
| 55 | + expect(hostMatchesNoProxy('api.github.com', ['github.com'])).toBe(true); // suffix match applies |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('domain suffix match', () => { |
| 60 | + it('matches subdomain when pattern is the parent domain', () => { |
| 61 | + expect(hostMatchesNoProxy('api.github.com', ['github.com'])).toBe(true); |
| 62 | + }); |
| 63 | + |
| 64 | + it('matches deeply nested subdomain', () => { |
| 65 | + expect(hostMatchesNoProxy('foo.bar.corp.local', ['corp.local'])).toBe(true); |
| 66 | + }); |
| 67 | + |
| 68 | + it('does not match unrelated domain that happens to end with same string', () => { |
| 69 | + expect(hostMatchesNoProxy('notgithub.com', ['github.com'])).toBe(false); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe('leading dot in pattern', () => { |
| 74 | + it('strips leading dot and still matches subdomain', () => { |
| 75 | + expect(hostMatchesNoProxy('api.github.com', ['.github.com'])).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it('strips leading dot and still matches exact host', () => { |
| 79 | + expect(hostMatchesNoProxy('github.com', ['.github.com'])).toBe(true); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('wildcard pattern', () => { |
| 84 | + it('matches any host when pattern is *', () => { |
| 85 | + expect(hostMatchesNoProxy('anything.example.com', ['*'])).toBe(true); |
| 86 | + }); |
| 87 | + |
| 88 | + it('matches bare hostname when pattern is *', () => { |
| 89 | + expect(hostMatchesNoProxy('localhost', ['*'])).toBe(true); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + describe('blank / whitespace patterns', () => { |
| 94 | + it('ignores empty string pattern', () => { |
| 95 | + expect(hostMatchesNoProxy('github.com', [''])).toBe(false); |
| 96 | + }); |
| 97 | + |
| 98 | + it('ignores whitespace-only pattern', () => { |
| 99 | + expect(hostMatchesNoProxy('github.com', [' '])).toBe(false); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('multiple patterns', () => { |
| 104 | + it('returns true when host matches any pattern in the list', () => { |
| 105 | + expect(hostMatchesNoProxy('github.com', ['gitlab.com', 'github.com', 'bitbucket.org'])).toBe( |
| 106 | + true, |
| 107 | + ); |
| 108 | + }); |
| 109 | + |
| 110 | + it('returns false when host matches none of the patterns', () => { |
| 111 | + expect(hostMatchesNoProxy('github.com', ['gitlab.com', 'bitbucket.org'])).toBe(false); |
| 112 | + }); |
| 113 | + }); |
| 114 | +}); |
0 commit comments