Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

special-characters.spec.ts

280 lines7.9 KB

1import { expect } from '@playwright/test'
2import { test } from '@tanstack/router-e2e-utils'
3import { isSpaMode } from './utils/isSpaMode'
4​
5test.use({
6 whitelistErrors: [
7 /Failed to load resource: the server responded with a status of 404/,
8 ],
9})
10test.describe('Unicode route rendering', () => {
11 test.beforeEach(async ({ page }) => {
12 await page.goto('/specialChars')
13 })
14​
15 test('should render non-latin route correctly with direct navigation', async ({
16 page,
17 baseURL,
18 }) => {
19 await page.goto('/specialChars/λŒ€ν•œλ―Όκ΅­')
20 await page.waitForURL(
21 `${baseURL}/specialChars/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD`,
22 )
23​
24 await expect(page.getByTestId('special-non-latin-heading')).toBeInViewport()
25 })
26​
27 test('should render non-latin route correctly during router navigation', async ({
28 page,
29 baseURL,
30 }) => {
31 const nonLatinLink = page.getByTestId('special-non-latin-link')
32​
33 await nonLatinLink.click()
34 await page.waitForURL(
35 `${baseURL}/specialChars/%EB%8C%80%ED%95%9C%EB%AF%BC%EA%B5%AD`,
36 )
37​
38 await expect(page.getByTestId('special-non-latin-heading')).toBeInViewport()
39 })
40​
41 test.describe('Special characters in path params', () => {
42 test('should render route correctly on direct navigation', async ({
43 page,
44 baseURL,
45 }) => {
46 await page.goto('/specialChars/λŒ€|')
47 await page.waitForURL(`${baseURL}/specialChars/%EB%8C%80%7C`)
48​
49 const param = await page.getByTestId('special-param').textContent()
50​
51 expect(param).toBe('λŒ€|')
52 })
53​
54 test('should render route correctly on router navigation', async ({
55 page,
56 baseURL,
57 }) => {
58 const link = page.getByTestId('special-param-link')
59​
60 await link.click()
61 await page.waitForURL(`${baseURL}/specialChars/%EB%8C%80%7C`)
62​
63 const param = await page.getByTestId('special-param').textContent()
64​
65 expect(param).toBe('λŒ€|')
66 })
67​
68 test('should not cause redirect loop for curly braces in path params', async ({
69 page,
70 baseURL,
71 }) => {
72 // %7B/%7D (curly braces) must not trigger infinite 307 redirects
73 const res = await page.goto('/specialChars/%7B%7Bapp_name%7D%7D')
74 await page.waitForLoadState('load')
75​
76 // Should not redirect β€” URL stays encoded
77 expect(page.url()).toBe(`${baseURL}/specialChars/%7B%7Bapp_name%7D%7D`)
78 expect(res!.status()).toBe(200)
79​
80 const param = await page.getByTestId('special-param').textContent()
81 expect(param).toBe('{{app_name}}')
82 })
83 })
84​
85 test.describe('Special characters in search params', () => {
86 test('should render route correctly on direct navigation', async ({
87 page,
88 baseURL,
89 }) => {
90 await page.goto('/specialChars/search?searchParam=λŒ€|')
91​
92 await page.waitForURL(
93 `${baseURL}/specialChars/search?searchParam=%EB%8C%80|`,
94 )
95​
96 await page.waitForLoadState('networkidle')
97​
98 const searchParam = await page
99 .getByTestId('special-search-param')
100 .textContent()
101​
102 expect(searchParam).toBe('λŒ€|')
103​
104 const loadedOn = await page
105 .getByTestId('special-search-loaded-info')
106 .textContent()
107 const beforeLoadOn = await page
108 .getByTestId('special-search-before-load-info')
109 .textContent()
110​
111 if (isSpaMode) {
112 expect(loadedOn).toBe('Loaded on: client')
113 expect(beforeLoadOn).toBe('Before load on: client')
114 } else {
115 expect(loadedOn).toBe('Loaded on: server')
116 expect(beforeLoadOn).toBe('Before load on: server')
117 }
118 })
119​
120 test('should render route correctly on router navigation', async ({
121 page,
122 baseURL,
123 }) => {
124 const link = page.getByTestId('special-searchParam-link')
125​
126 await link.click()
127 await page.waitForURL(
128 `${baseURL}/specialChars/search?searchParam=%EB%8C%80%7C`,
129 )
130​
131 const searchParam = await page
132 .getByTestId('special-search-param')
133 .textContent()
134​
135 expect(searchParam).toBe('λŒ€|')
136 })
137 })
138​
139 test.describe('Special characters in url hash', () => {
140 test('should render route correctly on direct navigation', async ({
141 page,
142 baseURL,
143 }) => {
144 await expect(page.getByTestId('special-hash-link-1')).not.toContainClass(
145 'font-bold',
146 )
147 await expect(page.getByTestId('special-hash-link-2')).not.toContainClass(
148 'font-bold',
149 )
150​
151 await page.goto('/specialChars/hash#λŒ€|')
152​
153 await page.waitForURL(`${baseURL}/specialChars/hash#%EB%8C%80|`)
154 await page.waitForLoadState('load')
155​
156 await expect(page.getByTestId('special-hash-heading')).toBeInViewport()
157​
158 await expect(page.getByTestId('special-hash-link-1')).toContainClass(
159 'font-bold',
160 )
161​
162 await expect(page.getByTestId('special-hash-link-2')).not.toContainClass(
163 'font-bold',
164 )
165​
166 await page.getByTestId('toggle-hash-button').click()
167​
168 const hashValue = await page.getByTestId('special-hash').textContent()
169​
170 expect(hashValue).toBe('λŒ€|')
171 })
172​
173 test('should render route correctly on router navigation', async ({
174 page,
175 baseURL,
176 }) => {
177 await expect(page.getByTestId('special-hash-link-1')).not.toContainClass(
178 'font-bold',
179 )
180​
181 await expect(page.getByTestId('special-hash-link-2')).not.toContainClass(
182 'font-bold',
183 )
184​
185 const link = page.getByTestId('special-hash-link-1')
186​
187 await link.click()
188​
189 await page.waitForURL(`${baseURL}/specialChars/hash#%EB%8C%80|`)
190 await page.waitForLoadState('load')
191​
192 await expect(page.getByTestId('special-hash-heading')).toBeInViewport()
193​
194 await expect(page.getByTestId('special-hash-link-1')).toContainClass(
195 'font-bold',
196 )
197​
198 await expect(page.getByTestId('special-hash-link-2')).not.toContainClass(
199 'font-bold',
200 )
201​
202 await page.getByTestId('toggle-hash-button').click()
203​
204 const hashValue = await page.getByTestId('special-hash').textContent()
205​
206 expect(hashValue).toBe('λŒ€|')
207 })
208 })
209​
210 test.describe('malformed paths', () => {
211 const malformedPathnames = [
212 '/specialChars/malformed/%E0%A4',
213 '/specialChars/malformed/%80',
214 '/specialChars/malformed/%FF',
215 ]
216​
217 test.use({
218 whitelistErrors: [
219 'Failed to load resource: the server responded with a status of 404',
220 'Failed to load resource: the server responded with a status of 400 (Bad Request)',
221 ],
222 })
223​
224 for (const pathname of malformedPathnames) {
225 test(`un-matched malformed path "${pathname}" should return bad request on direct navigation`, async ({
226 page,
227 }) => {
228 const res = await page.goto(pathname)
229​
230 await page.waitForLoadState(`load`)
231​
232 // in spa mode this is caught and handled at server level
233 if (!isSpaMode) {
234 expect(res!.status()).toBe(400)
235 } else {
236 expect(res!.status()).toBe(400)
237 }
238 })
239 }
240​
241 test('malformed path params should return not found on router link', async ({
242 page,
243 baseURL,
244 }) => {
245 await page.goto('/specialChars/malformed')
246 await page.waitForURL(`${baseURL}/specialChars/malformed`)
247​
248 const link = page.getByTestId('special-malformed-path-link')
249​
250 await link.click()
251​
252 await page.waitForLoadState('load')
253​
254 await expect(
255 page.getByTestId('default-not-found-component'),
256 ).toBeInViewport()
257 })
258​
259 test('un-matched malformed paths should return not found on direct navigation in search params', async ({
260 page,
261 baseURL,
262 }) => {
263 await page.goto('/specialChars/malformed/search?searchParam=%E0%A4')
264​
265 await page.waitForURL(
266 `${baseURL}/specialChars/malformed/search?searchParam=%E0%A4`,
267 )
268​
269 await expect(
270 page.getByTestId('special-malformed-search-param'),
271 ).toBeInViewport()
272​
273 const searchParam = await page
274 .getByTestId('special-malformed-search-param')
275 .textContent()
276​
277 expect(searchParam).toBe('οΏ½')
278 })
279 })
280})