url.test.ts
This file
Path
src/utils/url.test.ts
384 lines · 14.9 KiB · TypeScript
Last changed by
Merge commit from forkTaku Amanobefore adoption9c28d72
History of this fileTake it
The walk stopped after 200 commits: lines older than f0b094d point at it.
Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 1–184why, one step back1import {2 checkOptionalParameter,3 getPath,4 getPathNoStrict,5 getPattern,6 getQueryParam,7 getQueryParams,8 getQueryStrings,9 mergePath,10 splitPath,11 splitRoutingPath,12} from './url'1314describe('url', () => {15 it('splitPath', () => {16 let ps = splitPath('/')17 expect(ps).toStrictEqual([''])1819 ps = splitPath('/hello')20 expect(ps).toStrictEqual(['hello'])21 })2223 it('splitRoutingPath', () => {24 let ps = splitRoutingPath('/')25 expect(ps).toStrictEqual([''])2627 ps = splitRoutingPath('/hello')28 expect(ps).toStrictEqual(['hello'])2930 ps = splitRoutingPath('*')31 expect(ps).toStrictEqual(['*'])3233 ps = splitRoutingPath('/wildcard-abc/*/wildcard-efg')34 expect(ps).toStrictEqual(['wildcard-abc', '*', 'wildcard-efg'])3536 ps = splitRoutingPath('/map/:location/events')37 expect(ps).toStrictEqual(['map', ':location', 'events'])3839 ps = splitRoutingPath('/js/:location{[a-z/]+.js}')40 expect(ps).toStrictEqual(['js', ':location{[a-z/]+.js}'])4142 ps = splitRoutingPath('/users/:name{[0-9a-zA-Z_-]{3,10}}')43 expect(ps).toStrictEqual(['users', ':name{[0-9a-zA-Z_-]{3,10}}'])4445 ps = splitRoutingPath('/users/:@name{[0-9a-zA-Z_-]{3,10}}')46 expect(ps).toStrictEqual(['users', ':@name{[0-9a-zA-Z_-]{3,10}}'])4748 ps = splitRoutingPath('/users/:dept{\\d+}/:@name{[0-9a-zA-Z_-]{3,10}}')49 expect(ps).toStrictEqual(['users', ':dept{\\d+}', ':@name{[0-9a-zA-Z_-]{3,10}}'])50 })5152 describe('getPattern', () => {53 it('no pattern', () => {54 const res = getPattern('id')55 expect(res).toBeNull()56 })5758 it('no pattern with next', () => {59 const res = getPattern('id', 'next')60 expect(res).toBeNull()61 })6263 it('default pattern', () => {64 const res = getPattern(':id')65 expect(res).toEqual([':id', 'id', true])66 })6768 it('default pattern with next', () => {69 const res = getPattern(':id', 'next')70 expect(res).toEqual([':id', 'id', true])71 })7273 it('regex pattern', () => {74 const res = getPattern(':id{[0-9]+}')75 expect(res).toEqual([':id{[0-9]+}', 'id', /^[0-9]+$/])76 })7778 it('regex pattern with next', () => {79 const res = getPattern(':id{[0-9]+}', 'next')80 expect(res).toEqual([':id{[0-9]+}#next', 'id', /^[0-9]+(?=\/next)/])81 })8283 it('wildcard', () => {84 const res = getPattern('*')85 expect(res).toBe('*')86 })8788 it('wildcard with next', () => {89 const res = getPattern('*', 'next')90 expect(res).toBe('*')91 })92 })9394 describe('getPath', () => {95 it('getPath - no trailing slash', () => {96 let path = getPath(new Request('https://example.com/'))97 expect(path).toBe('/')98 path = getPath(new Request('https://example.com/hello'))99 expect(path).toBe('/hello')100 path = getPath(new Request('https://example.com/hello/hey'))101 expect(path).toBe('/hello/hey')102 path = getPath(new Request('https://example.com/hello?name=foo'))103 expect(path).toBe('/hello')104 path = getPath(new Request('https://example.com/hello/hey?name=foo&name=bar'))105 expect(path).toBe('/hello/hey')106 })107108 it('getPath - with trailing slash', () => {109 let path = getPath(new Request('https://example.com/hello/'))110 expect(path).toBe('/hello/')111 path = getPath(new Request('https://example.com/hello/hey/'))112 expect(path).toBe('/hello/hey/')113 })114115 it('getPath - http+unix', () => {116 const path = getPath(new Request('http+unix://%2Ftmp%2Fsocket%2Esock/hello/'))117 expect(path).toBe('/hello/')118 })119120 it.each([121 'http:/example.com/hello', // invalid HTTP URL122 'http:///hello', // invalid HTTP URL123 'http://a/:/hello', // starts with `/:/`124 'x://a/:/hello', // unknown schema125 ])('getPath - %s', (url) => {126 expect(getPath(new Request(url))).toBe(new URL(url).pathname)127 })128129 it('getPath - with fragment', () => {130 let path = getPath(new Request('https://example.com/users/#user-list'))131 expect(path).toBe('/users/')132 path = getPath(new Request('https://example.com/users/1#profile-section'))133 expect(path).toBe('/users/1')134 path = getPath(new Request('https://example.com/hello#section'))135 expect(path).toBe('/hello')136 path = getPath(new Request('https://example.com/#top'))137 expect(path).toBe('/')138 })139140 it('getPath - with query and fragment', () => {141 let path = getPath(new Request('https://example.com/hello?name=foo#section'))142 expect(path).toBe('/hello')143 path = getPath(new Request('https://example.com/search?q=test#results'))144 expect(path).toBe('/search')145 })146147 it('getPath - with percent encoding only (no query or fragment)', () => {148 const path = getPath(new Request('https://example.com/hello%20world'))149 expect(path).toBe('/hello world')150 })151152 it('getPath - with percent encoding and fragment', () => {153 let path = getPath(new Request('https://example.com/hello%20world#section'))154 expect(path).toBe('/hello world')155 path = getPath(new Request('https://example.com/%E7%82%8E#top'))156 expect(path).toBe('/炎')157 })158159 it('getPath - with percent encoding and fragment containing query-like chars', () => {160 const path = getPath(new Request('https://example.com/hello%20world#section?foo=bar'))161 expect(path).toBe('/hello world')162 })163164 it('getPath - with encoded hash (%23) in path and real fragment', () => {165 // %23 is encoded '#' - decodeURI preserves reserved characters, so %23 stays as %23166 let path = getPath(new Request('https://example.com/path%23test#real-fragment'))167 expect(path).toBe('/path%23test')168 path = getPath(new Request('https://example.com/foo%23bar%23baz#section'))169 expect(path).toBe('/foo%23bar%23baz')170 // Only encoded hash, no real fragment171 path = getPath(new Request('https://example.com/issue%23123'))172 expect(path).toBe('/issue%23123')173 })174 })175176 describe('getQueryStrings', () => {177 it('getQueryStrings', () => {178 let qs = getQueryStrings('https://example.com/hello?name=foo&name=bar&age=20')179 expect(qs).toBe('?name=foo&name=bar&age=20')180 qs = getQueryStrings('https://example.com/hello?')181 expect(qs).toBe('?')182 qs = getQueryStrings('https://example.com/hello')183 expect(qs).toBe('')184 qs = getQueryStrings('https://example.com/hello?name=foo&name=bar&age=20#hash')Merge commit from forkTaku Amanobefore adoption9c28d72
lines 185–189why, one step back185 expect(qs).toBe('?name=foo&name=bar&age=20')186 qs = getQueryStrings('https://example.com/hello#?name=probe')187 expect(qs).toBe('')188 qs = getQueryStrings('https://example.com/hello?name=foo%23bar#hash')189 expect(qs).toBe('?name=foo%23bar')Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 190–258why, one step back190 })191 })192193 describe('getPathNoStrict', () => {194 it('getPathNoStrict - no strict is false', () => {195 let path = getPathNoStrict(new Request('https://example.com/hello/'))196 expect(path).toBe('/hello')197 path = getPathNoStrict(new Request('https://example.com/hello/hey/'))198 expect(path).toBe('/hello/hey')199 })200201 it('getPathNoStrict - return `/` even if strict is false', () => {202 const path = getPathNoStrict(new Request('https://example.com/'))203 expect(path).toBe('/')204 })205 })206207 describe('mergePath', () => {208 it('mergePath', () => {209 expect(mergePath('/book', '/')).toBe('/book')210 expect(mergePath('/book/', '/')).toBe('/book/')211 expect(mergePath('/book', '/hey')).toBe('/book/hey')212 expect(mergePath('/book/', '/hey')).toBe('/book/hey')213 expect(mergePath('/book', '/hey/')).toBe('/book/hey/')214 expect(mergePath('/book/', '/hey/')).toBe('/book/hey/')215 expect(mergePath('/book', 'hey', 'say')).toBe('/book/hey/say')216 expect(mergePath('/book', '/hey/', '/say/')).toBe('/book/hey/say/')217 expect(mergePath('/book', '/hey/', '/say/', '/')).toBe('/book/hey/say/')218 expect(mergePath('/book', '/hey', '/say', '/')).toBe('/book/hey/say')219 expect(mergePath('/', '/book', '/hey', '/say', '/')).toBe('/book/hey/say')220221 expect(mergePath('book', '/')).toBe('/book')222 expect(mergePath('book/', '/')).toBe('/book/')223 expect(mergePath('book', '/hey')).toBe('/book/hey')224 expect(mergePath('book', 'hey')).toBe('/book/hey')225 expect(mergePath('book', 'hey/')).toBe('/book/hey/')226 })227 it('Should be `/book`', () => {228 expect(mergePath('/', 'book')).toBe('/book')229 })230 it('Should be `/book`', () => {231 expect(mergePath('/', '/book')).toBe('/book')232 })233 it('Should be `/`', () => {234 expect(mergePath('/', '/')).toBe('/')235 })236 })237238 describe('checkOptionalParameter', () => {239 it('checkOptionalParameter', () => {240 expect(checkOptionalParameter('/api/animals/:type?')).toEqual([241 '/api/animals',242 '/api/animals/:type',243 ])244 expect(checkOptionalParameter('/api/animals/type?')).toBeNull()245 expect(checkOptionalParameter('/api/animals/:type')).toBeNull()246 expect(checkOptionalParameter('/api/animals')).toBeNull()247 expect(checkOptionalParameter('/api/:animals?/type')).toBeNull()248 expect(checkOptionalParameter('/api/animals/:type?/')).toBeNull()249 expect(checkOptionalParameter('/:optional?')).toEqual(['/', '/:optional'])250 expect(checkOptionalParameter('/v1/leaderboard/:version?/:platform?')).toEqual([251 '/v1/leaderboard',252 '/v1/leaderboard/:version',253 '/v1/leaderboard/:version/:platform',254 ])255 expect(checkOptionalParameter('/api/:version/animal/:type?')).toEqual([256 '/api/:version/animal',257 '/api/:version/animal/:type',258 ])fix(url): strip trailing question mark correctly for optional params with regex quantifiers (#5209)Nanasibefore adoptiond982f63
line 259why, one step back259 expect(checkOptionalParameter('/api/:id{[0-9]?}?')).toEqual(['/api', '/api/:id{[0-9]?}'])Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 260–265why, one step back260 })261 })262263 describe('getQueryParam', () => {264 it('Parse URL query strings', () => {265 expect(getQueryParam('http://example.com/?name=hey', 'name')).toBe('hey')Merge commit from forkTaku Amanobefore adoption9c28d72
lines 266–271why, one step back266 expect(getQueryParam('http://example.com/?name=hey#fragment', 'name')).toBe('hey')267 expect(getQueryParam('http://example.com/#?name=probe', 'name')).toBe(undefined)268 expect(getQueryParam('http://example.com/?name=hey%23there#fragment', 'name')).toBe(269 'hey#there'270 )271 expect(getQueryParam('http://example.com/?safe=1#&admin=true')).toEqual({ safe: '1' })Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 272–318why, one step back272 expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170', 'age')).toBe('20')273 expect(getQueryParam('http://example.com/?Hono+is=a+web+framework', 'Hono is')).toBe(274 'a web framework'275 )276 expect(getQueryParam('http://example.com/?name=%E0%A4%A', 'name')).toBe('%E0%A4%A')277278 expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name0')).toBe('sam')279 expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name1')).toBe('tom')280 expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name')).toBe(undefined)281282 let searchParams = new URLSearchParams({ name: '炎' })283 expect(getQueryParam(`http://example.com/?${searchParams.toString()}`, 'name')).toBe('炎')284 searchParams = new URLSearchParams({ '炎 is': 'a web framework' })285 expect(286 getQueryParam(287 `http://example.com/?${searchParams.toString()}`,288 searchParams.keys().next().value289 )290 ).toBe('a web framework')291 expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170', 'weight')).toBe(292 undefined293 )294 expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170')).toEqual({295 name: 'hey',296 age: '20',297 tall: '170',298 })299 expect(getQueryParam('http://example.com/?pretty&&&&q=1%2b1=2')).toEqual({300 pretty: '',301 q: '1+1=2',302 })303 expect(getQueryParam('http://example.com/?pretty', 'pretty')).toBe('')304 expect(getQueryParam('http://example.com/?pretty', 'prtt')).toBe(undefined)305 expect(getQueryParam('http://example.com/?name=sam&name=tom', 'name')).toBe('sam')306 expect(getQueryParam('http://example.com/&name=sam?name=tom', 'name')).toBe('tom')307 expect(getQueryParam('http://example.com/&name=sam', 'name')).toBe(undefined)308 expect(getQueryParam('http://example.com/?name=sam&name=tom')).toEqual({309 name: 'sam',310 })311 searchParams = new URLSearchParams('?name=sam=tom')312 expect(getQueryParam('name', searchParams.get('name')?.toString()))313 })314 })315316 describe('getQueryParams', () => {317 it('Parse URL query strings', () => {318 expect(getQueryParams('http://example.com/?name=hey', 'name')).toEqual(['hey'])Merge commit from forkTaku Amanobefore adoption9c28d72
lines 319–322why, one step back319 expect(getQueryParams('http://example.com/?name=hey#fragment', 'name')).toEqual(['hey'])320 expect(getQueryParams('http://example.com/#?name=probe', 'name')).toBe(undefined)321 expect(getQueryParams('http://example.com/?name=hey%23there#fragment', 'name')).toEqual([322 'hey#there',Merge commit from forkYusuke Wadabefore adoptionf0b094d
line 323why, one step back323 ])Merge commit from forkTaku Amanobefore adoption9c28d72
line 324why, one step back324 expect(getQueryParams('http://example.com/?safe=1#&admin=true')).toEqual({ safe: ['1'] })Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 325–373why, one step back325 expect(getQueryParams('http://example.com/?name=hey&name=foo', 'name')).toEqual([326 'hey',327 'foo',328 ])329 expect(getQueryParams('http://example.com/?name=hey&age=20&tall=170', 'age')).toEqual(['20'])330 expect(331 getQueryParams('http://example.com/?name=hey&age=20&tall=170&name=foo&age=30', 'age')332 ).toEqual(['20', '30'])333 expect(getQueryParams('http://example.com/?Hono+is=a+web+framework', 'Hono is')).toEqual([334 'a web framework',335 ])336 expect(getQueryParams('http://example.com/?name=%E0%A4%A', 'name')).toEqual(['%E0%A4%A'])337338 let searchParams = new URLSearchParams()339 searchParams.append('tag', '炎')340 searchParams.append('tag', 'ほのお')341 expect(getQueryParams(`http://example.com/?${searchParams.toString()}`, 'tag')).toEqual([342 '炎',343 'ほのお',344 ])345 searchParams = new URLSearchParams()346 searchParams.append('炎 works on', 'Cloudflare Workers')347 searchParams.append('炎 works on', 'Fastly Compute')348 expect(349 getQueryParams(350 `http://example.com/?${searchParams.toString()}`,351 searchParams.keys().next().value352 )353 ).toEqual(['Cloudflare Workers', 'Fastly Compute'])354 expect(getQueryParams('http://example.com/?name=hey&age=20&tall=170', 'weight')).toEqual(355 undefined356 )357 expect(358 getQueryParams('http://example.com/?name=hey&age=20&tall=170&name=foo&age=30&tall=180')359 ).toEqual({360 name: ['hey', 'foo'],361 age: ['20', '30'],362 tall: ['170', '180'],363 })364 expect(getQueryParams('http://example.com/?pretty&&&&q=1%2b1=2&q=2%2b2=4')).toEqual({365 pretty: [''],366 q: ['1+1=2', '2+2=4'],367 })368 expect(getQueryParams('http://example.com/?pretty', 'pretty')).toEqual([''])369 expect(getQueryParams('http://example.com/?pretty', 'prtt')).toBe(undefined)370 expect(getQueryParams('http://example.com/?toString')).toEqual({371 toString: [''],372 })373 })fix: use `Object.create(null)` when parsing query, headers, and params (#5161)Yusuke Wadabefore adoptionc85aead
lines 374–382why, one step back374375 it('should treat `__proto__` as a normal key without changing the prototype', () => {376 const params = getQueryParams('http://example.com/?__proto__=a&__proto__=b') as Record<377 string,378 string[]379 >380 expect(Object.getPrototypeOf(params)).toBeNull()381 expect(params['__proto__']).toEqual(['a', 'b'])382 })Merge commit from forkYusuke Wadabefore adoptionf0b094d
lines 383–384why, one step back383 })384})