|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { normalizeProxiedRscFetch } from '../pages/_layout' |
| 3 | +import { normalizeRscFetchUrl } from './rsc-route-normalization' |
| 4 | + |
| 5 | +const currentHref = 'https://docs.tempo.xyz/docs/guide/payments/send-a-payment' |
| 6 | +const origin = 'https://docs.tempo.xyz' |
| 7 | + |
| 8 | +describe('normalizeRscFetchUrl', () => { |
| 9 | + it.each([ |
| 10 | + [ |
| 11 | + 'keeps cross-origin RSC requests on the current origin', |
| 12 | + 'https://tempo.xyz/RSC/R/docs/guide/payments.txt?query=', |
| 13 | + 'https://docs.tempo.xyz/RSC/R/docs/guide/payments.txt?query=', |
| 14 | + ], |
| 15 | + [ |
| 16 | + 'normalizes proxied developers route payloads', |
| 17 | + 'https://tempo.xyz/RSC/R/developers/docs/tools.txt?query=', |
| 18 | + 'https://docs.tempo.xyz/RSC/R/docs/tools.txt?query=', |
| 19 | + ], |
| 20 | + [ |
| 21 | + 'normalizes the proxied developers root payload', |
| 22 | + 'https://tempo.xyz/RSC/R/developers.txt?query=', |
| 23 | + 'https://docs.tempo.xyz/RSC/R/_root.txt?query=', |
| 24 | + ], |
| 25 | + [ |
| 26 | + 'preserves search and hash fragments', |
| 27 | + 'https://tempo.xyz/RSC/R/docs/tools.txt?query=abc#flight', |
| 28 | + 'https://docs.tempo.xyz/RSC/R/docs/tools.txt?query=abc#flight', |
| 29 | + ], |
| 30 | + [ |
| 31 | + 'leaves non-RSC asset requests alone', |
| 32 | + 'https://tempo.xyz/assets/index.js', |
| 33 | + 'https://tempo.xyz/assets/index.js', |
| 34 | + ], |
| 35 | + ['leaves relative non-RSC requests alone', '/api/og?title=Tools', '/api/og?title=Tools'], |
| 36 | + ])('%s', (_name, input, expected) => { |
| 37 | + expect(normalizeRscFetchUrl(input, currentHref, origin)).toBe(expected) |
| 38 | + }) |
| 39 | +}) |
| 40 | + |
| 41 | +describe('normalizeProxiedRscFetch', () => { |
| 42 | + it.each([ |
| 43 | + [ |
| 44 | + 'rewrites cross-origin RSC requests to the current origin', |
| 45 | + 'https://tempo.xyz/RSC/R/docs/tools.txt?query=', |
| 46 | + 'https://docs.tempo.xyz/RSC/R/docs/tools.txt?query=', |
| 47 | + ], |
| 48 | + [ |
| 49 | + 'rewrites proxied developers RSC requests', |
| 50 | + 'https://tempo.xyz/RSC/R/developers/docs/tools.txt?query=', |
| 51 | + 'https://docs.tempo.xyz/RSC/R/docs/tools.txt?query=', |
| 52 | + ], |
| 53 | + [ |
| 54 | + 'leaves non-RSC requests unchanged', |
| 55 | + 'https://tempo.xyz/assets/index.js', |
| 56 | + 'https://tempo.xyz/assets/index.js', |
| 57 | + ], |
| 58 | + ])('%s', async (_name, input, expected) => { |
| 59 | + const requests: unknown[] = [] |
| 60 | + const fetch = (request: unknown) => { |
| 61 | + requests.push(request) |
| 62 | + return Promise.resolve(new Response()) |
| 63 | + } |
| 64 | + Object.defineProperty(globalThis, 'window', { |
| 65 | + configurable: true, |
| 66 | + value: { |
| 67 | + __tempoNormalizeProxiedRscFetch: false, |
| 68 | + fetch, |
| 69 | + location: { |
| 70 | + href: currentHref, |
| 71 | + origin, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }) |
| 75 | + |
| 76 | + try { |
| 77 | + Function(normalizeProxiedRscFetch)() |
| 78 | + await globalThis.window.fetch(input) |
| 79 | + expect(requests).toEqual([expected]) |
| 80 | + } finally { |
| 81 | + Reflect.deleteProperty(globalThis, 'window') |
| 82 | + } |
| 83 | + }) |
| 84 | +}) |
0 commit comments