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

getNormalizedURL.test.ts

151 lines4.7 KB

1import { describe, expect, test } from 'vitest'
2import { getNormalizedURL } from '../src/ssr/ssr-server'
3​
4describe('getNormalizedURL', () => {
5 test.each(['/%2Fdocs', '/%5Cdocs', '/%00/docs'])(
6 'preserves encoded pathname data in a URL object: %s',
7 (pathname) => {
8 const input = new URL(pathname, 'https://app.example')
9 const { url, handledProtocolRelativeURL } = getNormalizedURL(input)
10 expect(url.href).toBe(input.href)
11 expect(handledProtocolRelativeURL).toBe(false)
12 },
13 )
14​
15 test.each(['//other.example/path', '///other.example/path'])(
16 'keeps a request pathname %j on its original origin',
17 (pathname) => {
18 const { url, handledProtocolRelativeURL } = getNormalizedURL(
19 `https://example.com${pathname}?next=//other.example#//section`,
20 )
21 expect(url.href).toBe(
22 'https://example.com/other.example/path?next=%2F%2Fother.example#//section',
23 )
24 expect(handledProtocolRelativeURL).toBe(true)
25 },
26 )
27​
28 test('should return URL that is in standardized format', () => {
29 const url1 = 'https://example.com/%EB%8C%80%7C/path?query=%EB%8C%80|#hash'
30 const url2 = 'https://example.com/%EB%8C%80|/path?query=%EB%8C%80%7C#hash'
31​
32 const normalizedUrl1 = getNormalizedURL(url1)
33 const normalizedUrl2 = getNormalizedURL(url2)
34​
35 expect(normalizedUrl1.url.pathname).toBe('/%EB%8C%80|/path')
36 expect(normalizedUrl1.url.pathname).toBe(normalizedUrl2.url.pathname)
37 expect(new URL(url1).pathname).not.toBe(new URL(url2).pathname)
38​
39 expect(normalizedUrl1.url.search).toBe(`?query=%EB%8C%80%7C`)
40 expect(normalizedUrl1.url.search).toBe(normalizedUrl2.url.search)
41 expect(new URL(url1).search).not.toBe(new URL(url2).search)
42 })
43​
44 const testCases = [
45 {
46 url: 'https://example.com/%3Fstart?query=value',
47 expectedPathName: '/%3Fstart',
48 expectedSearchParams: '?query=value',
49 expectedHash: '',
50 },
51 {
52 url: 'https://example.com/end%3F?query=value',
53 expectedPathName: '/end%3F',
54 expectedSearchParams: '?query=value',
55 expectedHash: '',
56 },
57 {
58 url: 'https://example.com/%23?query=value',
59 expectedPathName: '/%23',
60 expectedSearchParams: '?query=value',
61 expectedHash: '',
62 },
63 {
64 url: 'https://example.com/a%3Fb%3Fc%23d?query=value',
65 expectedPathName: '/a%3Fb%3Fc%23d',
66 expectedSearchParams: '?query=value',
67 expectedHash: '',
68 },
69 {
70 url: 'https://example.com/path?query=value#section%3Fpart',
71 expectedPathName: '/path',
72 expectedSearchParams: '?query=value',
73 expectedHash: '#section%3Fpart',
74 },
75 {
76 url: 'https://example.com/start%3Fmiddle%23end?key=value%23part&other=%3Fdata#section%3Fpart',
77 expectedPathName: '/start%3Fmiddle%23end',
78 expectedSearchParams: '?key=value%23part&other=%3Fdata',
79 expectedHash: '#section%3Fpart',
80 },
81 {
82 url: 'https://example.com/%E0%A4',
83 expectedPathName: '/%E0%A4',
84 expectedSearchParams: '',
85 expectedHash: '',
86 },
87 {
88 url: 'https://example.com/%ZZ',
89 expectedPathName: '/%ZZ',
90 expectedSearchParams: '',
91 expectedHash: '',
92 },
93 {
94 url: 'https://example.com/path?a=1&a=2',
95 expectedPathName: '/path',
96 expectedSearchParams: '?a=1&a=2',
97 expectedHash: '',
98 },
99 {
100 url: 'https://example.com/path+a',
101 expectedPathName: '/path+a',
102 expectedSearchParams: '',
103 expectedHash: '',
104 },
105 {
106 url: 'https://example.com/path a',
107 expectedPathName: '/path%20a',
108 expectedSearchParams: '',
109 expectedHash: '',
110 },
111 {
112 url: 'https://example.com/path%20a',
113 expectedPathName: '/path%20a',
114 expectedSearchParams: '',
115 expectedHash: '',
116 },
117 {
118 url: 'https://example.com/path%25a',
119 expectedPathName: '/path%25a',
120 expectedSearchParams: '',
121 expectedHash: '',
122 },
123 {
124 url: 'https://example.com/path%25a',
125 expectedPathName: '/path%25a',
126 expectedSearchParams: '',
127 expectedHash: '',
128 },
129 {
130 url: 'https://example.com/path\\a',
131 expectedPathName: '/path%5Ca',
132 expectedSearchParams: '',
133 expectedHash: '',
134 },
135 {
136 url: 'https://example.com/path%5Ca',
137 expectedPathName: '/path%5Ca',
138 expectedSearchParams: '',
139 expectedHash: '',
140 },
141 ]
142 test.each(testCases)(
143 'should treat encoded URL specific characters correctly',
144 ({ url, expectedPathName, expectedHash, expectedSearchParams }) => {
145 const normalizedUrl = getNormalizedURL(url)
146 expect(normalizedUrl.url.pathname).toBe(expectedPathName)
147 expect(normalizedUrl.url.search).toBe(expectedSearchParams)
148 expect(normalizedUrl.url.hash).toBe(expectedHash)
149 },
150 )
151})