|
| 1 | +const xForwardedHostEnvKeys = [ |
| 2 | + 'MULTI_TENANT', |
| 3 | + 'IS_MULTITENANT', |
| 4 | + 'REQUEST_X_FORWARDED_HOST_REGEXP', |
| 5 | + 'X_FORWARDED_HOST_REGEXP', |
| 6 | +] as const |
| 7 | + |
| 8 | +const originalEnv = Object.fromEntries( |
| 9 | + xForwardedHostEnvKeys.map((key) => [key, process.env[key]]) |
| 10 | +) as Record<(typeof xForwardedHostEnvKeys)[number], string | undefined> |
| 11 | + |
| 12 | +async function loadXForwardedHostRegExp({ |
| 13 | + isMultitenant, |
| 14 | + pattern, |
| 15 | +}: { |
| 16 | + isMultitenant: boolean |
| 17 | + pattern?: string |
| 18 | +}) { |
| 19 | + vi.resetModules() |
| 20 | + |
| 21 | + process.env.MULTI_TENANT = isMultitenant ? 'true' : 'false' |
| 22 | + process.env.IS_MULTITENANT = isMultitenant ? 'true' : 'false' |
| 23 | + process.env.X_FORWARDED_HOST_REGEXP = '' |
| 24 | + |
| 25 | + if (pattern === undefined) { |
| 26 | + process.env.REQUEST_X_FORWARDED_HOST_REGEXP = '' |
| 27 | + } else { |
| 28 | + process.env.REQUEST_X_FORWARDED_HOST_REGEXP = pattern |
| 29 | + } |
| 30 | + |
| 31 | + return await import('./x-forwarded-host') |
| 32 | +} |
| 33 | + |
| 34 | +function restoreXForwardedHostEnv() { |
| 35 | + for (const key of xForwardedHostEnvKeys) { |
| 36 | + const value = originalEnv[key] |
| 37 | + if (value === undefined) { |
| 38 | + delete process.env[key] |
| 39 | + } else { |
| 40 | + process.env[key] = value |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +afterEach(() => { |
| 46 | + restoreXForwardedHostEnv() |
| 47 | + vi.resetModules() |
| 48 | +}) |
| 49 | + |
| 50 | +describe('getXForwardedHostRegExp', () => { |
| 51 | + it('skips compiling the host pattern when multitenancy is disabled', async () => { |
| 52 | + const { getXForwardedHostRegExp } = await loadXForwardedHostRegExp({ |
| 53 | + isMultitenant: false, |
| 54 | + pattern: '[', |
| 55 | + }) |
| 56 | + |
| 57 | + expect(getXForwardedHostRegExp()).toBeUndefined() |
| 58 | + }) |
| 59 | + |
| 60 | + it('returns undefined when no host pattern is configured', async () => { |
| 61 | + const { getXForwardedHostRegExp } = await loadXForwardedHostRegExp({ |
| 62 | + isMultitenant: true, |
| 63 | + }) |
| 64 | + |
| 65 | + expect(getXForwardedHostRegExp()).toBeUndefined() |
| 66 | + }) |
| 67 | + |
| 68 | + it('reuses the compiled regexp from startup config', async () => { |
| 69 | + const { getXForwardedHostRegExp } = await loadXForwardedHostRegExp({ |
| 70 | + isMultitenant: true, |
| 71 | + pattern: '^([a-z]+)\\.local$', |
| 72 | + }) |
| 73 | + |
| 74 | + const first = getXForwardedHostRegExp() |
| 75 | + const second = getXForwardedHostRegExp() |
| 76 | + |
| 77 | + expect(second).toBe(first) |
| 78 | + expect('tenant.local'.match(first!)).toBeTruthy() |
| 79 | + }) |
| 80 | + |
| 81 | + it('does not recompile when config is reloaded after module load', async () => { |
| 82 | + const { getXForwardedHostRegExp } = await loadXForwardedHostRegExp({ |
| 83 | + isMultitenant: true, |
| 84 | + pattern: '^([a-z]+)\\.local$', |
| 85 | + }) |
| 86 | + const previous = getXForwardedHostRegExp() |
| 87 | + |
| 88 | + process.env.REQUEST_X_FORWARDED_HOST_REGEXP = '^([0-9]+)\\.local$' |
| 89 | + const { getConfig } = await import('../../config') |
| 90 | + getConfig({ reload: true }) |
| 91 | + |
| 92 | + const current = getXForwardedHostRegExp() |
| 93 | + |
| 94 | + expect(current).toBe(previous) |
| 95 | + expect('tenant.local'.match(current!)).toBeTruthy() |
| 96 | + expect('123.local'.match(current!)).toBeFalsy() |
| 97 | + }) |
| 98 | + |
| 99 | + it('throws while loading the helper when the configured pattern is invalid', async () => { |
| 100 | + await expect( |
| 101 | + loadXForwardedHostRegExp({ |
| 102 | + isMultitenant: true, |
| 103 | + pattern: '[', |
| 104 | + }) |
| 105 | + ).rejects.toThrow(SyntaxError) |
| 106 | + }) |
| 107 | +}) |
0 commit comments