diff options
Diffstat (limited to 'spec/frontend/lib/utils/url_utility_spec.js')
-rw-r--r-- | spec/frontend/lib/utils/url_utility_spec.js | 147 |
1 files changed, 124 insertions, 23 deletions
diff --git a/spec/frontend/lib/utils/url_utility_spec.js b/spec/frontend/lib/utils/url_utility_spec.js index 4960895890f..c494033badd 100644 --- a/spec/frontend/lib/utils/url_utility_spec.js +++ b/spec/frontend/lib/utils/url_utility_spec.js @@ -91,36 +91,75 @@ describe('URL utility', () => { }); describe('mergeUrlParams', () => { + const { mergeUrlParams } = urlUtils; + it('adds w', () => { - expect(urlUtils.mergeUrlParams({ w: 1 }, '#frag')).toBe('?w=1#frag'); - expect(urlUtils.mergeUrlParams({ w: 1 }, '/path#frag')).toBe('/path?w=1#frag'); - expect(urlUtils.mergeUrlParams({ w: 1 }, 'https://host/path')).toBe('https://host/path?w=1'); - expect(urlUtils.mergeUrlParams({ w: 1 }, 'https://host/path#frag')).toBe( - 'https://host/path?w=1#frag', - ); + expect(mergeUrlParams({ w: 1 }, '#frag')).toBe('?w=1#frag'); + expect(mergeUrlParams({ w: 1 }, '')).toBe('?w=1'); + expect(mergeUrlParams({ w: 1 }, '/path#frag')).toBe('/path?w=1#frag'); + expect(mergeUrlParams({ w: 1 }, 'https://host/path')).toBe('https://host/path?w=1'); + expect(mergeUrlParams({ w: 1 }, 'https://host/path#frag')).toBe('https://host/path?w=1#frag'); + expect(mergeUrlParams({ w: 1 }, 'https://h/p?k1=v1#frag')).toBe('https://h/p?k1=v1&w=1#frag'); + expect(mergeUrlParams({ w: 'null' }, '')).toBe('?w=null'); + }); - expect(urlUtils.mergeUrlParams({ w: 1 }, 'https://h/p?k1=v1#frag')).toBe( - 'https://h/p?k1=v1&w=1#frag', - ); + it('adds multiple params', () => { + expect(mergeUrlParams({ a: 1, b: 2, c: 3 }, '#frag')).toBe('?a=1&b=2&c=3#frag'); }); it('updates w', () => { - expect(urlUtils.mergeUrlParams({ w: 1 }, '?k1=v1&w=0#frag')).toBe('?k1=v1&w=1#frag'); + expect(mergeUrlParams({ w: 2 }, '/path?w=1#frag')).toBe('/path?w=2#frag'); + expect(mergeUrlParams({ w: 2 }, 'https://host/path?w=1')).toBe('https://host/path?w=2'); }); - it('adds multiple params', () => { - expect(urlUtils.mergeUrlParams({ a: 1, b: 2, c: 3 }, '#frag')).toBe('?a=1&b=2&c=3#frag'); + it('removes null w', () => { + expect(mergeUrlParams({ w: null }, '?w=1#frag')).toBe('#frag'); + expect(mergeUrlParams({ w: null }, '/path?w=1#frag')).toBe('/path#frag'); + expect(mergeUrlParams({ w: null }, 'https://host/path?w=1')).toBe('https://host/path'); + expect(mergeUrlParams({ w: null }, 'https://host/path?w=1#frag')).toBe( + 'https://host/path#frag', + ); + expect(mergeUrlParams({ w: null }, 'https://h/p?k1=v1&w=1#frag')).toBe( + 'https://h/p?k1=v1#frag', + ); }); - it('adds and updates encoded params', () => { - expect(urlUtils.mergeUrlParams({ a: '&', q: '?' }, '?a=%23#frag')).toBe('?a=%26&q=%3F#frag'); + it('adds and updates encoded param values', () => { + expect(mergeUrlParams({ foo: '&', q: '?' }, '?foo=%23#frag')).toBe('?foo=%26&q=%3F#frag'); + expect(mergeUrlParams({ foo: 'a value' }, '')).toBe('?foo=a%20value'); + expect(mergeUrlParams({ foo: 'a value' }, '?foo=1')).toBe('?foo=a%20value'); + }); + + it('adds and updates encoded param names', () => { + expect(mergeUrlParams({ 'a name': 1 }, '')).toBe('?a%20name=1'); + expect(mergeUrlParams({ 'a name': 2 }, '?a%20name=1')).toBe('?a%20name=2'); + expect(mergeUrlParams({ 'a name': null }, '?a%20name=1')).toBe(''); }); it('treats "+" as "%20"', () => { - expect(urlUtils.mergeUrlParams({ ref: 'bogus' }, '?a=lorem+ipsum&ref=charlie')).toBe( + expect(mergeUrlParams({ ref: 'bogus' }, '?a=lorem+ipsum&ref=charlie')).toBe( '?a=lorem%20ipsum&ref=bogus', ); }); + + it('treats question marks and slashes as part of the query', () => { + expect(mergeUrlParams({ ending: '!' }, '?ending=?&foo=bar')).toBe('?ending=!&foo=bar'); + expect(mergeUrlParams({ ending: '!' }, 'https://host/path?ending=?&foo=bar')).toBe( + 'https://host/path?ending=!&foo=bar', + ); + expect(mergeUrlParams({ ending: '?' }, '?ending=!&foo=bar')).toBe('?ending=%3F&foo=bar'); + expect(mergeUrlParams({ ending: '?' }, 'https://host/path?ending=!&foo=bar')).toBe( + 'https://host/path?ending=%3F&foo=bar', + ); + expect(mergeUrlParams({ ending: '!', op: '+' }, '?ending=?&op=/')).toBe('?ending=!&op=%2B'); + expect(mergeUrlParams({ ending: '!', op: '+' }, 'https://host/path?ending=?&op=/')).toBe( + 'https://host/path?ending=!&op=%2B', + ); + expect(mergeUrlParams({ op: '+' }, '?op=/&foo=bar')).toBe('?op=%2B&foo=bar'); + expect(mergeUrlParams({ op: '+' }, 'https://host/path?op=/&foo=bar')).toBe( + 'https://host/path?op=%2B&foo=bar', + ); + }); }); describe('removeParams', () => { @@ -284,20 +323,76 @@ describe('URL utility', () => { }); }); - describe('isAbsoluteOrRootRelative', () => { - const validUrls = ['https://gitlab.com/', 'http://gitlab.com/', '/users/sign_in']; - - const invalidUrls = [' https://gitlab.com/', './file/path', 'notanurl', '<a></a>']; + describe('isAbsolute', () => { + it.each` + url | valid + ${'https://gitlab.com/'} | ${true} + ${'http://gitlab.com/'} | ${true} + ${'/users/sign_in'} | ${false} + ${' https://gitlab.com'} | ${false} + ${'somepath.php?url=https://gitlab.com'} | ${false} + ${'notaurl'} | ${false} + ${'../relative_url'} | ${false} + ${'<a></a>'} | ${false} + `('returns $valid for $url', ({ url, valid }) => { + expect(urlUtils.isAbsolute(url)).toBe(valid); + }); + }); - it.each(validUrls)(`returns true for %s`, url => { - expect(urlUtils.isAbsoluteOrRootRelative(url)).toBe(true); + describe('isRootRelative', () => { + it.each` + url | valid + ${'https://gitlab.com/'} | ${false} + ${'http://gitlab.com/'} | ${false} + ${'/users/sign_in'} | ${true} + ${' https://gitlab.com'} | ${false} + ${'/somepath.php?url=https://gitlab.com'} | ${true} + ${'notaurl'} | ${false} + ${'../relative_url'} | ${false} + ${'<a></a>'} | ${false} + `('returns $valid for $url', ({ url, valid }) => { + expect(urlUtils.isRootRelative(url)).toBe(valid); }); + }); - it.each(invalidUrls)(`returns false for %s`, url => { - expect(urlUtils.isAbsoluteOrRootRelative(url)).toBe(false); + describe('isAbsoluteOrRootRelative', () => { + it.each` + url | valid + ${'https://gitlab.com/'} | ${true} + ${'http://gitlab.com/'} | ${true} + ${'/users/sign_in'} | ${true} + ${' https://gitlab.com'} | ${false} + ${'/somepath.php?url=https://gitlab.com'} | ${true} + ${'notaurl'} | ${false} + ${'../relative_url'} | ${false} + ${'<a></a>'} | ${false} + `('returns $valid for $url', ({ url, valid }) => { + expect(urlUtils.isAbsoluteOrRootRelative(url)).toBe(valid); }); }); + describe('relativePathToAbsolute', () => { + it.each` + path | base | result + ${'./foo'} | ${'bar/'} | ${'/bar/foo'} + ${'../john.md'} | ${'bar/baz/foo.php'} | ${'/bar/john.md'} + ${'../images/img.png'} | ${'bar/baz/foo.php'} | ${'/bar/images/img.png'} + ${'../images/Image 1.png'} | ${'bar/baz/foo.php'} | ${'/bar/images/Image 1.png'} + ${'/images/img.png'} | ${'bar/baz/foo.php'} | ${'/images/img.png'} + ${'/images/img.png'} | ${'/bar/baz/foo.php'} | ${'/images/img.png'} + ${'../john.md'} | ${'/bar/baz/foo.php'} | ${'/bar/john.md'} + ${'../john.md'} | ${'///bar/baz/foo.php'} | ${'/bar/john.md'} + ${'/images/img.png'} | ${'https://gitlab.com/user/project/'} | ${'https://gitlab.com/images/img.png'} + ${'../images/img.png'} | ${'https://gitlab.com/user/project/'} | ${'https://gitlab.com/user/images/img.png'} + ${'../images/Image 1.png'} | ${'https://gitlab.com/user/project/'} | ${'https://gitlab.com/user/images/Image%201.png'} + `( + 'converts relative path "$path" with base "$base" to absolute path => "expected"', + ({ path, base, result }) => { + expect(urlUtils.relativePathToAbsolute(path, base)).toBe(result); + }, + ); + }); + describe('isSafeUrl', () => { const absoluteUrls = [ 'http://example.org', @@ -386,6 +481,12 @@ describe('URL utility', () => { expect(urlUtils.queryToObject(searchQuery)).toEqual({ one: '1', two: '2' }); }); + + it('removes undefined values from the search query', () => { + const searchQuery = '?one=1&two=2&three'; + + expect(urlUtils.queryToObject(searchQuery)).toEqual({ one: '1', two: '2' }); + }); }); describe('objectToQuery', () => { |