Skip to content
Myr

url.test.ts

This file

Path

src/utils/url.test.ts

363 lines · 13.7 KiB · TypeScript

Last changed by

fix(url): ignore fragment identifiers in getPath() (#4627)Sano Sugurubefore adoption0c1d4c7

History of this file

Take it

The walk stopped after 200 commits: lines older than 82bdc4d point at it.

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 1–118why, one step back
1import {
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 })

Merge commit from forkYusuke Wadabefore adoption1d79aed

lines 119–127why, one step back
119
120 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 })

fix(url): ignore fragment identifiers in getPath() (#4627)Sano Sugurubefore adoption0c1d4c7

lines 128–173why, one step back
128
129 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 })

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 174–296why, one step back
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 // Allows to contain hash185 qs = getQueryStrings('https://example.com/hello?name=foo&name=bar&age=20#hash')186 expect(qs).toBe('?name=foo&name=bar&age=20#hash')187 })188 })189190 describe('getPathNoStrict', () => {191 it('getPathNoStrict - no strict is false', () => {192 let path = getPathNoStrict(new Request('https://example.com/hello/'))193 expect(path).toBe('/hello')194 path = getPathNoStrict(new Request('https://example.com/hello/hey/'))195 expect(path).toBe('/hello/hey')196 })197198 it('getPathNoStrict - return `/` even if strict is false', () => {199 const path = getPathNoStrict(new Request('https://example.com/'))200 expect(path).toBe('/')201 })202 })203204 describe('mergePath', () => {205 it('mergePath', () => {206 expect(mergePath('/book', '/')).toBe('/book')207 expect(mergePath('/book/', '/')).toBe('/book/')208 expect(mergePath('/book', '/hey')).toBe('/book/hey')209 expect(mergePath('/book/', '/hey')).toBe('/book/hey')210 expect(mergePath('/book', '/hey/')).toBe('/book/hey/')211 expect(mergePath('/book/', '/hey/')).toBe('/book/hey/')212 expect(mergePath('/book', 'hey', 'say')).toBe('/book/hey/say')213 expect(mergePath('/book', '/hey/', '/say/')).toBe('/book/hey/say/')214 expect(mergePath('/book', '/hey/', '/say/', '/')).toBe('/book/hey/say/')215 expect(mergePath('/book', '/hey', '/say', '/')).toBe('/book/hey/say')216 expect(mergePath('/', '/book', '/hey', '/say', '/')).toBe('/book/hey/say')217218 expect(mergePath('book', '/')).toBe('/book')219 expect(mergePath('book/', '/')).toBe('/book/')220 expect(mergePath('book', '/hey')).toBe('/book/hey')221 expect(mergePath('book', 'hey')).toBe('/book/hey')222 expect(mergePath('book', 'hey/')).toBe('/book/hey/')223 })224 it('Should be `/book`', () => {225 expect(mergePath('/', 'book')).toBe('/book')226 })227 it('Should be `/book`', () => {228 expect(mergePath('/', '/book')).toBe('/book')229 })230 it('Should be `/`', () => {231 expect(mergePath('/', '/')).toBe('/')232 })233 })234235 describe('checkOptionalParameter', () => {236 it('checkOptionalParameter', () => {237 expect(checkOptionalParameter('/api/animals/:type?')).toEqual([238 '/api/animals',239 '/api/animals/:type',240 ])241 expect(checkOptionalParameter('/api/animals/type?')).toBeNull()242 expect(checkOptionalParameter('/api/animals/:type')).toBeNull()243 expect(checkOptionalParameter('/api/animals')).toBeNull()244 expect(checkOptionalParameter('/api/:animals?/type')).toBeNull()245 expect(checkOptionalParameter('/api/animals/:type?/')).toBeNull()246 expect(checkOptionalParameter('/:optional?')).toEqual(['/', '/:optional'])247 expect(checkOptionalParameter('/v1/leaderboard/:version?/:platform?')).toEqual([248 '/v1/leaderboard',249 '/v1/leaderboard/:version',250 '/v1/leaderboard/:version/:platform',251 ])252 expect(checkOptionalParameter('/api/:version/animal/:type?')).toEqual([253 '/api/:version/animal',254 '/api/:version/animal/:type',255 ])256 })257 })258259 describe('getQueryParam', () => {260 it('Parse URL query strings', () => {261 expect(getQueryParam('http://example.com/?name=hey', 'name')).toBe('hey')262 expect(getQueryParam('http://example.com/?name=hey#fragment', 'name')).toBe('hey#fragment')263 expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170', 'age')).toBe('20')264 expect(getQueryParam('http://example.com/?Hono+is=a+web+framework', 'Hono is')).toBe(265 'a web framework'266 )267 expect(getQueryParam('http://example.com/?name=%E0%A4%A', 'name')).toBe('%E0%A4%A')268269 expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name0')).toBe('sam')270 expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name1')).toBe('tom')271 expect(getQueryParam('http://example.com/?name0=sam&name1=tom', 'name')).toBe(undefined)272273 let searchParams = new URLSearchParams({ name: '炎' })274 expect(getQueryParam(`http://example.com/?${searchParams.toString()}`, 'name')).toBe('炎')275 searchParams = new URLSearchParams({ '炎 is': 'a web framework' })276 expect(277 getQueryParam(278 `http://example.com/?${searchParams.toString()}`,279 searchParams.keys().next().value280 )281 ).toBe('a web framework')282 expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170', 'weight')).toBe(283 undefined284 )285 expect(getQueryParam('http://example.com/?name=hey&age=20&tall=170')).toEqual({286 name: 'hey',287 age: '20',288 tall: '170',289 })290 expect(getQueryParam('http://example.com/?pretty&&&&q=1%2b1=2')).toEqual({291 pretty: '',292 q: '1+1=2',293 })294 expect(getQueryParam('http://example.com/?pretty', 'pretty')).toBe('')295 expect(getQueryParam('http://example.com/?pretty', 'prtt')).toBe(undefined)296 expect(getQueryParam('http://example.com/?name=sam&name=tom', 'name')).toBe('sam')

fix(utils/url): make _getQueryParam search behind question mark (#4507)tuzi3040before adoptionfa8eef7

lines 297–298why, one step back
297 expect(getQueryParam('http://example.com/&name=sam?name=tom', 'name')).toBe('tom')
298 expect(getQueryParam('http://example.com/&name=sam', 'name')).toBe(undefined)

4.9.0Yusuke Wadabefore adoption82bdc4d

lines 299–363why, one step back
299 expect(getQueryParam('http://example.com/?name=sam&name=tom')).toEqual({
300 name: 'sam',301 })302 searchParams = new URLSearchParams('?name=sam=tom')303 expect(getQueryParam('name', searchParams.get('name')?.toString()))304 })305 })306307 describe('getQueryParams', () => {308 it('Parse URL query strings', () => {309 expect(getQueryParams('http://example.com/?name=hey', 'name')).toEqual(['hey'])310 expect(getQueryParams('http://example.com/?name=hey#fragment', 'name')).toEqual([311 'hey#fragment',312 ])313 expect(getQueryParams('http://example.com/?name=hey&name=foo', 'name')).toEqual([314 'hey',315 'foo',316 ])317 expect(getQueryParams('http://example.com/?name=hey&age=20&tall=170', 'age')).toEqual(['20'])318 expect(319 getQueryParams('http://example.com/?name=hey&age=20&tall=170&name=foo&age=30', 'age')320 ).toEqual(['20', '30'])321 expect(getQueryParams('http://example.com/?Hono+is=a+web+framework', 'Hono is')).toEqual([322 'a web framework',323 ])324 expect(getQueryParams('http://example.com/?name=%E0%A4%A', 'name')).toEqual(['%E0%A4%A'])325326 let searchParams = new URLSearchParams()327 searchParams.append('tag', '炎')328 searchParams.append('tag', 'ほのお')329 expect(getQueryParams(`http://example.com/?${searchParams.toString()}`, 'tag')).toEqual([330 '炎',331 'ほのお',332 ])333 searchParams = new URLSearchParams()334 searchParams.append('炎 works on', 'Cloudflare Workers')335 searchParams.append('炎 works on', 'Fastly Compute')336 expect(337 getQueryParams(338 `http://example.com/?${searchParams.toString()}`,339 searchParams.keys().next().value340 )341 ).toEqual(['Cloudflare Workers', 'Fastly Compute'])342 expect(getQueryParams('http://example.com/?name=hey&age=20&tall=170', 'weight')).toEqual(343 undefined344 )345 expect(346 getQueryParams('http://example.com/?name=hey&age=20&tall=170&name=foo&age=30&tall=180')347 ).toEqual({348 name: ['hey', 'foo'],349 age: ['20', '30'],350 tall: ['170', '180'],351 })352 expect(getQueryParams('http://example.com/?pretty&&&&q=1%2b1=2&q=2%2b2=4')).toEqual({353 pretty: [''],354 q: ['1+1=2', '2+2=4'],355 })356 expect(getQueryParams('http://example.com/?pretty', 'pretty')).toEqual([''])357 expect(getQueryParams('http://example.com/?pretty', 'prtt')).toBe(undefined)358 expect(getQueryParams('http://example.com/?toString')).toEqual({359 toString: [''],360 })361 })362 })363})