Open History to see the last commit
1import { describe, expect, test } from 'vitest'
2import { BaseRootRoute, BaseRoute } from '../src'
3import { createTestRouter, loadServerResponse } from './routerTestUtils'
4β
5const asciiControls = [...Array.from({ length: 32 }, (_, code) => code), 127]
6const staticPaths = ['/', '/admin', '/profile'] as const
7const parameterCases = [
8 '{{app_name}}',
9 '<test>',
10 '"quoted"',
11 "apostrophe's",
12 'back`tick',
13 'file name',
14 'a\\b',
15 'a%b',
16 'λνλ―Όκ΅',
17 'π',
18 ...asciiControls.map((code) => `a${String.fromCharCode(code)}b`),
19].map((value) => ({ value, path: `/params/${encodeURIComponent(value)}` }))
20const encodedControls = [
21 ...asciiControls.map(
22 (code) => `%${code.toString(16).padStart(2, '0').toUpperCase()}`,
23 ),
24 '%0D%0A',
25]
26β
27describe.each([false, true])('SSR URL encoding (rewrite: %s)', (rewrite) => {
28 function setupRouter() {
29 const loaderCalls: Array<string> = []
30 const rootRoute = new BaseRootRoute()
31 const staticRoutes = staticPaths.map(
32 (path) =>
33 new BaseRoute({
34 getParentRoute: () => rootRoute,
35 path,
36 loader: () => {
37 loaderCalls.push(path)
38 return path
39 },
40 }),
41 )
42 const paramRoute = new BaseRoute({
43 getParentRoute: () => rootRoute,
44 path: '/params/$value',
45 loader: ({ params }) => params.value,
46 })
47 const router = createTestRouter({
48 routeTree: rootRoute.addChildren([...staticRoutes, paramRoute]),
49 isServer: true,
50 rewrite: rewrite
51 ? { input: ({ url }) => url, output: ({ url }) => url }
52 : undefined,
53 })
54 return { router, paramRoute, loaderCalls }
55 }
56β
57 test.each(parameterCases)(
58 'serves and rebuilds $path without redirecting',
59 async ({ value, path }) => {
60 const { router, paramRoute } = setupRouter()
61β
62 const response = await loadServerResponse(router, path)
63β
64 expect(response.status).toBe(200)
65 expect(response.headers.get('Location')).toBeNull()
66 const match = router.state.matches.find(
67 (item) => item.routeId === paramRoute.id,
68 )
69 expect(match?.params).toEqual({ value })
70 expect(match?.loaderData).toBe(value)
71 expect(
72 router.buildLocation({ to: '/params/$value', params: { value } })
73 .publicHref,
74 ).toBe(path)
75 },
76 )
77β
78 test.each(staticPaths)('loads the ordinary route %s', async (path) => {
79 const { router, loaderCalls } = setupRouter()
80β
81 const response = await loadServerResponse(router, path)
82β
83 expect(response.status).toBe(200)
84 expect(response.headers.get('Location')).toBeNull()
85 expect(loaderCalls).toEqual([path])
86 })
87β
88 test.each(encodedControls)(
89 'preserves %s when resolving routes and canonicalizing trailing slashes',
90 async (encoded) => {
91 for (const variant of new Set([encoded, encoded.toLowerCase()])) {
92 for (const path of [
93 `/${variant}admin`,
94 `/assets/..${variant}/profile`,
95 `/assets/..${variant}/`,
96 ]) {
97 const normalizedPath = path.replace(variant, encoded)
98 const { router, loaderCalls } = setupRouter()
99β
100 const response = await loadServerResponse(router, path)
101β
102 expect(loaderCalls, path).toEqual([])
103 expect(router.state.location.pathname, path).toBe(normalizedPath)
104 if (path.endsWith('/')) {
105 const destination = normalizedPath.slice(0, -1)
106 expect(response.status, path).toBe(307)
107 expect(response.headers.get('Location'), path).toBe(destination)
108β
109 // Removing the trailing slash must preserve the encoded segment
110 // and end at a 404, rather than redirecting again or loading '/'.
111 const followUp = setupRouter()
112 const finalResponse = await loadServerResponse(
113 followUp.router,
114 destination,
115 )
116 expect(finalResponse.status, path).toBe(404)
117 expect(finalResponse.headers.get('Location'), path).toBeNull()
118 expect(followUp.loaderCalls, path).toEqual([])
119 expect(followUp.router.state.location.pathname, path).toBe(
120 destination,
121 )
122 } else {
123 expect(response.status, path).toBe(404)
124 expect(response.headers.get('Location'), path).toBeNull()
125 }
126 }
127 }
128 },
129 )
130})