|
| 1 | +import { describe, expect, test } from '@rstest/core'; |
| 2 | +import { pluginClientRedirects } from '../src'; |
| 3 | +import { getInlineRedirectScript } from '../src/inlineRedirect'; |
| 4 | +import type { RedirectsOptions } from '../src/types'; |
| 5 | + |
| 6 | +const runInlineRedirectScript = ( |
| 7 | + options: RedirectsOptions, |
| 8 | + { |
| 9 | + pathname, |
| 10 | + hash = '', |
| 11 | + }: { |
| 12 | + pathname: string; |
| 13 | + hash?: string; |
| 14 | + }, |
| 15 | +) => { |
| 16 | + let redirectedTo: string | undefined; |
| 17 | + const warnings: unknown[][] = []; |
| 18 | + const script = getInlineRedirectScript(options); |
| 19 | + |
| 20 | + Function( |
| 21 | + 'window', |
| 22 | + 'console', |
| 23 | + script, |
| 24 | + )( |
| 25 | + { |
| 26 | + location: { |
| 27 | + pathname, |
| 28 | + hash, |
| 29 | + replace: (url: string) => { |
| 30 | + redirectedTo = url; |
| 31 | + }, |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + warn: (...args: unknown[]) => { |
| 36 | + warnings.push(args); |
| 37 | + }, |
| 38 | + }, |
| 39 | + ); |
| 40 | + |
| 41 | + return { redirectedTo, script, warnings }; |
| 42 | +}; |
| 43 | + |
| 44 | +describe('getInlineRedirectScript', () => { |
| 45 | + test('redirects internal routes and preserves the hash', () => { |
| 46 | + expect( |
| 47 | + runInlineRedirectScript( |
| 48 | + { |
| 49 | + redirects: [ |
| 50 | + { |
| 51 | + from: ['/docs/2022', '/docs/2023'], |
| 52 | + to: '/docs/2024', |
| 53 | + }, |
| 54 | + ], |
| 55 | + }, |
| 56 | + { |
| 57 | + pathname: '/docs/2023/guide', |
| 58 | + hash: '#install', |
| 59 | + }, |
| 60 | + ).redirectedTo, |
| 61 | + ).toBe('/docs/2024/guide#install'); |
| 62 | + }); |
| 63 | + |
| 64 | + test('redirects to external URLs', () => { |
| 65 | + expect( |
| 66 | + runInlineRedirectScript( |
| 67 | + { |
| 68 | + redirects: [ |
| 69 | + { |
| 70 | + from: '/docs/old', |
| 71 | + to: 'https://example.com/new', |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + { pathname: '/docs/old' }, |
| 76 | + ).redirectedTo, |
| 77 | + ).toBe('https://example.com/new'); |
| 78 | + }); |
| 79 | + |
| 80 | + test('warns for invalid patterns and continues matching', () => { |
| 81 | + const result = runInlineRedirectScript( |
| 82 | + { |
| 83 | + redirects: [ |
| 84 | + { |
| 85 | + from: ['[', '/docs/old'], |
| 86 | + to: '/docs/new', |
| 87 | + }, |
| 88 | + ], |
| 89 | + }, |
| 90 | + { pathname: '/docs/old' }, |
| 91 | + ); |
| 92 | + |
| 93 | + expect(result.redirectedTo).toBe('/docs/new'); |
| 94 | + expect(result.warnings).toHaveLength(1); |
| 95 | + expect(result.warnings[0][0]).toBe('Invalid redirect pattern: ['); |
| 96 | + }); |
| 97 | + |
| 98 | + test('does not inject a script without redirect rules', () => { |
| 99 | + expect(getInlineRedirectScript()).toBe(''); |
| 100 | + expect(getInlineRedirectScript({ redirects: [] })).toBe(''); |
| 101 | + }); |
| 102 | + |
| 103 | + test('escapes data that could close the inline script element', () => { |
| 104 | + const script = getInlineRedirectScript({ |
| 105 | + redirects: [{ from: '</script>', to: '/docs/new' }], |
| 106 | + }); |
| 107 | + |
| 108 | + expect(script).not.toContain('</script>'); |
| 109 | + expect(script).toContain('\\u003c/script>'); |
| 110 | + }); |
| 111 | +}); |
| 112 | + |
| 113 | +describe('pluginClientRedirects', () => { |
| 114 | + test('only injects the redirect script in production', async () => { |
| 115 | + const plugin = pluginClientRedirects({ |
| 116 | + redirects: [{ from: '/docs/old', to: '/docs/new' }], |
| 117 | + }); |
| 118 | + const utils = { |
| 119 | + addPlugin: () => {}, |
| 120 | + removePlugin: () => {}, |
| 121 | + }; |
| 122 | + |
| 123 | + await plugin.config?.({}, utils, false); |
| 124 | + expect(plugin.builderConfig).toBeUndefined(); |
| 125 | + |
| 126 | + await plugin.config?.({}, utils, true); |
| 127 | + expect(plugin.builderConfig?.html?.tags).toHaveLength(1); |
| 128 | + |
| 129 | + await plugin.config?.({}, utils, false); |
| 130 | + expect(plugin.builderConfig).toBeUndefined(); |
| 131 | + }); |
| 132 | +}); |
0 commit comments