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