Skip to content

Commit

Permalink
fix: Handle undefined pathname in generateWebLink() method
Browse files Browse the repository at this point in the history
The URL javascript object does not support undefined `pathname`. This
member should be an empty string (transformed to `/`) or have a value

```js
const url = new URL('https://cozy.io')
console.log(url.pathname) // prints '/'
console.log(url.toString()) // prints 'https://cozy.io/'

url.pathname = undefined
console.log(url.pathname) // prints '/undefined'
console.log(url.toString()) // prints 'https://cozy.io/undefined'

url.pathname = ''
console.log(url.pathname) // prints '/'
console.log(url.toString()) // prints 'https://cozy.io/'

url.pathname = ensureFirstSlash(undefined)
console.log(url.pathname) // prints '/'
console.log(url.toString()) // prints 'https://cozy.io/'
```

By using 'ensureFirstSlash' method we ensure that the caller can pass
undefined `pathname` without negative side effect
  • Loading branch information
Ldoppea committed Mar 31, 2023
1 parent fd111e1 commit c633f43
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/cozy-client/src/helpers/urlHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const generateWebLink = ({
.split('.')
.map((x, i) => (i === 0 ? x + '-' + slug : x))
.join('.')
url.pathname = pathname
url.pathname = ensureFirstSlash(pathname)
url.hash = ensureFirstSlash(hash)

for (const [param, value] of searchParams) {
Expand Down
12 changes: 12 additions & 0 deletions packages/cozy-client/src/helpers/urlHelper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ describe('generateWebLink', () => {
})
).toEqual(`https://drive.alice.cozy.tools/public/#/files/432432`)
})

it('should handle undefined pathname', () => {
expect(
generateWebLink({
cozyUrl: 'https://alice.cozy.tools',
pathname: undefined,
hash: 'files/432432',
slug: 'drive',
subDomainType: 'nested'
})
).toEqual(`https://drive.alice.cozy.tools/#/files/432432`)
})
})

describe('deconstructWebLink', () => {
Expand Down

0 comments on commit c633f43

Please sign in to comment.