Skip to content

Commit

Permalink
Ensures there is no unexpected slash in url before query params (#2470)
Browse files Browse the repository at this point in the history
Co-authored-by: Lenz Weber-Tronic <mail@lenzw.de>
  • Loading branch information
ygrishajev and phryneas authored Aug 19, 2022
1 parent 4822769 commit ceb3d50
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 26 deletions.
44 changes: 19 additions & 25 deletions packages/toolkit/src/query/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,31 +74,25 @@ describe('isDocumentVisible', () => {
})

describe('joinUrls', () => {
test('correctly joins variations of relative urls', () => {
expect(joinUrls('/api/', '/banana')).toBe('/api/banana')
expect(joinUrls('/api/', 'banana')).toBe('/api/banana')

expect(joinUrls('/api', 'banana')).toBe('/api/banana')
expect(joinUrls('/api', '/banana/')).toBe('/api/banana/')

expect(joinUrls('', '/banana')).toBe('/banana')
expect(joinUrls('', 'banana')).toBe('banana')
})

test('correctly joins variations of absolute urls', () => {
expect(joinUrls('https://example.com/api', 'banana')).toBe(
'https://example.com/api/banana'
)
expect(joinUrls('https://example.com/api', '/banana')).toBe(
'https://example.com/api/banana'
)

expect(joinUrls('https://example.com/api/', 'banana')).toBe(
'https://example.com/api/banana'
)
expect(joinUrls('https://example.com/api/', '/banana/')).toBe(
'https://example.com/api/banana/'
)
test.each([
['/api/', '/banana', '/api/banana'],
['/api/', 'banana', '/api/banana'],
['/api', '/banana', '/api/banana'],
['/api', 'banana', '/api/banana'],
['', '/banana', '/banana'],
['', 'banana', 'banana'],
['api', '?a=1', 'api?a=1'],
['api/', '?a=1', 'api/?a=1'],
['api', 'banana?a=1', 'api/banana?a=1'],
['api/', 'banana?a=1', 'api/banana?a=1'],
['https://example.com/api', 'banana', 'https://example.com/api/banana'],
['https://example.com/api', '/banana', 'https://example.com/api/banana'],
['https://example.com/api/', 'banana', 'https://example.com/api/banana'],
['https://example.com/api/', '/banana', 'https://example.com/api/banana'],
['https://example.com/api/', 'https://example.org', 'https://example.org'],
['https://example.com/api/', '//example.org', '//example.org'],
])('%s and %s join to %s', (base, url, expected) => {
expect(joinUrls(base, url)).toBe(expected)
})
})

Expand Down
3 changes: 2 additions & 1 deletion packages/toolkit/src/query/utils/joinUrls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ export function joinUrls(
return url
}

const delimiter = base.endsWith('/') || !url.startsWith('?') ? '/' : ''
base = withoutTrailingSlash(base)
url = withoutLeadingSlash(url)

return `${base}/${url}`
return `${base}${delimiter}${url}`;
}

0 comments on commit ceb3d50

Please sign in to comment.