Skip to content

Commit

Permalink
add more testcases and respect base for html files
Browse files Browse the repository at this point in the history
  • Loading branch information
fschade committed Feb 3, 2022
1 parent aaaa4a9 commit f284af2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
9 changes: 6 additions & 3 deletions packages/web-runtime/src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ export const buildUrl = (pathname) => {
}

if (/\.(html?)$/i.test(pathname)) {
baseUrl.pathname = base
? pathname
: [...baseUrl.pathname.split('/'), ...pathname.split('/')].filter(Boolean).join('/')
baseUrl.pathname = [
...(base ? [...new URL(base.href).pathname.split('/')] : [...baseUrl.pathname.split('/')]),
...pathname.split('/')
]
.filter(Boolean)
.join('/')
} else {
baseUrl[base ? 'pathname' : 'hash'] = router.resolve(pathname).href
}
Expand Down
21 changes: 12 additions & 9 deletions packages/web-runtime/tests/unit/router/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
describe('buildUrl', () => {
it.each`
location | base | path | expected
${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login'} | ${'https://localhost:8080/index.php/apps/web#/login'}
${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:8080/index.php/apps/web#/login/foo'}
${'https://localhost:8080/index.php/apps/web/#/login'} | ${''} | ${'/bar.html'} | ${'https://localhost:8080/index.php/apps/web/bar.html'}
${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:9200/#/login/foo'}
${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'}
${'https://localhost:9200/files/list/all'} | ${'/'} | ${'/login/foo'} | ${'https://localhost:9200/login/foo'}
${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'}
${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/bar.htm'} | ${'https://localhost:9200/bar.htm'}
location | base | path | expected
${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login'} | ${'https://localhost:8080/index.php/apps/web#/login'}
${'https://localhost:8080/index.php/apps/web/index.html#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:8080/index.php/apps/web#/login/foo'}
${'https://localhost:8080/index.php/apps/web/#/login'} | ${''} | ${'/bar.html'} | ${'https://localhost:8080/index.php/apps/web/bar.html'}
${'https://localhost:8080/index.php/apps/web/#/login'} | ${'/index.php/apps/web/foo'} | ${'/bar'} | ${'https://localhost:8080/index.php/apps/web/foo/bar'}
${'https://localhost:8080/index.php/apps/web/#/login'} | ${'/index.php/apps/web/foo'} | ${'/bar.html'} | ${'https://localhost:8080/index.php/apps/web/foo/bar.html'}
${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/login/foo'} | ${'https://localhost:9200/#/login/foo'}
${'https://localhost:9200/#/files/list/all'} | ${''} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'}
${'https://localhost:9200/files/list/all'} | ${'/'} | ${'/login/foo'} | ${'https://localhost:9200/login/foo'}
${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/login/foo'} | ${'https://localhost:9200/foo/login/foo'}
${'https://localhost:9200/files/list/all'} | ${'/'} | ${'/bar.html'} | ${'https://localhost:9200/bar.html'}
${'https://localhost:9200/files/list/all'} | ${'/foo'} | ${'/bar.html'} | ${'https://localhost:9200/foo/bar.html'}
`('$path -> $expected', async ({ location, base, path, expected }) => {
delete window.location
window.location = new URL(location) as any
Expand Down

0 comments on commit f284af2

Please sign in to comment.