Skip to content

Commit

Permalink
fix(server): fix url join (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregberge authored Nov 23, 2018
1 parent 6b5ba21 commit ba90289
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
6 changes: 4 additions & 2 deletions packages/server/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export const smartRequire = modulePath => {
return require(modulePath)
}

export const joinURLPath = (...paths) =>
paths.join('/').replace(/(?<!http(s)?:)(?<=.)\/\//g, '/')
export const joinURLPath = (...paths) => {
const cleanPaths = paths.map(path => path.replace(/\/$/, ''))
return cleanPaths.join('/')
}
13 changes: 10 additions & 3 deletions packages/server/src/util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@ describe('util', () => {
expect(joinURLPath('public', 'style.css')).toBe('public/style.css')
expect(joinURLPath('public/', 'style.css')).toBe('public/style.css')
})

it('should join paths starting with "/"', () => {
expect(joinURLPath('/foo', 'style.css')).toBe('/foo/style.css')
expect(joinURLPath('/', 'style.css')).toBe('/style.css')
})

it('should join paths with absolute public path', () => {
const publicPath = 'http://localhost:3001/public'

expect(joinURLPath(publicPath, 'style.css')).toBe(
`${publicPath}/style.css`,
`http://localhost:3001/public/style.css`,
)
expect(joinURLPath(`${publicPath}/`, 'style.css')).toBe(
`${publicPath}/style.css`,
`http://localhost:3001/public/style.css`,
)
})

it('should join paths with protocol free public path', () => {
const publicPath = '//127.0.0.1/public'
expect(joinURLPath(publicPath, 'style.css')).toBe(
`${publicPath}/style.css`,
`//127.0.0.1/public/style.css`,
)
})
})
Expand Down

0 comments on commit ba90289

Please sign in to comment.