|
1 | 1 | /** |
2 | | - * Copyright IBM Corp. 2025 |
| 2 | + * Copyright IBM Corp. 2025, 2026 |
3 | 3 | * |
4 | 4 | * This source code is licensed under the Apache-2.0 license found in the |
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import { test, expect, describe } from 'vitest'; |
9 | | -import { routes, routesInHeader, routesInSideNav } from '../routes/config'; |
| 9 | +import { |
| 10 | + routes, |
| 11 | + routesInHeader, |
| 12 | + routesInSideNav, |
| 13 | + isDirectChildPath, |
| 14 | +} from '../routes/config'; |
10 | 15 | import Dashboard from '../pages/dashboard/Dashboard'; |
11 | 16 | import NotFound from '../pages/not-found/NotFound'; |
12 | 17 | import Placeholder from '../pages/placeholder/Placeholder'; |
@@ -113,3 +118,192 @@ describe('routes configuration', () => { |
113 | 118 | }); |
114 | 119 | }); |
115 | 120 | }); |
| 121 | + |
| 122 | +describe('isDirectChildPath', () => { |
| 123 | + test('returns true for direct child paths', () => { |
| 124 | + expect(isDirectChildPath('/link-4', '/link-4/sub-link-1')).toBe(true); |
| 125 | + expect(isDirectChildPath('/dashboard', '/dashboard/123')).toBe(true); |
| 126 | + expect(isDirectChildPath('/api', '/api/users')).toBe(true); |
| 127 | + expect(isDirectChildPath('/a', '/a/b')).toBe(true); |
| 128 | + }); |
| 129 | + |
| 130 | + test('returns false for nested child paths (grandchildren)', () => { |
| 131 | + expect(isDirectChildPath('/link-4', '/link-4/sub-link-1/nested')).toBe( |
| 132 | + false, |
| 133 | + ); |
| 134 | + expect(isDirectChildPath('/dashboard', '/dashboard/123/details')).toBe( |
| 135 | + false, |
| 136 | + ); |
| 137 | + expect(isDirectChildPath('/api', '/api/users/profile')).toBe(false); |
| 138 | + expect(isDirectChildPath('/a', '/a/b/c')).toBe(false); |
| 139 | + }); |
| 140 | + |
| 141 | + test('returns false when paths are identical', () => { |
| 142 | + expect(isDirectChildPath('/dashboard', '/dashboard')).toBe(false); |
| 143 | + expect(isDirectChildPath('/link-4', '/link-4')).toBe(false); |
| 144 | + expect(isDirectChildPath('/', '/')).toBe(false); |
| 145 | + }); |
| 146 | + |
| 147 | + test('returns false for non-matching paths', () => { |
| 148 | + expect(isDirectChildPath('/link-4', '/link-5')).toBe(false); |
| 149 | + expect(isDirectChildPath('/dashboard', '/profile')).toBe(false); |
| 150 | + expect(isDirectChildPath('/api', '/app')).toBe(false); |
| 151 | + }); |
| 152 | + |
| 153 | + test('returns false for paths that start similarly but are not children', () => { |
| 154 | + expect(isDirectChildPath('/link', '/link-4')).toBe(false); |
| 155 | + expect(isDirectChildPath('/dash', '/dashboard')).toBe(false); |
| 156 | + expect(isDirectChildPath('/api', '/api-v2')).toBe(false); |
| 157 | + }); |
| 158 | + |
| 159 | + test('handles edge cases with null, undefined, and empty strings', () => { |
| 160 | + expect(isDirectChildPath(null, '/path')).toBe(false); |
| 161 | + expect(isDirectChildPath('/path', null)).toBe(false); |
| 162 | + expect(isDirectChildPath(undefined, '/path')).toBe(false); |
| 163 | + expect(isDirectChildPath('/path', undefined)).toBe(false); |
| 164 | + expect(isDirectChildPath('', '/path')).toBe(false); |
| 165 | + expect(isDirectChildPath('/path', '')).toBe(false); |
| 166 | + expect(isDirectChildPath('', '')).toBe(false); |
| 167 | + }); |
| 168 | + |
| 169 | + test('handles paths with special characters', () => { |
| 170 | + expect(isDirectChildPath('/user-profile', '/user-profile/settings')).toBe( |
| 171 | + true, |
| 172 | + ); |
| 173 | + expect(isDirectChildPath('/api_v2', '/api_v2/endpoint')).toBe(true); |
| 174 | + expect(isDirectChildPath('/path.name', '/path.name/child')).toBe(true); |
| 175 | + }); |
| 176 | + |
| 177 | + test('handles trailing slashes correctly', () => { |
| 178 | + // Parent with trailing slash should not match (different path structure) |
| 179 | + expect(isDirectChildPath('/link-4/', '/link-4/sub-link-1')).toBe(false); |
| 180 | + // Child with trailing slash contains '/' in remainder, so not a direct child |
| 181 | + expect(isDirectChildPath('/link-4', '/link-4/sub-link-1/')).toBe(false); |
| 182 | + // Without trailing slashes works correctly |
| 183 | + expect(isDirectChildPath('/link-4', '/link-4/sub-link-1')).toBe(true); |
| 184 | + }); |
| 185 | + |
| 186 | + test('is case-sensitive', () => { |
| 187 | + expect(isDirectChildPath('/Link-4', '/link-4/sub-link-1')).toBe(false); |
| 188 | + expect(isDirectChildPath('/link-4', '/Link-4/sub-link-1')).toBe(false); |
| 189 | + }); |
| 190 | + |
| 191 | + test('works with root path', () => { |
| 192 | + // Root path '/' + '/' = '//', so '/dashboard' doesn't start with '//' |
| 193 | + // This is expected behavior - root path needs special handling if needed |
| 194 | + expect(isDirectChildPath('/', '/dashboard')).toBe(false); |
| 195 | + |
| 196 | + // For actual use case, routes don't use '/' as parent in the config |
| 197 | + // But if they did, this would be the correct behavior |
| 198 | + expect(isDirectChildPath('/', '//dashboard')).toBe(true); |
| 199 | + expect(isDirectChildPath('/', '//dashboard/123')).toBe(false); |
| 200 | + }); |
| 201 | + |
| 202 | + test('handles URL parameters correctly', () => { |
| 203 | + // Dynamic route parameters (common in React Router) |
| 204 | + expect(isDirectChildPath('/dashboard', '/dashboard/:id')).toBe(true); |
| 205 | + expect(isDirectChildPath('/users', '/users/:userId')).toBe(true); |
| 206 | + expect(isDirectChildPath('/api', '/api/:version')).toBe(true); |
| 207 | + |
| 208 | + // Multiple parameters |
| 209 | + expect(isDirectChildPath('/users/:userId', '/users/:userId/posts')).toBe( |
| 210 | + true, |
| 211 | + ); |
| 212 | + expect( |
| 213 | + isDirectChildPath('/users/:userId/posts', '/users/:userId/posts/:postId'), |
| 214 | + ).toBe(true); |
| 215 | + |
| 216 | + // Should not match nested parameters |
| 217 | + expect(isDirectChildPath('/users', '/users/:userId/posts/:postId')).toBe( |
| 218 | + false, |
| 219 | + ); |
| 220 | + }); |
| 221 | + |
| 222 | + test('handles query strings and hash fragments', () => { |
| 223 | + // Query strings should be treated as part of the path |
| 224 | + expect(isDirectChildPath('/search', '/search/results?q=test')).toBe(true); |
| 225 | + expect(isDirectChildPath('/api', '/api/data?format=json')).toBe(true); |
| 226 | + |
| 227 | + // Hash fragments |
| 228 | + expect(isDirectChildPath('/docs', '/docs/intro#section')).toBe(true); |
| 229 | + |
| 230 | + // Combined |
| 231 | + expect(isDirectChildPath('/page', '/page/view?id=1#top')).toBe(true); |
| 232 | + |
| 233 | + // Should not match if there's a nested path before query/hash |
| 234 | + expect(isDirectChildPath('/api', '/api/v1/data?format=json')).toBe(false); |
| 235 | + }); |
| 236 | + |
| 237 | + test('handles special URL characters', () => { |
| 238 | + // Encoded characters |
| 239 | + expect(isDirectChildPath('/search', '/search/hello%20world')).toBe(true); |
| 240 | + expect(isDirectChildPath('/files', '/files/document.pdf')).toBe(true); |
| 241 | + |
| 242 | + // Special characters that are valid in URLs |
| 243 | + expect(isDirectChildPath('/items', '/items/item-123')).toBe(true); |
| 244 | + expect(isDirectChildPath('/tags', '/tags/tag_name')).toBe(true); |
| 245 | + expect(isDirectChildPath('/docs', '/docs/v1.0.0')).toBe(true); |
| 246 | + expect(isDirectChildPath('/api', '/api/~user')).toBe(true); |
| 247 | + |
| 248 | + // Parentheses (sometimes used in routing) |
| 249 | + expect(isDirectChildPath('/routes', '/routes/(auth)')).toBe(true); |
| 250 | + |
| 251 | + // Plus sign |
| 252 | + expect(isDirectChildPath('/search', '/search/C++')).toBe(true); |
| 253 | + }); |
| 254 | + |
| 255 | + test('handles wildcard and catch-all patterns', () => { |
| 256 | + // Asterisk (catch-all routes) |
| 257 | + expect(isDirectChildPath('/files', '/files/*')).toBe(true); |
| 258 | + expect(isDirectChildPath('/docs', '/docs/*')).toBe(true); |
| 259 | + |
| 260 | + // Should not match nested wildcards |
| 261 | + expect(isDirectChildPath('/files', '/files/subfolder/*')).toBe(false); |
| 262 | + }); |
| 263 | + |
| 264 | + test('handles optional segments notation', () => { |
| 265 | + // Optional segments (React Router syntax) |
| 266 | + expect(isDirectChildPath('/users', '/users/:id?')).toBe(true); |
| 267 | + expect(isDirectChildPath('/posts', '/posts/:slug?')).toBe(true); |
| 268 | + |
| 269 | + // Splat parameters |
| 270 | + expect(isDirectChildPath('/files', '/files/*filepath')).toBe(true); |
| 271 | + }); |
| 272 | + |
| 273 | + test('handles real-world routing scenarios', () => { |
| 274 | + // API versioning |
| 275 | + expect(isDirectChildPath('/api', '/api/v1')).toBe(true); |
| 276 | + expect(isDirectChildPath('/api', '/api/v1/users')).toBe(false); |
| 277 | + |
| 278 | + // Locale routing |
| 279 | + expect(isDirectChildPath('/en', '/en/dashboard')).toBe(true); |
| 280 | + expect(isDirectChildPath('/en', '/en/dashboard/settings')).toBe(false); |
| 281 | + |
| 282 | + // Admin routes |
| 283 | + expect(isDirectChildPath('/admin', '/admin/users')).toBe(true); |
| 284 | + expect(isDirectChildPath('/admin', '/admin/users/edit')).toBe(false); |
| 285 | + |
| 286 | + // Resource routes (RESTful) |
| 287 | + expect(isDirectChildPath('/posts', '/posts/new')).toBe(true); |
| 288 | + expect(isDirectChildPath('/posts', '/posts/:id')).toBe(true); |
| 289 | + expect(isDirectChildPath('/posts/:id', '/posts/:id/edit')).toBe(true); |
| 290 | + expect(isDirectChildPath('/posts/:id', '/posts/:id/comments')).toBe(true); |
| 291 | + expect( |
| 292 | + isDirectChildPath('/posts/:id', '/posts/:id/comments/:commentId'), |
| 293 | + ).toBe(false); |
| 294 | + }); |
| 295 | + |
| 296 | + test('prevents ReDoS by avoiding regex', () => { |
| 297 | + // These patterns would be problematic with regex but are safe with string methods |
| 298 | + const maliciousPath = '/a'.repeat(100); |
| 299 | + const maliciousSubPath = maliciousPath + '/b'; |
| 300 | + |
| 301 | + // Should complete quickly without hanging |
| 302 | + const startTime = Date.now(); |
| 303 | + const result = isDirectChildPath(maliciousPath, maliciousSubPath); |
| 304 | + const endTime = Date.now(); |
| 305 | + |
| 306 | + expect(result).toBe(true); |
| 307 | + expect(endTime - startTime).toBeLessThan(10); // Should be nearly instant |
| 308 | + }); |
| 309 | +}); |
0 commit comments