|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import assert from 'node:assert'; |
| 10 | +import { mkdtemp, rm, writeFile } from 'node:fs/promises'; |
| 11 | +import { join } from 'node:path'; |
| 12 | +import { loadProxyConfiguration } from './load-proxy-config'; |
| 13 | + |
| 14 | +describe('loadProxyConfiguration', () => { |
| 15 | + let tempDir: string; |
| 16 | + |
| 17 | + beforeEach(async () => { |
| 18 | + const baseTmpDir = process.env['TEST_TMPDIR']; |
| 19 | + assert(baseTmpDir, 'TEST_TMPDIR is not set'); |
| 20 | + tempDir = await mkdtemp(join(baseTmpDir, 'angular-proxy-config-spec-')); |
| 21 | + }); |
| 22 | + |
| 23 | + afterEach(async () => { |
| 24 | + await rm(tempDir, { recursive: true, force: true }); |
| 25 | + }); |
| 26 | + |
| 27 | + async function createGlobMatcher(glob: string): Promise<(url: string) => boolean> { |
| 28 | + const configPath = join(tempDir, 'proxy.conf.json'); |
| 29 | + await writeFile(configPath, JSON.stringify({ [glob]: { target: 'http://localhost:3000' } })); |
| 30 | + const proxy = await loadProxyConfiguration(tempDir, 'proxy.conf.json'); |
| 31 | + assert(proxy, 'Expected proxy configuration to be loaded'); |
| 32 | + const keys = Object.keys(proxy); |
| 33 | + expect(keys.length).toBe(1); |
| 34 | + const regex = new RegExp(keys[0]); |
| 35 | + |
| 36 | + return (url: string) => regex.test(url); |
| 37 | + } |
| 38 | + |
| 39 | + describe('glob to RegExp conversion with query strings', () => { |
| 40 | + it('handles trailing globstar (/**) with and without query strings', async () => { |
| 41 | + const matches = await createGlobMatcher('/search/**'); |
| 42 | + |
| 43 | + expect(matches('/search')).toBeTrue(); |
| 44 | + expect(matches('/search?q=angular')).toBeTrue(); |
| 45 | + expect(matches('/search/')).toBeTrue(); |
| 46 | + expect(matches('/search/?q=angular')).toBeTrue(); |
| 47 | + expect(matches('/search/items')).toBeTrue(); |
| 48 | + expect(matches('/search/items?q=angular')).toBeTrue(); |
| 49 | + expect(matches('/search/items?callback=/.hidden')).toBeTrue(); |
| 50 | + |
| 51 | + expect(matches('/searching')).toBeFalse(); |
| 52 | + expect(matches('/searching?q=angular')).toBeFalse(); |
| 53 | + expect(matches('/other?redirect=/search')).toBeFalse(); |
| 54 | + }); |
| 55 | + |
| 56 | + it('handles single-segment wildcard (*) without matching across slashes or query strings', async () => { |
| 57 | + const matches = await createGlobMatcher('/api/*'); |
| 58 | + |
| 59 | + expect(matches('/api/users')).toBeTrue(); |
| 60 | + expect(matches('/api/users?id=1')).toBeTrue(); |
| 61 | + expect(matches('/api/users?redirect=/home/page')).toBeTrue(); |
| 62 | + |
| 63 | + expect(matches('/api')).toBeFalse(); |
| 64 | + expect(matches('/api/')).toBeFalse(); |
| 65 | + expect(matches('/api/?a=1')).toBeFalse(); |
| 66 | + expect(matches('/api/users/profile')).toBeFalse(); |
| 67 | + expect(matches('/api/users/profile?a=1')).toBeFalse(); |
| 68 | + }); |
| 69 | + |
| 70 | + it('handles suffix globs (*.json) without matching query parameters', async () => { |
| 71 | + const matches = await createGlobMatcher('/api/*.json'); |
| 72 | + |
| 73 | + expect(matches('/api/data.json')).toBeTrue(); |
| 74 | + expect(matches('/api/data.json?v=1')).toBeTrue(); |
| 75 | + |
| 76 | + expect(matches('/api/users')).toBeFalse(); |
| 77 | + expect(matches('/api/users?file=data.json')).toBeFalse(); |
| 78 | + expect(matches('/api/.json?v=1')).toBeFalse(); |
| 79 | + }); |
| 80 | + |
| 81 | + it('handles middle globstar (/**/) without matching paths in query parameters', async () => { |
| 82 | + const matches = await createGlobMatcher('/api/**/users'); |
| 83 | + |
| 84 | + expect(matches('/api/users')).toBeTrue(); |
| 85 | + expect(matches('/api/users?page=1')).toBeTrue(); |
| 86 | + expect(matches('/api/v1/users?page=1')).toBeTrue(); |
| 87 | + expect(matches('/api/v1/v2/users?page=1')).toBeTrue(); |
| 88 | + |
| 89 | + expect(matches('/api/other?redirect=/users')).toBeFalse(); |
| 90 | + expect(matches('/api/users/profile?page=1')).toBeFalse(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('handles single-character wildcard (?) without matching the query delimiter', async () => { |
| 94 | + const matches = await createGlobMatcher('/api/v?'); |
| 95 | + |
| 96 | + expect(matches('/api/v1')).toBeTrue(); |
| 97 | + expect(matches('/api/v1?a=1')).toBeTrue(); |
| 98 | + |
| 99 | + expect(matches('/api/v')).toBeFalse(); |
| 100 | + expect(matches('/api/v?')).toBeFalse(); |
| 101 | + expect(matches('/api/v?a=1')).toBeFalse(); |
| 102 | + expect(matches('/api/v12?a=1')).toBeFalse(); |
| 103 | + }); |
| 104 | + |
| 105 | + it('handles negated globs (!) with and without query strings', async () => { |
| 106 | + const matches = await createGlobMatcher('!/api/public/**'); |
| 107 | + |
| 108 | + expect(matches('/api/public')).toBeFalse(); |
| 109 | + expect(matches('/api/public?foo=1')).toBeFalse(); |
| 110 | + expect(matches('/api/public/assets?foo=1')).toBeFalse(); |
| 111 | + |
| 112 | + expect(matches('/api/private')).toBeTrue(); |
| 113 | + expect(matches('/api/private?foo=1')).toBeTrue(); |
| 114 | + }); |
| 115 | + |
| 116 | + it('handles brace expansion and extglobs with query strings', async () => { |
| 117 | + const matchesBraces = await createGlobMatcher('/api/{auth,users}/**'); |
| 118 | + expect(matchesBraces('/api/auth?q=1')).toBeTrue(); |
| 119 | + expect(matchesBraces('/api/users/list?q=1')).toBeTrue(); |
| 120 | + expect(matchesBraces('/api/other?q=1')).toBeFalse(); |
| 121 | + |
| 122 | + const matchesExtglob = await createGlobMatcher('/**/!(*.d).ts'); |
| 123 | + expect(matchesExtglob('/app/main.ts')).toBeTrue(); |
| 124 | + expect(matchesExtglob('/app/main.ts?extra=.d.ts')).toBeTrue(); |
| 125 | + expect(matchesExtglob('/app/main.d.ts')).toBeFalse(); |
| 126 | + expect(matchesExtglob('/app/main.d.ts?extra=.ts')).toBeFalse(); |
| 127 | + }); |
| 128 | + }); |
| 129 | +}); |
0 commit comments