fix(vue-app): not strip trailing slash for redirect external domain (#7533)

This commit is contained in:
Xin Du (Clark) 2020-06-18 16:32:39 +01:00 committed by GitHub
parent 39dd866b7b
commit 7f1429ebb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 66 additions and 22 deletions

View File

@ -597,6 +597,10 @@ function formatUrl (url, query) {
let result = (protocol ? protocol + '://' : '//') + parts.shift()
let path = parts.join('/')
if (path === '' && parts.length === 1) {
result += '/'
}
let hash
parts = path.split('#')
if (parts.length === 2) {

View File

@ -152,24 +152,9 @@ describe('basic ssr', () => {
expect(html).toContain('<h1>Index page</h1>')
})
test('/redirect', async () => {
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
expect(html).toContain('<div id="__nuxt"></div>')
expect(html).not.toContain('window.__NUXT__')
expect(redirected.path === '/').toBe(true)
expect(redirected.status === 302).toBe(true)
})
test('/redirect -> check redirected source', async () => {
// there are no transition properties in jsdom, ignore the error log
const window = await nuxt.server.renderAndGetWindow(url('/redirect'))
const html = window.document.body.innerHTML
expect(html).toContain('<h1>Index page</h1>')
})
test('/redirect -> external link', async () => {
describe('/redirect', () => {
let _headers, _status
const { html } = await nuxt.server.renderRoute('/redirect-external', {
const renderContext = {
res: {
writeHead (status, headers) {
_status = status
@ -177,10 +162,43 @@ describe('basic ssr', () => {
},
end () { }
}
}
test('/redirect', async () => {
const { html, redirected } = await nuxt.server.renderRoute('/redirect')
expect(html).toContain('<div id="__nuxt"></div>')
expect(html).not.toContain('window.__NUXT__')
expect(redirected.path === '/').toBe(true)
expect(redirected.status === 302).toBe(true)
})
test('/redirect -> check redirected source', async () => {
// there are no transition properties in jsdom, ignore the error log
const window = await nuxt.server.renderAndGetWindow(url('/redirect'))
const html = window.document.body.innerHTML
expect(html).toContain('<h1>Index page</h1>')
})
test('/redirect -> external link', async () => {
const { html } = await nuxt.server.renderRoute('/redirect-external', renderContext)
expect(_status).toBe(302)
expect(_headers.Location).toBe('https://nuxtjs.org/api/')
expect(html).toContain('<div data-server-rendered="true"></div>')
})
test('/redirect -> external link without trailing slash', async () => {
const { html } = await nuxt.server.renderRoute('/redirect-external-no-slash', renderContext)
expect(_status).toBe(302)
expect(_headers.Location).toBe('https://nuxtjs.org/api')
expect(html).toContain('<div data-server-rendered="true"></div>')
})
test('/redirect -> external link with root domain url', async () => {
const { html } = await nuxt.server.renderRoute('/redirect-external-root', renderContext)
expect(_status).toBe(302)
expect(_headers.Location).toBe('https://nuxtjs.org/')
expect(html).toContain('<div data-server-rendered="true"></div>')
})
expect(_status).toBe(302)
expect(_headers.Location).toBe('https://nuxtjs.org')
expect(html).toContain('<div data-server-rendered="true"></div>')
})
test('/special-state -> check window.__NUXT__.test = true', async () => {

View File

@ -261,7 +261,7 @@ describe('basic browser', () => {
await page.nuxt.navigate('/redirect-external', false)
await page.waitForFunction(
() => window.location.href === 'https://nuxtjs.org/'
() => window.location.href === 'https://nuxtjs.org/api/'
)
page.close()
})

View File

@ -0,0 +1,11 @@
<template>
<div>Redirecting...</div>
</template>
<script>
export default {
middleware ({ redirect }) {
return redirect('https://nuxtjs.org/api')
}
}
</script>

View File

@ -0,0 +1,11 @@
<template>
<div>Redirecting...</div>
</template>
<script>
export default {
middleware ({ redirect }) {
return redirect('https://nuxtjs.org/')
}
}
</script>

View File

@ -5,7 +5,7 @@
<script>
export default {
middleware ({ redirect }) {
return redirect('https://nuxtjs.org/')
return redirect('https://nuxtjs.org/api/')
}
}
</script>