|
| 1 | +import { describe, expect, test } from 'vitest' |
| 2 | +import { |
| 3 | + composeRewrites, |
| 4 | + executeRewriteInput, |
| 5 | + executeRewriteOutput, |
| 6 | + rewriteBasepath, |
| 7 | +} from '../src/rewrite' |
| 8 | +import type { LocationRewrite } from '../src' |
| 9 | + |
| 10 | +describe('rewrite helpers', () => { |
| 11 | + test('basepath rewrite strips input and restores output', () => { |
| 12 | + const rewrite = rewriteBasepath({ basepath: '/app/' }) |
| 13 | + const inputUrl = new URL('https://example.com/app/posts') |
| 14 | + |
| 15 | + expect(executeRewriteInput(rewrite, inputUrl).pathname).toBe('/posts') |
| 16 | + |
| 17 | + const outputUrl = new URL('https://example.com/posts') |
| 18 | + expect(executeRewriteOutput(rewrite, outputUrl).pathname).toBe('/app/posts') |
| 19 | + }) |
| 20 | + |
| 21 | + test('basepath rewrite handles root output', () => { |
| 22 | + const rewrite = rewriteBasepath({ basepath: '/app' }) |
| 23 | + const url = new URL('https://example.com/') |
| 24 | + |
| 25 | + expect(executeRewriteOutput(rewrite, url).pathname).toBe('/app/') |
| 26 | + }) |
| 27 | + |
| 28 | + test('case-insensitive basepath input preserves original suffix casing', () => { |
| 29 | + const rewrite = rewriteBasepath({ basepath: '/App' }) |
| 30 | + const url = new URL('https://example.com/app/Users') |
| 31 | + |
| 32 | + expect(executeRewriteInput(rewrite, url).pathname).toBe('/Users') |
| 33 | + }) |
| 34 | + |
| 35 | + test('composeRewrites applies input forward and output backward', () => { |
| 36 | + const calls: Array<string> = [] |
| 37 | + const first: LocationRewrite = { |
| 38 | + input: ({ url }) => { |
| 39 | + calls.push('first-in') |
| 40 | + url.pathname += 'a' |
| 41 | + return url |
| 42 | + }, |
| 43 | + output: ({ url }) => { |
| 44 | + calls.push('first-out') |
| 45 | + url.pathname += 'd' |
| 46 | + return url |
| 47 | + }, |
| 48 | + } |
| 49 | + const second: LocationRewrite = { |
| 50 | + input: ({ url }) => { |
| 51 | + calls.push('second-in') |
| 52 | + url.pathname += 'b' |
| 53 | + return url |
| 54 | + }, |
| 55 | + output: ({ url }) => { |
| 56 | + calls.push('second-out') |
| 57 | + url.pathname += 'c' |
| 58 | + return url |
| 59 | + }, |
| 60 | + } |
| 61 | + |
| 62 | + const rewrite = composeRewrites([first, second]) |
| 63 | + |
| 64 | + expect( |
| 65 | + executeRewriteInput(rewrite, new URL('https://example.com/')).pathname, |
| 66 | + ).toBe('/ab') |
| 67 | + expect( |
| 68 | + executeRewriteOutput(rewrite, new URL('https://example.com/')).pathname, |
| 69 | + ).toBe('/cd') |
| 70 | + expect(calls).toEqual(['first-in', 'second-in', 'second-out', 'first-out']) |
| 71 | + }) |
| 72 | +}) |
0 commit comments