Skip to content

Commit

Permalink
fix(vue-app): sanitize path with trailing slash in getLocation (#6744
Browse files Browse the repository at this point in the history
…) (#6745)
  • Loading branch information
crutch12 committed Jun 30, 2020
1 parent 6b06ab2 commit b4d3ebf
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/vue-app/template/utils.js
Expand Up @@ -282,7 +282,8 @@ export function getLocation (base, mode) {
if (mode === 'hash') {
return window.location.hash.replace(/^#\//, '')
}
if (base && path.indexOf(base) === 0) {
// To get matched with sanitized router.base add trailing slash
if (base && (path.endsWith('/') ? path : path + '/').startsWith(base)) {
path = path.slice(base.length)
}
return (path || '/') + window.location.search + window.location.hash
Expand Down
71 changes: 71 additions & 0 deletions test/e2e/spa-base.browser.test.js
@@ -0,0 +1,71 @@
import Browser from '../utils/browser'
import { loadFixture, getPort, Nuxt } from '../utils'

let port
const browser = new Browser()
const url = route => 'http://localhost:' + port + route

let nuxt = null
let page = null

describe('spa router base browser', () => {
beforeAll(async () => {
const config = await loadFixture('spa-base')
nuxt = new Nuxt(config)
await nuxt.ready()

port = await getPort()
await nuxt.server.listen(port, 'localhost')

await browser.start({
// slowMo: 50,
// headless: false
})
})

test('Open /app (router base)', async () => {
page = await browser.page(url('/app'))

expect(await page.evaluate(() => location.href)).toBe(url('/app'))

expect(await page.html()).not.toContain('This page could not be found')

expect(await page.evaluate(() => {
const headings = document.evaluate("//div[text()='Hello SPA!']", document, null, XPathResult.ANY_TYPE, null)
return headings.iterateNext()
})).not.toBe(null)
})

test('Open /app/ (router base with trailing slash)', async () => {
page = await browser.page(url('/app/'))

expect(await page.evaluate(() => location.href)).toBe(url('/app/'))

expect(await page.html()).not.toContain('This page could not be found')
})

test('Open /app/mounted', async () => {
page = await browser.page(url('/app/mounted'))

expect(await page.$text('h1')).toMatch('Test: updated')
})

test('/app/unknown', async () => {
page = await browser.page(url('/app/unknown'))

expect(await page.evaluate(() => location.href)).toBe(url('/app/unknown'))

expect(await page.html()).toContain('This page could not be found')
})

// Close server and ask nuxt to stop listening to file changes
afterAll(async () => {
await nuxt.close()
})

// Stop browser
afterAll(async () => {
await page.close()
await browser.close()
})
})
13 changes: 13 additions & 0 deletions test/fixtures/spa-base/nuxt.config.js
@@ -0,0 +1,13 @@
import { resolve } from 'path'
import defaultsDeep from 'lodash/defaultsDeep'
import baseNuxtConfig from '../spa/nuxt.config'

const config = {
buildDir: resolve(__dirname, '.nuxt'),
srcDir: resolve(__dirname, '..', 'spa'),
router: {
base: '/app'
}
}

export default defaultsDeep(config, baseNuxtConfig)
1 change: 1 addition & 0 deletions test/fixtures/spa/spa.test.js
Expand Up @@ -4,3 +4,4 @@ import { buildFixture } from '../../utils/build'
// That's why building both from same test file.
buildFixture('spa')
buildFixture('spa-hash')
buildFixture('spa-base')

0 comments on commit b4d3ebf

Please sign in to comment.