-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathurl-encoding.ts
More file actions
72 lines (58 loc) · 3 KB
/
url-encoding.ts
File metadata and controls
72 lines (58 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { describe, expect, test } from 'vitest'
import { get } from '@/tests/helpers/e2etest'
describe('URL encoding for version paths', () => {
test('handles URL-encoded @ symbol in enterprise-cloud version', async () => {
// SharePoint encodes @ as %40, so /en/enterprise-cloud@latest becomes /en/enterprise-cloud%40latest
const encodedUrl = '/en/enterprise-cloud%40latest/copilot/concepts/chat'
const res = await get(encodedUrl)
// Should either:
// 1. Work directly (200) - the encoded URL should decode and work
// 2. Redirect (301/302) to the proper decoded URL
// Should NOT return 404
expect([200, 301, 302]).toContain(res.statusCode)
if (res.statusCode === 301 || res.statusCode === 302) {
// If it redirects, it should redirect to the decoded version
expect(res.headers.location).toBe('/en/enterprise-cloud@latest/copilot/concepts/chat')
}
})
test('handles URL-encoded @ symbol in enterprise-server version', async () => {
const encodedUrl =
'/en/enterprise-server%403.17/admin/managing-github-actions-for-your-enterprise'
const res = await get(encodedUrl)
expect([200, 301, 302]).toContain(res.statusCode)
if (res.statusCode === 301 || res.statusCode === 302) {
expect(res.headers.location).toBe(
'/en/enterprise-server@3.17/admin/managing-github-actions-for-your-enterprise',
)
}
})
test('handles URL-encoded @ symbol in second path segment', async () => {
// When no language prefix is present
const encodedUrl = '/enterprise-cloud%40latest/copilot/concepts/chat'
const res = await get(encodedUrl)
// Should redirect to add language prefix and decode
expect([301, 302]).toContain(res.statusCode)
expect(res.headers.location).toBe('/en/enterprise-cloud@latest/copilot/concepts/chat')
})
test('normal @ symbol paths continue to work', async () => {
// Ensure we don't break existing functionality
const normalUrl = '/en/enterprise-cloud@latest/copilot/concepts/chat'
const res = await get(normalUrl)
expect(res.statusCode).toBe(200)
})
test('URL encoding in other parts of URL is preserved', async () => {
// Only @ symbols in version paths should be decoded, other encoding should be preserved
const encodedUrl = '/en/enterprise-cloud@latest/copilot/concepts/some%20page'
const res = await get(encodedUrl)
// This might 404 if the page doesn't exist, but shouldn't break due to encoding
expect(res.statusCode).not.toBe(500)
})
test('Express URL properties are correctly updated after decoding', async () => {
// Test that req.path, req.query, etc. are properly updated when req.url is modified
const encodedUrl = '/en/enterprise-cloud%40latest/copilot/concepts/chat?test=value'
const res = await get(encodedUrl)
// Should work correctly (200 or redirect) - the middleware should properly update
// req.path from '/en/enterprise-cloud%40latest/...' to '/en/enterprise-cloud@latest/...'
expect([200, 301, 302]).toContain(res.statusCode)
})
})